Vim
Personal VIM cheatsheet.
Useful Websites
Commands
q/- opens search history windowq:- opens command history window:g!/dont/d- delete all lines not containing the stringdontg- is the global commandd- stands for delete!- inverts the matching
:g/delete-me/d- delete all lines containing the stringdelete-me:!command- executes an external command.:ror:read- read command:r FILENAME- retrieves disk file FILENAME and puts it below the cursor position.:r !ls- reads the output of the dir command and puts it below the cursor position.
R- replace mode. Like insert mode but deletes existing characters- When typing a
:command, pressCtrl-dto see possible completions. Press<TAB>to use one completion. q/- opens a command window with history of searchesq:- opens a command window with history of Ex commands2,$!sort -k2- executesortcmd in terminal on lines2-endwith-k2arg to sort by items in second columnedit %:h- edit active file directory
Copy & move
:copy or :co or :t
:move or :m
:copy $- copy current line to the end:t6- copy current line below line 6:6t.- copy line 6 below current line:m$- move current line at the end of the file
Normal cmd
:.,$normal A;- append;at the end of each line from the current one till the last one
Under the hood
:{start},{end}{command}.- current line$- last line1- first line0- zero line (used when you move test to beginning for example:move 0)%- all lines in the file<- start of visual selection>- end of visual selection
:1,10d- delete lines 1-10:.,$copy- copy from current line.till the end$
Substitute cmds
:s is just shortcut for :substitute
:s/old/new- to substitutenewfor the firstoldin a line:s/old/new/g- to substitutenewfor allold'son a line:#,#s/old/new/g- to substitute phrases between two line #'s:%s/old/new/g- to substitute all occurrences in the file:%s/old/new/gc- to ask for confirmation each time addc
Project-wide search and replace
/Rg "old" # will populate quick-fix list
:cfdo %s/old/new/gc # will ask you if you want to replace 'old' with 'new' for each occurence in quick-fix list
:wa #write all to disk
Search
/\ccase insensitive search- use\cswitch/\Ccase sensitive search- use\Cswitchg*search for word under cursor with partial word matching
Text editing
dawdeletes the whole word wherever the cursor isdipdeleteinsideparagraphdasdeletearoundsentencedi{deleteinside{curly bracesvipvisual selectinsideparagraphcipchangeinsideparagraphgU- change text to UPPERCASEgu- change text to lowercase
Navigation
Ctrl+o- backCtrl+i- forward:jumps- displays jumps for current window
<number> + Ggo to lineCtrl+gshows info about current position within the file in the bottom lineH- Highest line on the screenM- Middle line on the screenL- Lowest line on the screenzt- scroll view so that cursor is aligned to thetopzz- scroll view so thet cursor is aligned in the middlezb- scroll view so that cursor is aligned to thebottom
Automatic Marks
"position before the last jump within current file'.location of last change'^location of last insertion'[start of last change or yank']end of last change or yank'<start of last visual selection'>end of last visual selection
Tabs
gt(or:tabn) - to go to next tabgT(or:tabpor:tabN) - to go to previous tab#gt(or:tabn #) - to go to #th tab
Motions
g;- go to most recent modification in the document- press again and you go to most recent - 1
`.go to most recent modification`^go to last insertion0go to the start of the line^go to the first character on the line
Spell checking
:setlocal spellenable spell checking:set nospelldisable spell check]smove to next misspelled word[smove to previous misspeled wordzgmark word as a good wordzGmark good just for current instance (will be lost after restart)zwmark word as wrong wordzWmark word as wrong word for current instancez=suggest correction
Netrw explorer
-go one level up in direcotry tree:Vexvertical split:Sexhorizontal split%create new fileRrename fileDdelete filedcreate new dirichange view
Registers (a.k.a. clipboard)
"{register-name}{y|p|d}"byycopy tobregister"bppaster frombregister"bdddelete tobregisteroregister holds what wasyanked most recently- lowercase letter overwrites register, uppercase appends to it
"_black hole register - a place from which nothing returns*register references system clipboard ("*p,"*y)
Buffers
:buffersor:lsto list all open buffers:ballopens all buffers in windows:bdclose current buffer (buffer delete):bnextor:bn:bprevor:bp%bddelete all unsaved buffers (it will refuse to delete unsaved ones)
Windows
Ctrl+w =resize windows equallyCtrl+w 10 >change width to left by 10Ctrl+w 10 <change width to right by 10Ctrl+w 10 +incerease height by 10Ctrl+w 10 -decrease height by 10:res[ize] +10increase window height/width by 10:res[ize] -10decrease window height/width by 10z10set height/width to 10:only[!]close all other windows except this one (add ! to close even unsaved ones)
Splits
Ctrl+wfollowed by capitalH,J,KorLwill move the current window to the far left, bottom, top or right respectively like normal cursor navigationCtrl+wfollowed byShift+Twill move split to new tab
Insert mode
Ctrl+wdelete back one wordCtrl+udelete all entered characters before the cursorCtrl+hdelete one character (backspace)Ctrl+oenters Insert Normal Mode -> You can execute one command (e.g.:zz) and then return back to Insert mode
Visual mode
Ctrl+vblock-wise visual modegvreselect the last visual selectionogo to the other end of visual selection
Folding
Collapsing multiple lines of text into single line
zf{motion}- fold (e.g.:zf{folds inside{}brackets)za- toggle foldzR- open all folds in current bufferzM- close all folds in current bufferzc- close foldzo- open foldzd- delete fold under cursor (not text, just fold)
Macros
q{a-z}to record@{a-z}to replay@@to replay the most recently replayed macro
External commands
:%!<command>- runs external command with current buffer content:%!sortwill runsortwith current buffer