The leader key is a useful way of creating custom mappings in Vim. While browsing code in normal mode, it can be useful to quickly toggle settings on and off or execute other commands. By defining mappings with the leader key, a short sequence of keys can be used to quickly execute commands. Check out
:help <Leader> for more information on the leader key and defining mappings.
This post contains some examples from my vimrc file that are frequently helpful. These mappings should work on gVim 7.3 (probably other Vim/gVim versions too but not tested) without additional dependencies.
UPDATE: Simplified toggle commands thanks to suggestion from Reddit user Elessardan. Updated use case info and "mapping" verbage thanks to suggestion from Reddit user -romainl-. Various simplifications and updates thanks to suggestions from this Reddit thread. Please note the GIFs still show the original mappings.
Toggle Line Wrap
Useful when working with long lines or small windows.
nnoremap <Leader>wr :set wrap! wrap?<CR>
Highlight Word Under Cursor
Useful for quickly identifying instances of a word. Use
SHIFT-n to jump between matches and
:noh to turn off highlighting.
" Case sensitive, partial match inclusive. nnoremap <Leader>hi :set hlsearch<CR>:let @/='<C-r><C-w>'<CR> " Case sensitive, no partial match. nnoremap <Leader>ho :set hlsearch<CR>:let @/='\<<C-r><C-w>\>'<CR> " Case insensitive, partial match inclusive. nnoremap <Leader>hu :set hlsearch<CR>:let @/='<C-r><C-w>\c'<CR> " Case insensitive, no partial match. nnoremap <Leader>hy :set hlsearch<CR>:let @/='\<<C-r><C-w>\>\c'<CR>
Toggle List (Whitespace) Display
Can be useful depending on your Vim setup. I typically use 4 spaces for indentation but some people prefer tabs. My
listchars variable is set to display tabs as
>· but sometimes it is nice to quickly hide the list characters (especially if editing a file where someone mixed tabs and spaces, the savage).
nnoremap <Leader>li :set list! list?<CR>
Zooming out can be useful for navigation (like the minimap in Sublime Text). Zooming in can be useful for presentations. Keep in mind this is not really a zoom, it is just changing the font size. This can cause some strange side-effects such as changing window size and position.
" Simple zoom hacks; Ultra, Extra, Normal, Out. nnoremap <Leader>zu :set guifont=courier_new:h24<CR> nnoremap <Leader>ze :set guifont=courier_new:h18<CR> nnoremap <Leader>zn :set guifont=courier_new:h10<CR> nnoremap <Leader>zo :set guifont=courier_new:h4<CR>
Highlighting the cursor line is pretty standard but there is a known issue in Vim which causes slowdown when long lines are highlighted; quickly toggling line highlighting can be useful. Toggling column toggling can be occasionally useful too.
nnoremap <Leader>cl :set cursorline!<CR> nnoremap <Leader>cc :set cursorcolumn!<CR> nnoremap <Leader>cx :set cursorcolumn cursorline<CR> nnoremap <Leader>cn :set nocursorline nocursorcolumn<CR>
Use this to quickly fix spelling mistakes. Use
[s to jump between flagged mistakes.
nnoremap <Leader>fs 1z=
Use this to quickly open your vimrc file.
nnoremap <Leader>rc :e $MYVIMRC<CR>