Post

Vim cheatsheet

Made an tested with an azerty keyboard; paths relate to Debian.

In case of emergency

  • Quit Vim: :q
  • Quit Vim anyway: :q!
  • Help: F1 (quit help: :q)
  • Help about command: :help command
  • If the requested command name is incomplete, use TAB for auto completion of CTRL+d to get the list of available commands

Vim modes

  • Normal mode: ESC
  • Insert mode, before or after cursor: i or a
    • Replace mode: R
  • Visual mode: v (“cursor visual”) or V (“line visual”)
  • Command mode: :
  • Move cursor: left, down, up, right: h, j, k, l, respectively; can be preeceded by a number.
  • Go to the start or end of line: 0 or $
  • Go to start or end of block: ALT+{ or ALT+}
  • Go to the start or end of file: gg or G
  • Go to line number n: nGG
  • Show position status: CTRL+g
  • Go to next word: w
  • Go to end of word: e
  • Go to previous word: b
  • Go to beginning of current word: b
  • Go to next or previous pattern under cursor: * or #
  • Go to matching bracket when on (, ), [, ], {, }, <, or >: %

Apart from %, these commands can be preceeded by a number; e.g. 5 w goes 5 words forward.

  • Search for a word: /word
  • Backward search: ?word
  • Got to next or previous match: n or N
  • Highlight search result: :set hls
  • Ignore case: :set ic

Selection

  • Enter in cursor visual visual mode: v
  • Enter line visual visual mode: V
  • When in visual mode, most of the navigation commands above will select the relevant text; e.g. vG will select text from cursor to the end of the file.
  • Save selection to filename: :w filename
  • Copy selected text: y
  • Delete selected text: d
  • Paste the copied selection before or after cursor or line: P or p

Edition

  • Copy: y
  • Paste: P or p
  • Copy and paste require some text to be selected; see section “Selection” above.
  • Undo: u
  • Redo: CTRL+r

Indentation

  • Indent current line: >>
  • Remove current line indentation: <<
  • Indent selection: >; . to repeat
  • Remove selection indentation: <; repeat with .
  • Example: V}>. selects the current line up to the end of the block and indents it 2 times

Comments

TODO

Deletion

  • Delete char under cursor: x
  • Delete current line: dd (or Vd)
  • Delete n lines: ndd
  • In addition, d can be combined to most of the navigation commands above, e.g. d0 will delete text from cursor position to the beginning of the line;

Replacement

  • Replace char under cursor with a: ra
  • Replace mode: R and overwrite; ESC when finished

See also substitution commands below.

Insert mode

  • Enter in insert mode before or after cursor: i or a
  • Go to beginning or end of line and enter visual mode: I or A
  • Delete text and enter insert mode: c combined with a displacement; e.g. c2w deletes 2 words and enters to insert mode
  • Delete until end of line and enter insert mode: C
  • Open a new line before or after current line and enter insert mode: O or o

Substitution

  • Replace a by b: :s/a/b
  • Replace a by b in the wole line: :s/a/b/g
  • Replace a by b in the whole document: :%s/a/b/g
  • Replace a by b between line n°x and line n°y: :x,ys/a/b/g
  • Ask for confirmation before replacement: use gc instead of g

I/O

  • Save file: :w
  • Save and quit: :wq or :x
  • Save to filename: :w filename
  • Read from filename and put data after current line: :r filename
  • Execute a shell command: :!command
  • Execute command and set output after current line: :r!command

Layout

Vertical pane

  • Vertical split: CTRL+w v
  • Increase width of vertical pane: CTRL+w >
  • Increase width of vertical pane by n: n CTRL+w >
  • Decrease width of vertical pane: CTRL+w <
  • Decrease width of vertical pane by n: n CTRL+w <
  • Set width=n: n CTRL+w |

Horizontal pane

  • Horizontal split: CTRL+w s
  • Increase height of horizontal pane: CTRL+w +
  • Increase height of horizontal pane by n: n CTRL+w +
  • Decrease height of horizontal pane: CTRL+w -
  • Decrease height of horizontal pane by n: n CTRL+w -
  • Set height=n: n CTRL+w _

Common

  • Equalize 2 panes: CTRL+w =
  • Navigate between panes: CTRL+w hjkl
  • Close pane: CTRL+w c
  • Open the file explorer to the left: :Lexplore ENTER

Color

  • Show current color scheme used: :colorscheme
  • List available color schemes: :colorscheme TAB or :colorscheme CTRL+d
  • Use colorscheme name: :colorscheme name

Default methods for highlighting are found in file /usr/share/vim/vim<version>/syntax/syncolor.vim; I edit this file when I don’t want things like special words being highlighted in bold.

Additional keywords can be highlighted for a given language, e.g. C:

1
2
3
4
5
6
7
cat ~/.vim/after/syntax/c.vim 
syn keyword msType QWORD DWORD WORD BYTE
syn keyword msType BOOL HANDLE PSLIST_HEADER HWND ATOM HBRUSH WNDCLASSA
syn keyword msConstant TRUE FALSE WM_CLOSE WM_DESTROY WM_TIMER

hi link msType cType
hi link msConstant cConstant

New color schemes can be installed. I put the one I’m using in /usr/share/vim/vim<version>/colors/.


EOF

This post is licensed under CC BY 4.0 by the author.