# vim cheat sheet # Huan Chen, 2/3/2017 How to exit :q!: quit without writing :wq: write current file and exit ZZ: write current file if modified and exit ZQ: quit current file and exit, same as ":q!" Inserting text: a: append text after the cursor [cnt] times A: append text at the end of the line [count] times i: insert text before the first non-blank in the line [count] times I: insert text before the first non-blank in the line [count] times gI: insert text in column 1 [count] times o: Begin a new line below the cursor and insert text, repeat [count] times O: begin a new line above the cursor and insert text, repeat [count] times Inserting a file #:r[ead] [name]: insert the file [name] below the cursor #:r[ead] !{cmd}: execute {cmd} and insert its standard output below the cursor Deleting text or x: delete [count] characters under and after the cursor X: delete [count] characters before the cursor d{motion}: delete text that {motion} moves over dd: delete [count] lines D: delete the characters under the cursor until the end of the line {Visual}x or {Visual}d: delete the highlighted text {Visual}CTRL-H or {Visual}: delete highlighted text {Visual}X or {Visual}D: delete the highlighted lines :[range]d[elete]: delete [range] lines (default: current line) :[range]d {count}: delete {count} lines starting with [range], ex,:40d3 Undo/Redo/Repeat u: Undo [count] changes :u[ndo]: Undo one change CTRL-R: Redo [count] changes which were undone :red[o]: Redo one change which was undone U: Undo all latest changes on one line. {Vi: while not moved off it} .: Repeat the last change, with count replaced with [count] Moving around h: [count] characters to the left l: [count] characters to the right k: [count] lines upward j: [count] lines downward 0 or : to the first character of the line ^: to the first non-blank character of the line e $ or : to the end of the line and [count-1] lines downward g0 or g: when lines wrap: to the first character of the screen line g^: when lines wrap: to the first non-blank character from the screen line g$: when lines warp: to the last character of the screen line f{char}: to the [count]'th occurrence of {char} to the right, the cursor is palced on {char}. Find {char} e F{char}: to the [count]'th occurrence of {char} to the left t{char}: till before [count]'th occurrence of {char} to the right ;: repeat latest f, t, f, T [count] times ,: repeat latest f, t, f, T in opposite direction [count] times - : [count] lines upward, on the first non-blank character + or CTRL-M or : [count] lines downward, on the first non-blank character _ : [count] - 1 lines downward G: Goto line [count], default last line, on the first non-blank character gg: go the line [count], default first line, on the first non-blank character or w: [count] words forward e: forward to the end of word [count] E: forward to the end of WORD [count] b: [count] words backward B: [count] WORDS backward ge: backward to the end of word [count] gE: backward to the end of WORD [count] (: [count] sentences backward ): [count] sentences upward {: [count] paragraphs backward }: [count] paragrfphs upward z.: center the screen on the cursor zt: scroll the screen so the cursor is at the top zb: scroll the screen so the cursor is at the bottom Selecting Text (enter the visual mode) ~: switch case d: delete c: change y: yank >: shift right <: shift left !: filter through external command =: filter through 'equalprg' option command gq: format lines to 'textwidth' length v: start Visual mode per character V: start Visual mode per linewise : exit Visual mode without making any changes Searching /{pattern}[/]: search forward for the [count]'th occurrence of {pattern} /{pattern}/{offset}: search forward for the [count]'th occurrence of {pattern} and go {offset} linse up or down. Change the text CTRL-A: moves the cursor to the start of a line CTRL-E: mvoes the cursor to the end of a line Esc-B: moves to the beginning of the previous or current word CTRL-K: delete from the current cursor position to the end of the line CTRL-U: delete from the start of the line to the current cursor position CTRL-W: delete the word before the cursor c + i + ( : change inside paren d + i + ( : delete inside paren y + i + ( : yank inside paren c + a + w / d + a + w c + a + " : change all "" c + a + { v + a + " v + i + " c + a + ( : change all paren d + a + ( : delete all paren c + a + s / d + a + s: change/delete all sentence substitute until end of line: line, $s/str/str2/gc - How to indent all lines of code? gg=G