[linux@yak.onl ~]$ Vim cheatsheet+

This is a cheatsheet for some vim basic reference, plus a couple plugins that I use. It is a work-in-progress. Last updates 3.2019. If you wish to lend your own tips or command snippets, you can get in touch via:



Vim init

  • vim - launch vim
  • vim + mydocument.txt - in terminal, out of vim, open/create "mydocument.txt"

Vim repo/docs

  • vim - launch vim
  • vim + mydocument.txt - in terminal, out of vim, open/create "mydocument.txt"

Quick Command Basics

  • i - enter "insert" mode (typing)
  • ESC - exit to "normal/command" mode (navigation/editing/shell for commands)
  • : - command-line mode (type explicit vim or vim plugin commands)
  • :w - save/write to file (overwrite/update file timestamp)
  • :q - quit editor (or doc sections) without saving
  • :wq! - ex. write, quit ! forces; executing in sequence
  • :x - exit, plus save and update timestamp only if unaved changes
  • . - repeat last command

Quick Nav Basics

  • simple movement - h (arrow left); j (arrow down); k (arrow up); l (arrow right)
  • [TAB]/ / [SHIFT] + [TAB] - find next/prev wiki link in normal mode
  • w - next word in normal mode
  • b - back a word in normal mode
  • 24gg - ex. goto line 24

Quick Editing Basics

  • u - undo (typing) in normal mode
  • 3yy - ex. copy 3 lines in normal mode
  • 3dd - ex. cut 3 lines in normal mode
  • 3cc - ex. change (replace) 3 lines in normal mode
  • p / P- put (paste) after/before cursor in normal mode
  • cw - ex. replace (change) word in normal mode
  • dw - ex. delete word in normal mode

