Vim Tricks

Increment/Decrement Integers

In normal mode the Ctrl-a command finds the next integer on the line at or after the cursor and increments it. The Ctrl-x command works similarly but decrements the integer.

Either command can be prefaced by a number to increment or decrement by that amount, e.g.


will find the next integer on the line and add ten to it.

Vim defaults to treating numbers with leading zeros as octal but this can be customized by setting the nrformats attribute in your .vimrc file, e.g.

set nrformats=bin,hex

will ignore leading zeros but recognise 0b and 0x as prefixes for binary and hexadecimal bases.

Delete All Lines Containing A Pattern

The global command :g runs a command on all lines containing a pattern. To see which lines will be affected run:


This is equivalent to calling :p or :print on each affected line:


To delete all lines containing a pattern use the :d or :delete command:


To delete all lines not containing a pattern use :g! to invert the selection:


Sidenote — the global print command, :g/<regex>/p, is where the grep tool gets its name.

Jump Between Marks

Marks are easy to use and let you jump around within and between files. The m<letter> command (where <letter> is a single letter) marks the current cursor location with the specified letter. Lowercase letters are local to the buffer while uppercase letters are global and let you jump between files.

The `<letter> command jumps the cursor to the specified mark. That's a backtick by the way — the similar-looking '<letter> command (with an apostrophe) jumps the cursor to the first non-whitespace character on the line containing the mark.

Jump Between Matching Parentheses

The % command jumps between opening and closing parentheses. It works with parentheses (), braces {}, and square brackets [], though not with angle brackets <>.

Vertical Guides

You can add a vertical guideline to the screen using the colorcolumn command:

:set colorcolumn=80

Multiple guides can be added by specifying a comma-separated list of column numbers:

:set colorcolumn=80,100

To turn the guide off, set its value to zero:

:set colorcolumn=0

The default color is bracing red but you can customize it with the highlight command:

:highlight ColorColumn ctermbg=233

Numbered Registers

There are ten numbered registers, "0 to "9, which are automatically populated by Vim.

You can view the contents of all numbered and named registers using the registers command, or reg for short:


Toggle Spellcheck

I have the following line in my .vimrc file so I can toggle spellcheck on and off using the <leader>s combination:

nnoremap <leader>s :set spell!<cr>

The only problem is remembering the cryptic spellcheck commands:

Toggle Search Highlighting

Adding the following line to your .vimrc file creates a useful shortcut for telling Vim to stop highlighting the last search term. (Highlighting will be automatically re-enabled after the next search.)

nnoremap <silent> <C-L> :noh<cr><C-L>

This shortcut piggy-backs on the existing Ctrl-L command which clears and redraws the screen.

Command History Scrolling

When typing a command on Vim's command line, the <up> and <down> arrow keys match the existing prefix when scrolling backwards and forwards through the command history. Ctrl-P (previous) and Ctrl-N (next) also scroll through the command history but ignore the prefix.

You can remap Ctrl-P and Ctrl-N to behave like the arrow keys by adding the following lines to your .vimrc file:

cnoremap <C-P> <Up>
cnoremap <C-N> <Down>

Unfortunately this simple remapping stops Ctrl-P and Ctrl-N working with wildmenus. The more complex remapping below avoids this problem:

cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"

Insert A Blank Line

This .vimrc snippet lets you hit the Enter key in normal mode to insert a blank line below the cursor:

nnoremap <Enter> :call append(line('.'), '')<CR>

This doesn't affect the cursor's position.

Buffer Scrolling

Adding the following lines to your .vimrc file enables you to scroll the buffer up and down using Ctrl-P and Ctrl-N while keeping the cursor at the same relative position within the window:

nnoremap <C-N> <C-E>gj
nnoremap <C-P> <C-Y>gk

By default Ctrl-P and Ctrl-N act just like j and k so this mapping doesn't overwrite any useful functionality.

Move Around Within Soft-Wrapped Lines

In normal mode you can move up and down within a soft-wrapped line (i.e. a line that's visually wrapped on the screen) using gj and gk instead of j and k.

Similarly, g0, g^, and g$ move the cursor horizontally within a soft-wrapped line.

Jump Around

Jump the cursor around the window:

H - jump to the top of the window
M - jump to the middle of the window
L - jump to the bottom of the window

Jump the window around the cursor:

zz - move the current line to the middle of the window
zt - move the current line to the top of the window
zb - move the current line to the bottom of the window

Keep Your Own Vim Settings When Using Sudo

This is one of my favourite tricks. Instead of running:

$ sudo vim <somefile>


$ sudo -E vim <somefile>

The -E flag preserves your existing environment variables so your normal .vimrc file will get loaded.

Copy From Above Or Below The Cursor

In insert mode Ctrl-Y copies and inserts the character directly above the cursor; Ctrl-E copies and inserts the character directly below the cursor.

Automatically Center Search Matches on the Screen

I've added the following mappings to my .vimrc file. They automatically center the screen on the next search match:

nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz

Swap Selection End in Visual Mode

In visual mode, pressing o jumps the cursor to the opposite end of the selection, i.e. from the end to the beginning or vice versa. You can use this feature to expand your selection in either direction, working backwards from the start or forwards from the end.

Reselect Last Visual Selection

In normal mode you can reselect the last visual selection by typing gv.