Useful Vim Mappings

A few mappings to help boost your Vim workflow.

Rotate through different line numbering settings

Switch between absolute line numbers (normal), relative numbers (based on distance from your cursor) and no numbers at all using CTRL-n:

[crayon toolbar=’false’]

function! NumberToggle()
if(&relativenumber == 1)
set number
elseif(&number == 1)
set nonumber
set relativenumber

nnoremap :call NumberToggle()


Easier git blame

Ever wonder who wrote the line of code you’re looking at? With this mapping, select a line or group of lines in Visual mode, then use “g” to run git blame on them.

[crayon toolbar=’false’]
vmap g :!git blame =expand(“%:p”) | sed -n =line(“‘<“) ,=line(“‘>”) p
Credit to Thoughtbot

Display tabs and unncessary white spaces

Hate those ugly looking Git files full of unnecessary white spaces and tabs? This mapping toggles a specified group of “listchars,” ordinarily invisible characters that you sometimes want to see:

[crayon toolbar=’false’]
nnoremap l :set list! listchars=tab:»·,trail:·

To just leave this on, you can use:

[crayon toolbar=’false’]
set list listchars=tab:»·,trail:·

Select all text in a buffer

Surprisingly, there’s no native mapping for this, but it’s simple enough:

[crayon toolbar=’false’]
noremap a ggVG

Move lines up and down (à la Sublime Text)

These mappings move the current line up or down.

[crayon toolbar=’false’]
nnoremap :m .+1==
nnoremap :m .-2==
inoremap :m .+1==gi
inoremap :m .-2==gi

Toggle active buffers

To toggle quickly between active buffers, I use this mapping:

[crayon toolbar=’false’]
noremap v :b#

Indent in Visual mode

Use Tab and Shift-Tab to indent and unindent lines in Visual mode:

[crayon toolbar=’false’]
vmap >gv
vmap <gv

Search using normal regular expressions

By default, Vim uses its own regular expression syntax. If you want to search using PCRE (Perl-compatible regular expressions, the kind used in Ruby, Python, etc.), you can enable “very magic mode” with \v. Very magic mode isn’t exactly the same as PCRE, but is quite close. To default to very magic mode, I use the following mappings, suggested by Steve Losh:

[crayon toolbar=’false’]
nnoremap / /\v
vnoremap / /\v

Copy and Paste with CTRL-c and CTRL-v>

[crayon toolbar=’false’ lang=’vim’]
let &clipboard = has(‘unnamedplus’) ? ‘unnamedplus’ : ‘unnamed’
” map c-x and c-v to work as they do in windows, only in insert mode
vm “+x
vm “+y
cno +
exe ‘ino

Leave a Reply