vimwiki.vim (plug-in)

  • vimwiki txt man / github
  • \ default "leader" keystroke to init vimwiki commands
  • \ww - open current vimwiki index page
  • \ws - open vimwiki wiki list (as defined in ~/.vimrc); select/switch a wiki
  • [Enter] - (or carriage return) over word in normal/command mode, creates new page/doc
  • \wr - rename current page/doc
  • \wd - del current page/doc
  • :help + vimwiki - help doc
  • :h vimwiki-syntax- markup ref for insert mode
  • :h vimwiki-mappings - vimwiki command ref/key bindings
  • :resize [+/-][integer - adjust split pane heights in doc, ex. :resize +40
  • [Enter] - create link on word in normal mode, 2x enter linked page node
  • [Backspace] - exit page node, and go back a level
  • [Tab] - jump forward cycling through links
  • vim myproblemdocument.wiki - MAJOR TIP: If your terminal crashes or you close it without saving a document with edits > swap file error: "Error detected while processing function vimwiki#base#follow_link[58]..". A swap file was created and you must locate it, restore or delete it to remove further error. The .swp file should be located in your .cache. Open file direct from plain terminal commandline to locate and correct, or navigate to ~/.cache/vim/swap/ and simply delete if warranted.

Global

  • :help keyword - open help for keyword
  • :saveas file - save file as
  • :close - close current pane
  • K - open man page for word under the cursor

Cursor movement

  • h - move cursor left
  • j - move cursor down
  • k - move cursor up
  • l - move cursor right
  • H - move to top of screen
  • M - move to middle of screen
  • L - move to bottom of screen
  • w - jump forwards to the start of a word
  • W - jump forwards to the start of a word (words can contain punctuation)
  • e - jump forwards to the end of a word
  • E - jump forwards to the end of a word (words can contain punctuation)
  • b - jump backwards to the start of a word
  • B - jump backwards to the start of a word (words can contain punctuation)
  • % - move to matching character (default supported pairs: '()', '{}', '[]' - use :h matchpairs in vim for more info)
  • 0 - jump to the start of the line
  • ^ - jump to the first non-blank character of the line
  • $ - jump to the end of the line
  • g_ - jump to the last non-blank character of the line
  • gg - go to the first line of the document
  • G - go to the last line of the document
  • 5G - go to line 5
  • fx - jump to next occurrence of character x
  • tx - jump to before next occurrence of character x
  • Fx - jump to previous occurence of character x
  • Tx - jump to after previous occurence of character x
  • ; - repeat previous f, t, F or T movement
  • , - repeat previous f, t, F or T movement, backwards
  • } - jump to next paragraph (or function/block, when editing code)
  • { - jump to previous paragraph (or function/block, when editing code)
  • zz - center cursor on screen
  • Ctrl + e - move screen down one line (without moving cursor)
  • Ctrl + y - move screen up one line (without moving cursor)
  • Ctrl + b - move back one full screen
  • Ctrl + f - move forward one full screen
  • Ctrl + d - move forward 1/2 a screen
  • Ctrl + u - move back 1/2 a screen
Tip Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert mode - inserting/appending text

  • i - insert before the cursor
  • I - insert at the beginning of the line
  • a - insert (append) after the cursor
  • A - insert (append) at the end of the line
  • o - append (open) a new line below the current line
  • O - append (open) a new line above the current line
  • ea - insert (append) at the end of the word
  • Esc - exit insert mode

Visual BLOCK Mode- prepend/append to multiblock of lines

  • ESC enter command mode
  • CTRL + V enter visual BLOCK mode
  • Up / Down select your text block
  • SHIFT + i and type text to prepend
  • SHIFT + a and type text to append
  • ESC and wait 1 second.

Command Mode- prepend/append to multiblock of lines

  • :13,32s/^/# / (reads) for lines 13-32, at start of line insert "# "
  • :13,32s/$/# / (reads) for lines 13-32, at end of line insert "# "
  • TIP: visal block mode can be use to select range then :
  • :s/^/* /prepend "* " to current line
  • :g/\S/s/^/* / g/\S/ is a range operator (analogous to % except it selects all lines with a non-blank character)
  • :g/\S/s/$/ */ appends " *" at the end of each selected line
  • :%s/^\(.*[^\n]\)$/* \1/ insert "* " at the beginning of only those lines with text
  • :% all lines
  • s/ beginning of substitute command; begin pattern
  • ^ beginning of line
  • \( beginning of group we want to preserve (see below, rel.)
  • .* any number of characters
  • [^\n] some character besides newline
  • \) end of group
  • $ end of line
  • / end of pattern, beginning of substitution
  • * \1 insert *, then the first group that we selected on the left
  • / end of substitution and command

Editing, Formatting, Reflow

  • r - replace a single character
  • J - join line below to the current one with one space in between
  • gJ - join line below to the current one without space in between
  • gwip - reflow paragraph using native vim, conforming to :set textwidth=x (columns), ex. set textwidth=80 (line in .vimrc would reflow to 80 columns on key sequence)
  • gqip - reflow paragraph using external formatter (ex. par installed on system) :set textwidth=x (columns), ex. set formatprg=par\ -w80 (line in .vimrc would reflow with par using gqip instead of native vim formatting)
  • cc - change (replace) entire line
  • c$ - change (replace) to the end of the line
  • ciw - change (replace) entire word
  • cw - change (replace) to the end of the word
  • s - delete character and substitute text
  • S - delete line and substitute text (same as cc)
  • xp - transpose two letters (delete and paste)
  • u - undo
  • Ctrl + r - redo
  • . - repeat last command

Marking text (visual mode)

  • v - start visual mode, mark lines, then do a command (like y-yank)
  • V - start linewise visual mode
  • o - move to other end of marked area
  • Ctrl + v - start visual block mode
  • O - move to other corner of block
  • aw - mark a word
  • ab - a block with ()
  • aB - a block with {}
  • ib - inner block with ()
  • iB - inner block with {}
  • Esc - exit visual mode

Visual commands

  • > - shift text right
  • < - shift text left
  • y - yank (copy) marked text
  • d - delete marked text
  • ~ - switch case

Registers

  • :reg - show registers content
  • "xy - yank into register x
  • "xp - paste contents of register x
Tip Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim.
Tip Register 0 contains always the value of the last yank command.

Marks

  • :marks - list of marks
  • ma - set current position for mark A
  • `a - jump to position of mark A
  • y`a - yank text to position of mark A

Macros

  • qa - record macro a
  • q - stop recording macro
  • @a - run macro a
  • @@ - rerun last run macro

Cut and paste

  • yy - yank (copy) a line
  • 2yy - yank (copy) 2 lines
  • yw - yank (copy) the characters of the word from the cursor position to the start of the next word
  • y$ - yank (copy) to end of line
  • p - put (paste) the clipboard after cursor
  • P - put (paste) before cursor
  • dd - delete (cut) a line
  • 2dd - delete (cut) 2 lines
  • dw - delete (cut) the characters of the word from the cursor position to the start of the next word
  • D - delete (cut) to the end of the line
  • d$ - delete (cut) to the end of the line
  • x - delete (cut) character

Saving & Exiting

  • :w - write (save) the file, but don't exit
  • :w !sudo tee % - write out the current file using sudo
  • :wq or :x or ZZ - write (save) and quit
  • (*) w(rite) to buffer/overwrite file and file modified timestamp. x will exit and not save or update timestamp if unless there are unsaved changes.
  • :up updates buffer and timestamp only if unsaved changes.
  • :q - quit (fails if there are unsaved changes)
  • :q! or ZQ - quit and throw away unsaved changes
  • :wqa - write (save) and quit on all tabs

Search and replace

  • /pattern - search for pattern
  • ?pattern - search backward for pattern
  • \vpattern - 'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
  • n - repeat search in same direction
  • N - repeat search in opposite direction
  • :%s/old/new/g - replace all old with new throughout file
  • :%s/old/new/gc - replace all old with new throughout file with confirmations
  • :noh - remove highlighting of search matches

Search in multiple files

  • :vimgrep /pattern/ {file} - search for pattern in multiple files
e.g. :vimgrep /foo/ **/*
  • :cn - jump to the next match
  • :cp - jump to the previous match
  • :copen - open a window containing the list of matches

Matching & Highlighting

  • :match Todo /myword/ - match and highlight with "Todo" vim style word or phrase
  • :match Error /myword/ - match and highlight with "Error" vim style word or phrase
  • :match Title /myword/ - match and highlight with "Title" vim style word or phrase
  • (*) w(rite) to buffer/overwrite file and file modified timestamp. x will exit and not save or update timestamp if unless there are unsaved changes.
  • :up updates buffer and timestamp only if unsaved changes.
  • :q - quit (fails if there are unsaved changes)
  • :q! or ZQ - quit and throw away unsaved changes
  • :wqa - write (save) and quit on all tabs

Working with multiple files

  • :e file - edit a file in a new buffer
  • :bnext or :bn - go to the next buffer
  • :bprev or :bp - go to the previous buffer
  • :bd - delete a buffer (close a file)
  • :ls - list all open buffers
  • :sp file - open a file in a new buffer and split window
  • :vsp file - open a file in a new buffer and vertically split window
  • Ctrl + ws - split window
  • Ctrl + ww - switch windows
  • Ctrl + wq - quit a window
  • Ctrl + wv - split window vertically
  • Ctrl + wh - move cursor to the left window (vertical split)
  • Ctrl + wl - move cursor to the right window (vertical split)
  • Ctrl + wj - move cursor to the window below (horizontal split)
  • Ctrl + wk - move cursor to the window above (horizontal split)

Tabs

  • :tabnew or :tabnew file - open a file in a new tab
  • Ctrl + wT - move the current split window into its own tab
  • gt or :tabnext or :tabn - move to the next tab
  • gT or :tabprev or :tabp - move to the previous tab
  • #gt - move to tab number #
  • :tabmove # - move current tab to the #th position (indexed from 0)
  • :tabclose or :tabc - close the current tab and all its windows
  • :tabonly or :tabo - close all tabs except for the current one
  • :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

Other Cursor Navigation

  • Ctrl + o - jump back a visited point
  • Ctrl + i - jump forward a visited point
  • Ctrl + ] - jump to tag of cursored word (ex. help doc)
  • z + Enter - move display of current line to top
  • 5gg - ex. goto line 5
  • :5 - goto line 5 in line mode
  • :$ - goto end of file in line mode

