cheatsheet(1)
cheatsheet(1)
Vim for
for Muggles™
Muggles™
The Cheatsheet
CHEATSHEET CONVENTIONS • Flags: . . c – confirmation, g – for whole line, p – print, without
• Key combination – several keys pressed together, e.g. flag changes only first occurence
<Ctrl>+AN means <Ctrl>+<Shift>+aN . • Cancel highlight: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :nohlsearch
• Key sequence – several keys or combinations pressed • Search in more files: . . . . . . . :vimgrep /pattern/g f1 f2 …fn
gradually, e.g.. <Ctrl>+wwN means a press of combination REGULAR EXPRESSIONS
<Ctrl>+wN and than a key wN . • Begin/end of: . . . . . . . . . . . . line ^ / $, word \< / \>, file \%^ / \%$
• Working mode – it is displayed bottom right: • Last searched pattern/substitution: . . . . . . . . . . . . . . . . . . . . . .& / ~
∘ fooN – Normal mode • Matches any character: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (dot)
∘ fooI – Insert mode • Or/and: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \| / \&
∘ fooV – Visual mode • Suppress next special symbol: . . . . . . . . . . . . \., \&, $, \^, \\, \*
∘ :foo – Command line mode • Atom: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \(pattern\)
𝑁th reference: . . . . . . . . . . . \N, . . . . . . . . . . . first \(pattern\) = \1
BASIC CONTROL •
• Ranges: . . . . . . . . [abcd] ≈ [a-d], [a-zA-Z] ≈ \a, [0-9] ≈ \d
• Repeat command with typing number 𝑛× of repetitions before • Ignored chars and ranges:. . . . . . . . . . . . . . . . . .[^abcd] ≈ [^a-d]
command, e.g. 5xN deletes 5 chars from cursor position. Option • Zero or multiple z: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z* ≈ z\{0,\}
:set showcmd shows command sequence in bottom right. • One or multiple z: . . . . . . . . . . . . . . . . . . . . . . . . . . . . z\+ ≈ z\{1,\}
• Help: :help topic , use Tabex for command completion. • One or zero times: . . . . . . . . . . . . . . . . . . . . . . . . . . . z\? ≈ z\{0,1\}
• Similar commands uses same keys, e.g. fN / FN , tN / TN (see • Repetition z: . . . . . . z\{n\} accurate 𝑛×, z\{n,\} minimal 𝑛×,
below). The difference is direction or range of operation. z\{n,m\} min. 𝑛 and max. 𝑚 times
• Repeat last: . . . . . . . . . . . . . . . . . . . . . . . . . command .N , macro @@N
• Undo: . . . . . . . . . . . . . . . . . uN ≈ :undo , redo <Ctrl>+rN / :redo
EDIT MULTIPLE FILES
• Set or unset options with :set option a :set nooption . BUFFERS
Alone :set displays current options. • Start vim: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vim file1 file2…
• Shortening of options: . . . . . . . . . . . :set ignorecase ≈ :se ic • Display buffers: . . . . . . . . . . . . . . . . . . . :ls ≈ :files (numbered)
• More ex-commands together: . . . . . . . . . :command1 | command2 • Switch to next/previous file: . . . . . . . . . . . . . . . . . . . :next / :prev
• Comparison of two files: . . . . . . . . . . . . . . . . vimdiff file1 file2 • Switch to first/last file: . . . . . . . . . . . :rewind ≈ :first / :last
EDIT AND SAVE • Perform mass command for all files: . . . . . . . . . :bufdo command
• Edit: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :edit file WINDOWS
• Open new empty file: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :enew • New window: . . . . . . . . . . . . . . . . . . . . . . . . . . . . :new ≈ <Ctrl>+wnN
• Save/append file: . . . . . . . . . . . . . . . . . :write file / :w >> file • Split window: . . . . . . . . . . . . . . . . . . . . . . . . . :split ≈ <Ctrl>+wsN
• Save/append lines 𝑛 to 𝑚: . . . . . :𝑛,𝑚w file / :𝑛,𝑚w>> file • Vertically split window: . . . . . . . . . . . . . . :vsplit ≈ <Ctrl>+wvN
• Save and quit: . . . . . . . . . . . . . . . . . . . . . . . . . . . . :wq , enforced :wq! • Close window: . . . . . . . . . . . . . . . . . . . . . . . . :close ≈ <Ctrl>+wcN
• Quit without save: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :q! ≈ ZQN • Close all except current: . . . . . . . . . . . . . . . . :only ≈ <Ctrl>+woN
• Quit and save when file was changed: . . . . . . . . . . . . . . . :x ≈ ZZN • Jump to next window: . . . . . . . . . . . . . . . . . . . . . . . . . . . <Ctrl>+wwN
• Go to file under the cursos: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . gfN • Shrink/large window by 𝑛 lines: . <Ctrl>+w𝑛-N / <Ctrl>+w𝑛+N
• Change name of current file:. . . . . . . . . . . . . . . . . . . . . . . . :f jméno • Shrink/large window by 𝑛 columns: <Ctrl>+w𝑛<N / <Ctrl>+w𝑛>N
• Same size for all windows:. . . . . . . . . . . . . . . . . . . . . . . . <Ctrl>+w=N
MARKING OF POSITION
• Mass: . . . . . . . . . . . . . . . . . . . . . . write :wall , quit :qall , :wqall
• Insert invisible mark a: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . maN
• Perform mass command for all windows: . . . . :windo command
• Jump to mark a: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 'aN
• Open name under cursor in new window: . . . . . . . . . <Ctrl>+wfN
• Print marks: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :marks
MOVING IN WINDOW
• Erase mark a: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :delmarks a
• Jump to window line: . . . . . . . . . . . . . . top HN , middle MN , last LN
• Process text from mark a to b: . . . . . . . . . . :'a,'b příkaz , e.g.
• Jump to 𝑛th line from top/bottom:. . . . . . . . . . . . . . . . . . 𝑛HN / 𝑛LN
:'a,'b !sort will use external command for sort
• Set actual line to: . . . . . . top z<End>N , center z.N , bottom z-N
INSERT SPECIAL SYMBOLS • Scroll window up/down by:line <Ctrl>+eN / <Ctrl>+yN , page
• Display unprintable characters: . . . . . . . . . . . . . . . . . . . . :set list <Ctrl>+bN / <Ctrl>+fN , half-page <Ctrl>+uN / <Ctrl>+dN
• Insert ASCII char: . . . . . . . . . . . . . . . . . . . . . <Ctrl>+v code <End>I • Redraw window without scrolling: . . . . . . . . . . . . . . . . . <Ctrl>+lN
• Show ASCII/UTF-8 of char under cursor: . . . . . . . . . . . gaN / g8N TABS
• Suppress control character, e.g. <Tab> (^I): . <Ctrl>+v <Tab>I
• Command: . . . . . . . . . . . . . . . vim -p 𝑓1, 𝑓2 … open files in tabs
• Table of Unicode digraphs: . . . . . . . . . . . . . . . . . . . . . . . . :digraphs
• Create/close tab:. . . . . . . . . . . . . . . . . . . . . . . . . . . . :tabnew / :tabc
• Insert digraphs: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . <Ctrl>+k K1 K2I
• Close all, except current tab:. . . . . . . . . . . . . . . . . . . . . . . . . . . :tabo
• Digraphs: ± +- ° DG × *X ÷ -: – -N — -M „ :9 “ :6
• Go to next/previous: :tabn ≈ <Ctrl>+<PageDown>I / :tabp ≈
• Define digraphs .3 for „…“ (U+2026): . . . . . . . . . :dig .3 8230
<Ctrl>+<PageUp>I
SEARCH AND REPLACE • Move after the fold 𝑛: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :tabm 𝑛
• Ignore case: . . . . . . . . . . :set ignorecase , opposite :set noic • Perform mass command: . . . . . . . . . . . . . . . . . . . . :tabdo command
• Repeat last search:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . /N / ?N • Change tab design: . . . . . . . . . . . . . . . . . . . . . . . . . . . . :set tabline
• Ignore case: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . /\cpatternN • Open name of file under cursor in new tab: . . . . . . <Ctrl>+wgfN
• Search for word under the cursor: . . #N forward, . . *N backward PROGRAMMER SUPPORT
• Search by ASCII code of „M“: . . . . . . . . . \%d77, \%x4d, \%o0115
• Run make: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :make
• Search Unicode character „𝛼“ (U+03B1): . . . . . . . . . . . . \%u03b1
• Jump to next/previous error: . . . . . . . . . . . :cprevious / :cnext
• Substitution: . . . . . . . . . . :substitute/from/to/flag , abbr. :s
• List all errors: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . :clist
• Repeat last substitution: . . . . . . . . . . . . . . . . . . . . . . . :s ≈ :s//~/
• Add window with list of errors: . . . . . . . . . . . . . . . . . . . . . . . :copen
• The delimiter / can be any char instead \, " and |
NORMAL MODE
SYNTAX MOTIONS
• [𝑛+]𝐨𝐩𝐞𝐫𝐚𝐭𝐨𝐫 + 𝐦𝐨𝐭𝐢𝐨𝐧: . . . . . . operate during motion n times
• [𝑛+]𝐨𝐩𝐞𝐫𝐚𝐭𝐨𝐫 + 𝐭𝐞𝐱𝐭𝐨𝐛𝐣𝐞𝐜𝐭: . . . operate on text object n times • Matching bracket/parenthesis: . . . . . . . . . . . . . . . . . . . . . . . %N
• [𝑛+](𝐨𝐩𝐞𝐫𝐚𝐭𝐨𝐫 ∗ 𝟐): . . . . . . . . . . . operate on current line n times • Forward/Back. to start/end of word: wN / bN // eN / geN
• [𝑛+]𝐚𝐜𝐭𝐢𝐨𝐧: . . . . . . . . . . . . . . . . . . . . . . . . . . . perform action n times • Forward/Back. to start/end of WORD: WN / BN // EN / gEN
Example • Sentence forward/backwards: . . . . . . . . . . . . . . . . . . . )N / (N
• c GN . . . . . . . . . . . . . . . . . . . . . . . . . . change from here to end of file • Paragraph forward/backwards: . . . . . . . . . . . . . . . . . . }N / {N
• 3 ddN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . delete 3 lines VERTICAL
• 2 gq apN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . format 2 paragraphs • Goto line x: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xGN
• 10 JN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . join 10 lines • Goto file percent: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x%N
ACTIONS • Repeat last search in the same/reverse direction: nN / NN
• Start/End of file: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ggN / GN
• Repeat last command: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .N • Lines up/down: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . kN / jN
• Undo: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . uN , line UN • Beggining of next/previous line: . . . . . . . . . . . . . . . . +N / -N
• Replace character by x: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rxN • Search forward: . . . . . . . . . . . . . . . . /patternN / ?patternN
• Enter replace mode:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RN • Up/Down 12 page: . . . . . . . . . . . . . . <Ctrl>+uN / <Ctrl>+dN
• Enter insert mode before/after current character: iN / aN • Backwards/Forward a page: . . . . <Ctrl>+bN / <Ctrl>+fN
• Enter insert mode start/end of the line: . . . . . . . . . IN / AN • Top/middle/bottom of visible text: . . . . . . . . . . . HN / MN / LN
• Add new line: . . . . . . . . . . . . . . . . . . . . . . . . after oN , before ON HORIZONTAL
• Join lines: . . . . . . . . . . . . . . . . . . . . . . . JN , without spaces gJN • Character right/left: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lN , hN
• Substitute: . . . . . . . . . . . . . . . . . . . . . . . . character sN , line SN • First/Last character: . . . . . . . . . . . . . . . . . . . . . . . . . . . 0N / $N
• Paste (put) register: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . pN • First non-blank character: . . . . . . . . . . . . . . . . . . . . . . . . . . . . ^N
• Delete character: . . . . . . . . . . . . . . . . . . . xN , before cursor XN • Next/Last character x occurrence: . . . . . . . . . . . . fxN / FxN
• Change to end of line:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CN • Before/After character x occurrence:. . . . . . . . . . txN / TxN
• Increment/Decrement number: . <Ctrl>+aN / <Ctrl>+xN • Repeat/Reverse last fN , tN , FN , TN : . . . . . . . . . . . . ;N / ,N
VISUAL MODE
OPERATORS CHANGING SELECTION
Normal Mode operators apply to selection. Additionally: Selection can be modified using Normal Mode motions and text
• Block insert/append: . . . . . . . . . . . . . . . . . ( IV / AV ) text <Esc>I objects.
• Perform ex command in selected range: :V → :'<,'> command • Chaging the visual area top-left/bottom-right: . . . . . . . . oV / OV