Vim Cheat Sheet
Movement Commands:
- '0' move to beginning of line where cursor is present
- 'w' move to start of next word
- 'b' move to beginning of current word
- 'j' move down
- 'k' move up
Insert / Append Commands:
- 'i' insert before cursor
- 'a' insert after cursor
- 'A' insert at end of line
- 'o' open a newline, below cursor
- 'ctrl-n' insert next keyword
- 'ctrl-p' insert next/previous keyword
Delete Commands:
- 'x' delete character
- 'd$' or 'D' delete from cursor to end of line
- 'dd' delete whole line
- 'ce' delete from cursor to end of word and enter Insert mode
- 'c$' delete from cursor to end of line and enter Insert mode
- 'dw' delete word
Put Commands:
- 'P' put text back after deletion or put line below cursor (using register contents
from dd)
- 'p' put line below cursor (using register contents from dd)
Paste Commands:
- ':set paste' paste pre-formatted text into vim (disable formatting)
- ':set nopaste' re-enable paste formatting
Undo / Redo Commands:
- 'u' undo previous
- 'U' undo line
- 'ctrl-r' redo
Replace Commands:
- 'r' replace (follow with char)
Status and File Position Commands:
- 'ctrl-g' show location and file status
- 'gg' move to top of file
- 'G' move to bottom of file
- '-G' move to line ‘’
- '/string' search for string (n for next, N for previous, ctrl-o return to previous position)
- '%' move from cursor highlighted (,),{,},[ or ] to matching parenthesis or bracket
Substitution Commands:
- ':s/old/new' substitute old word with new. Use :s/old/new/g for global substitution (one line).
- ':#,#s/old/new/g' substitute phrases between two line #'s
- ':%s/old/new/g' substitute all occurrences in the file type
- ':%s/old/new/gc' ask for confirmation each time add 'c'
Execute External Commands:
- ':![command]' execute any external command (for example browse files)
Save File Commands:
- 'SHIFT+zz' saves file and exits (same as :wq)
- ':w FILENAME' saves current as "FILENAME"
Visual Mode Commands:
- 'v' enter Visual Mode
- 'v+selection' save selection with :w FILENAME
String manipulation:
- 'u' while in visual mode, transform selection to lower case
- 'U' while in visual mode, transform selection to upper case
Folding:
- 'zfj' create a fold from cursor ‘’ lines down
- 'zj' move cursor to next fold
- 'zk' move cursor to previous fold
- 'zo' open one fold under cursor
- 'zc' close one fold under cursor
- 'zn' open all folds
- 'zN' close all folds
Marks:
- 'm' set mark to char
- ''' move cursor to beginning of line mark ‘’
- "`" move cursor to exact mark ‘’ (note backtick)
Macros:
- 'qw' start recording macro using register ‘w’
- 'q' stop recording macro
- '@w' execute macro in register ‘w’
- '@w' execute macro in register ‘w’ ‘’ times
- '@@' execute previously executed macro
- '@@' execute previously executed macro ‘’ times
Productivity Tips:
- visual aids
- ':set cursorline' highlight line containing cursor.
- search current word (both cycle)
- '*' search next instance of current word
- '#' search previous instance of current word
General Tips:
- save a file that needs root permission (must have sudo privileges)
- ':w !sudo tee %'
- same as above but without echo to stout
- ':w !sudo tee > /dev/null %'
- dealing with whitespaces
- search and show all whitespaces
- ':/\s'
- deleting trailing whitespaces
- ':%s/\s\+$//'