VISUAL mode + Copy/Paste tips

  • Shift + v - will highlight/select line, in which you can then d, y, p, etc in VISUAL MODE
  • line nr. + G (go) + V + line nr. 2 + G - this will select first line and continue to another line number; select a block or adjacent range of lines in VISUAL MODE. Alternative simple notation normal mode-style: 232GV255G
  • 55Gy65G - ex. in simple normal mode notation, this goes to line 55, yanks, to line 65
  • Ctrl + Shift + c - X system clipboard copy in INSERT MODE
  • Ctrl + Shift + v - X system clipboard paste in INSERT MODE

Search-and-replace, Global command

  • :%s/pattern-to-replace/substitute-pattern/g - (%s) search-and-replace all instances
  • :%s/ .. /gc - (%s) search-and-replace all instances with confirmation
  • :13,17s/ .. /g - search-and-replace all instances lines 13 - 17
  • :13,17s/ .. /gi - search-and-replace all instances lines 13 - 17, case insensitive
  • :global or :g - run arbitrary :ex commands on every line that matches a regex
  • NOTE: This acts on the specified [range] (default whole file), by executing the Ex command cmd for each line [in sequence, overwriting the last, ex. simple delete { pattern/d } stores only last instance] and matching pattern (an Ex command is one starting with a colon such as :d for delete). Before executing cmd, "." is set to the current line.
  • :g/sometextpattern/d - ex. (get) all lines matching pattern, then delete them
  • :g/sometextpattern/ .+5 d - ex. :global has its own address/range prefix, which will be relative to the matched line. This deletes the 5th line after "sometextpattern"
  • :g!/sometextpattern/ s/orange/blue/ - ex. on all lines NOT (!) containing sometextpattern, replace all instances of orange with blue
  • :g/sometextpattern/d - ex. (get) all lines matching pattern, then delete them

