Heading image for post: 8 Great Vim Mappings


8 Great Vim Mappings

Profile picture of Vic Ramon

I frequently create mappings or functions whenever I find myself repeating the same keystrokes. By now I've accumulated a decent list of custom mappings. Here are a few of my favorites:

Number 8: Clone Paragraph with cp

noremap cp yap<S-}>p

This will copy the paragraph your cursor is on then paste a copy of it just below. This is great when you're about to create a block of code that will be similar, but not different enough to abstract (e.g. it blocks in rspec).

Number 7: Align Current Paragraph with Leader + a

noremap <leader>a =ip

Quickly align your current paragraph with this command. Sometimes this can help you see where you're missing an end or a bracket.

Number 6: Toggle Paste Mode

set pastetoggle=<leader>z

Avoid typing :set paste and :set nopaste by setting a paste toggle command.

Number 5: Apply Macros with Q

nnoremap Q @q
vnoremap Q :norm @q<cr>

This mapping makes macros even easier to remember: hit qq to record, q to stop recording, and Q to apply.

This mapping also allows you to play macros across a visual selection with Q.

Number 4: Shift + Direction to Change Tabs

noremap <S-l> gt
noremap <S-h> gT

Who has time to type gt and gT? Not me. Using shift and a direction to change tabs is a great alternative.

Number 3: Control + Direction to Change Panes

noremap <C-l> <C-w>l
noremap <C-h> <C-w>h
noremap <C-j> <C-w>j
noremap <C-k> <C-w>k

Same thing goes for changing panes, but these use control . Skip that pesky w and get straight to the good stuff.

Number 2: Quit Files with Leader + q

noremap <leader>q :q<cr>

Quickly close a file with <leader>q.

Number 1: Save File with Leader + s

nnoremap <leader>s :w<cr>
inoremap <leader>s <C-c>:w<cr>

If you're like me and you like to constantly save files, this one is nice. Just hit <leader>s. The second line makes this work in insert mode too, saving you even more effort.

That's it. Got your own favorite mappings? Share them in the comments!

More posts about Community Vim