vim:cheatsheet
This is an old revision of the document!
VIM - Cheatsheet
Insert mode - inserting/appending text
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 :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[lsearch] - remove highlighting of search matches
Search in multiple files
:vim[grep] /pattern/ {`{file}`} - search for pattern in multiple files
e.g. :vim[grep] /foo/ **/*
:cn[ext] - jump to the next match :cp[revious] - jump to the previous match :cope[n] - open a window containing the list of matches :ccl[ose] - close the quickfix window
Tabs
:tabnew or :tabnew {page.words.file} - open a file in a new tab Ctrl + wT - move the current split window into its own tab gt or :tabn[ext] - move to the next tab gT or :tabp[revious] - move to the previous tab #gt - move to tab number # :tabm[ove] # - move current tab to the #th position (indexed from 0) :tabc[lose] - close the current tab and all its windows :tabo[nly] - close all tabs except for the current one :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)
Working with multiple files
:e[dit] file - edit a file in a new buffer :bn[ext] - go to the next buffer :bp[revious] - go to the previous buffer :bd[elete] - delete a buffer (close a file) :b[uffer]# - go to a buffer by index # :b[uffer] file - go to a buffer by file :ls or :buffers - list all open buffers :sp[lit] file - open a file in a new buffer and split window :vs[plit] file - open a file in a new buffer and vertically split window :vert[ical] ba[ll] - edit all buffers as vertical windows :tab ba[ll] - edit all buffers as tabs Ctrl + ws - split window Ctrl + wv - split window vertically Ctrl + ww - switch windows Ctrl + wq - quit a window Ctrl + wx - exchange current window with next one Ctrl + w= - make all windows equal height & width 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)
Diff
zf - manually define a fold up to motion zd - delete fold under the cursor za - toggle fold under the cursor zo - open fold under the cursor zc - close fold under the cursor zr - reduce (open) all folds by one level zm - fold more (close) all folds by one level zi - toggle folding functionality ]c - jump to start of next change [c - jump to start of previous change do or :diffg[et] - obtain (get) difference (from other buffer) dp or :diffpu[t] - put difference (to other buffer) :diffthis - make current window part of diff :dif[fupdate] - update differences :diffo[ff] - switch off diff mode for current window
Tip The commands for folding (e.g. za) operate on one level. To operate on all levels, use uppercase letters (e.g. zA). Tip To view the differences of files, one can directly start Vim in diff mode by running vimdiff in a terminal. One can even set this as git difftool.
vim/cheatsheet.1613471940.txt.gz · Last modified: 2021/02/16 10:39 by peter