43 Registers

  • :reg - list registers with storage
  • :"" - unnamed, last used register
  • :"5 - ex. one the numbered registers, cascading 9 being oldest used
  • :"f - ex. f register, of a-z alpha registers available
  • Ctrl + r + = - INSERT MODE, initiate expression register function or even system command. Places = sign in command line. (a) This mode you type an expression, ex. 2+2 [Enter], which will output calculation. (b) This mode you can also output system commands, ex. system('ls') [Enter], which will output the ls command from your current terminal location.
  • "= - the expression register
  • "/ - the search register (last searched)
  • ". - the read-only insert register (last inserted text)
  • "% - the read-only filepath register (file's path)
  • ": - the read-only ex register (last executed command)
  • "# - the alternate-file register (last edited, see :h alternate-file)

Yak-ish Register Snips

  • :reg 5 8 a b - ex. display contents of registers 5, 8, a, and b in command mode
  • "cy - ex. copy to c register
  • "Cy - ex. append to c register (using cap)
  • "5p - ex. paste from register 5 to the document line at cursor
  • :g/sometextpattern/y B - gets all instances of lines with sometextpattern, yanks them and appends to register b. B must follow separately rather than yb.
  • "bp - vim v7.4: unnamed register seems not to store multiple lines for global commands as in previous example, however yanking or cutting to an alpha register does store them. Thus paste from the chosen alpha register.
  • :g/pattern/t$ - copy all lines matching pattern to end of file
  • :g/pattern/m$ - move all matching lines to end of file

Other useful commands

  • Ctrl + G - display file info in bottom gutter
  • . - repeat last command
  • "up" - on Command line, you cycle through last used
  • $ - represents "to end" in any command
  • << or >> - move indent of text (shifting blocks) in normal mode
  • :150,198d - delete lines in a specific range by line number in command mode
  • :d20j - delete current line + 20 lines forward (down) in command mode
  • :d20k - delete current line + 20 lines backward (upwards) in command mode
  • :.,.+55d - delete lines in a range of 55 lines beginning with the current in command mode
  • d$ / d0 - del to end of line / del to beginning of line
  • :1,.d / :.,$d - del from line 1 to cursor position / del cursor to end of file

Useful vimrc configs

  • set clipboard=unnamedplus - (linux - vim 7.3.74+): enable yank, delete to unnamed clipboard
  • set pastetoggle=<F3> - keymap toggle paste mode
  • set number - enable line numbers
  • nmap <f2> :set number! number?<cr> - keymap toggle line numbers
  • set columns=80 - set # of columns
  • setlocal cm=blowfish2 - (for :X) set native encryption method as blowfish2
  • colorscheme yak - set theme, depends on mytheme.vim in .vim\/colors

calendar.vim (plug-in)

  • github
  • :Calendar or :Cal or :C - open Calendar
  • optional window parameters for :C -split=vertical -width=27 -split=horizontal -height=12
  • optional positioning parameters for :C -position={below/tab/here/left/right/topleft/topright}
  • extra parameters for :C -view=clock -view=year
  • Example: :C -split=horizontal -height=15 -position=below -view 2019 3
  • < or > - cycle through views
  • CTRL + ww - switch between windows
  • E - toggle Event entry (on a date)
  • EVENTS (cont.): i insert new event; typical edit commands
  • T - toggle Tasklist; i insert new/edit event; o new event below; etc
  • TASKLIST (cont.): dd or D marks done; L clears done tasks
  • q - quit/close calendar

line folding

  • :8,16 fold / :8,16 o - commandline mode fold / open lines 8 -16
  • zf#j - creates a fold from the cursor down # lines
  • zf/string - creates a fold from the cursor to string
  • zj - moves the cursor to the next fold
  • zk - moves the cursor to the previous fold
  • zo - opens a fold at the cursor
  • z0 - opens all folds at the cursor
  • zm - increases the foldlevel by one
  • zM - closes all open folds
  • zr - decreases the foldlevel by one
  • zR - decreases the foldlevel to zero -- all folds will be open
  • zd - deletes the fold at the cursor
  • zE - deletes all folds
  • [z - move to start of open fold
  • ]z - move to end of open fold
  • :mkview - after :w will save folds for next session
  • :loadview - will restore the folds on next session
  • .vimrc config for permanent fold retention:
    " remember folds
    augroup remember_folds
    autocmd!
    autocmd BufWinLeave * mkview
    autocmd BufWinEnter * silent! loadview
    augroup END

blockquotes.vim

  • github
  • alt style
  • :Bq - blockquote the line at cursor
  • :8,16 Bq - blockquote the range, ex. lines 8 through 16
  • :Bq sometext - blockquotes with a special title header
  • :UBq - remove blockquote, cursor positioned at first line of quote style