Wastholm.com

Wiki

Vim cheatsheet

updated

Command output in a buffer

Commands that produce output, like :map or :digraphs, dump this output in some sort of less-like buffer that is quite primitive compared to a "real" Vim buffer. I haven't found a way to convert this into a buffer after the fact but it is possible to run such commands through an incantation like this:

:enew | pu=execute('map')

This makes all normal Vim editing commands available.

Folds

First :set foldmethod=indent or :set foldmethod=manual (there are other foldmethods, but I personally don't use them).

Command Effect
z i Toggle folding
z a Toggle current fold
z A Toggle current fold recursively
z m Fold more (close one level)
z r Reduce folds (open one level)

Regular expressions

Vim regexes are a bit peculiar. Some things I can never remember and always have to look up:

  • Non-greedy search: use \{-} instead of *, e.g. foo.\{-}bar
  • Match across line endings: add \_, e.g. foo\_.\{-}bar

Tabs

Command Effect
:tabnew Open a new empty tab
g t Go to next tab on the right
g T Go to previous tab on the left
number g t Go to tab with given number

Vimdiff

To enter Vimdiff mode, either launch vimdiff file1 file2 or, inside Vim, open two buffers side by side (:vs) and do :windo diffthis. Or set up Git to use Vim as its difftool, see below.

Command Effect
:diffupdate Rescan files
[ c Go to previous change
] c Go to next change
d o Apply (obtain) this change from other buffer
d p Apply (push) this change to other buffer

Vimdiff as Git mergetool

Tell Git to use Vimdiff as difftool and mergetool with git config --global diff.tool vimdiff (and same with merge.tool). When resolving Git merge conflicts we get a layout with four panes:

  • LOCAL — our changes
  • BASE — common ancestor
  • REMOTE — their changes
  • MERGED (bottom pane, usually labeled with just the filename) — result

Go to the bottom pane. Use :diffget REMOTE and similar to pull in changes. Check with /^<<<<, /^====, /^>>>> if there are remaining conflicts.

Windows

Command Effect
:new Create a new window by splitting current window horizontally
:vnew Create a new window by splitting current window vertically
Ctrl-w arrow1 Move cursor between windows
Ctrl-w r Rotate windows
Ctrl-w R Rotate windows backwards
Ctrl-w t Go to top left window
Ctrl-w b Go to bottom right window
Ctrl-w │2 Maximize current window horizontally
Ctrl-w _ Maximize current window vertically
Ctrl-w = Equalize window sizes

  1. Yes, h/j/k/l work too. ↩

  2. That's actually supposed to be a vertical bar (|), not a "box drawings light vertical" Unicode character (│). Vimwiki, at least in Markdown mode, won't let me include a vertical bar inside a table. ↩