
This is my Vim cheatsheet with all of the most essential commands! You can check out my YouTube video for a complete guide on how almost all of these work and how I go about remembering them.
i: Enter INSERT modea: Enter INSERT mode after the cursor (append)A: Enter INSERT mode at the end of the line (Append)o: Open new line below the cursor and enter INSERT modeO: Open new line above the cursor and enter INSERT modev: Enter VISUAL modeCtrl-v: Enter VISUAL-BLOCK mode:: Enter COMMAND-LINE modeR: Enter REPLACE modeESC: Go back to NORMAL mode from other modes:w: Write (save) file without exiting:wa: Write (save) all open files without exiting:q: Quit but fail if unsaved changes exist:q!: Quit and discard unsaved changes:wq: Write (save) and quit:wqa: Write and quit on all open filesh: Move cursor left (leftmost)j: Move cursor down (looks like down arrow)k: Move cursor upl: Move cursor right (rightmost)$: Move cursor to the end of the line0: Move cursor to the beginning of the line^: Move cursor to the first non-blank character in linefx: Find the next occurrence of character ‘x’Fx: Find the previous occurrence of character ‘x’tx: Go towards the next occurrence of character ‘x’ (stops right before it)Tx: Go towards the previous occurrence of character ‘x’ (stops right before it);: Repeat the previous f, F, t, or T movement forwards,: Repeat the previous f, F, t, or T movement backwardDefinitions:
word: Sequence of letters, digits, and underscores OR sequence of other symbols, separated by whitespace. Use :h word to learn more.
WORD: Any sequence of non-blank characters (any symbols, letters, digits, etc.), separated by whitespace. Use :h WORD to learn more.
w: Move cursor forwards to start of word (sequence of letters, digits, underscores OR sequence of other symbols)
W: Move cursor forwards to start of WORD (any sequence of non-blank characters)
b: Move cursor backwards to start of word (sequence of letters, digits, underscores OR sequence of other symbols)
B: Move cursor backwards to start of WORD (any sequence of non-blank characters)
e: Move cursor forwards to end of word (sequence of letters, digits, underscores OR sequence of other symbols)
E: Move cursor forwards to end of WORD (any sequence of non-blank characters)
ge: Move cursor backward to the end of word (sequence of letters, digits, underscores OR sequence of other symbols)
gE: Move cursor backward to the end of WORD (any sequence of non-blank characters)
Definitions:
sentence: A sentence ends with a ”.”, ”!” or ”?” followed by the end of the line, a space, or tab. Use :h sentence to learn more.): Move cursor to the next sentence(: Move cursor to the previous sentenceDefinitions:
paragraph: Blocks of consecutive non-empty lines. NOTE: Line with whitespace is not empty. Use :h paragraph to learn more.}: Move cursor to the next paragraph (block of consecutive non-empty lines){: Move cursor to the previous paragraph (block of consecutive non-empty lines)Note: Replace {number} with an actual number. You can also use numbers in front of other cursor movements like {number}w, {number}b or {number} and many others.
gg: Move cursor to the first line of the documentG: Move cursor to the last line of the document{number}G: Move cursor to line {number}{number}j: Go {number} lines down{number}k: Go {number} lines upH: Move cursor to the line at the top of the windowM: Move cursor to the line at the middle of the windowL: Move cursor to the line at the bottom of the window%: Find the next parenthesis, bracket, or curly brace in front of or under the cursor and jump to its match[(: Go to the previous unmatched ([{: Go to the previous unmatched `{]): Go to the next unmatched )]}: Go to the next unmatched `}]m: Go to the next start of the method (Java-like languages)]M: Go to the next end of the method[m: Go to the previous start of the method[M: Go to the previous end of the methodCtrl-F: Move cursor forwards one full screenCtrl-B: Move cursor backward one full screenCtrl-D: Move cursor down half a screenCtrl-U: Move cursor up half a screenzz: Place the current cursor line in the middle of the windowzt: Place the current cursor line at the top of the windowzb: Place the current cursor line at the bottom of the windowCtrl-E: Scroll down a single line, leaving the cursor in placeCtrl-Y: Scroll up a single line, leaving the cursor in placeNote: Use :set ignorecase for case-insensitive searching and :set smartcase to override case insensitivity if the search pattern has uppercase characters.
/pattern: Search forward for pattern?pattern: Search backward for pattern*: Search forward for the word under or in front of the cursor#: Search backward for the word under or in front of the cursorn: Repeat the last search in the same directionN: Repeat the last search in the opposite directionTip: Use :nohl after a search to temporarily turn off search highlights until another search command is used.
Certain Vim movements that move the cursor several lines away will add entries to the jumplist. You can display the jumplist with :jump.
Common commands that will add entries to the jumplist: G, gg, [number]G, /, ?, n, N, %, (, ), {, }, :s, L, M, H. Navigating to a different file/buffer in the same window also works.
IMPORTANT: [number]j and [number]k will not add entries to the jumplist.
For a complete list of commands that add entries to the jumplist use :h jump-motions.
I use this often to navigate from and to different buffers/files in the same window.
Ctrl-O: Go to the previous cursor position in the jump listCtrl-I: Go to the next cursor position in the jump listNow that you know how to move within Vim, editing text will go hand in hand with Vim movements.
Note: A {motion} is a Vim command that moves the cursor. Like many of the commands described in the previous section.
d{motion}: Delete the text that the {motion} command moves over and copy into registerdd: Delete the whole current line and copy into registerD: Delete from under the cursor to the end of the line and copy into registerTip: You can also use a {number} before these commands to execute the deletion that {number} of times.
dw: Delete from cursor's current position to the start of the next wordde: Delete from cursor's current position to the end of the worddG: Delete from cursor's current position to the end of the filed]}: Delete from cursor's current position to the next unmatched }2dd: Delete the whole line under the cursor and the line below itThe possible combinations are endless…
u: Undo the last changeCtrl-R: Redo changes that have been undone with uTip: You can also use {number} before these undo & redo commands to execute it that {number} of times.
Note: Executing a change command with c is pretty much the same as d except that it takes you into insert mode afterward.
c{motion}: Delete the text that the {motion} command moves over, copy into register, and enter insert modecc: Delete the whole current line, copy into register, and enter insert modeC: Delete from under the cursor to the end of the line, copy into register, and enter insert modeExamples would be the same as with delete but changing c to d.
.: Repeat the last change you made to the fileTip: You can use {number} before . to repeat the change that {number} of times.
This is a really nice one!
Note: When executing, substitute {character} with an actual character.
r{character}: Replace current character under the cursor with {character}R: Enter replace mode and start replacing characters by typing until ESC is pressedx: Delete the current character under the cursor and copy into registerTip: You can use {number} before r and x to execute that {number} of times.
y{motion}: Yank or copy text that the motion command moves over into registeryy: Yank or copy the whole current line into registerY: Yank or copy from under the cursor to the end of the line into registerp: Put or paste the text found in register (register x) after the cursorP: Put or paste the text found in register (register x) before the cursorTip: You can use {number} before y or p to repeat the yank (copy) or put (paste) command that {number} of times.
Note: Commands such as d, c, and x mentioned above also copy text into a register. These as well as the y command copy into register x by default.
~: Switch the case of the character under the cursor and move the cursor to the right~{motion}: Switch the case of the text that the {motion} command moves overgu{motion}: Change the text that the {motion} command moves over to lowercaseguu: Make the whole current line lowercasegU{motion}: Change the text that the {motion} command moves over to uppercasegUU: Make the whole current line uppercase:%s/old/new/g: Replace all occurrences of “old” with “new” in the whole file:%s/old/new/gc: Replace all occurrences of “old” with “new” in the whole file, asking for confirmation:%s/old/new/gi: Replace all occurrences of “old” with “new” in the whole file, ignoring caseI personally love using this feature!
You can use Vim text objects to execute an operator on that object or select it with Visual mode (Use v to enter visual mode).
First, here’s a list of handy text objects to remember.
a": A double-quoted string, including the quotesi": A double-quoted string, excluding the quotesa': A single-quoted string, including the quotesi': A single-quoted string, excluding the quotesa( or a): A block surrounded by parentheses, including the parenthesesi( or i): A block surrounded by parentheses, excluding the parenthesesa[ or a]: A block surrounded by brackets, including the bracketsi[ or i]: A block surrounded by brackets, excluding the bracketsa{ or a}: A block surrounded by curly braces, including the curly bracesi{ or i}: A block surrounded by curly braces, excluding the curly bracesa< or a>: Text surrounded by <>, including the opening < and the closing >i< or i>: Text surrounded by <>, excluding the opening < and the closing >at: A block surrounded by XML/HTML tags, including the tagsit: A block surrounded by XML/HTML tags, excluding the tagsaw: A word including the surrounding whitespaceiw: A word excluding the surrounding whitespaceap: A paragraph including the surrounding whitespaceip: A paragraph including the surrounding whitespaceTo use text objects, place the cursor anywhere inside one and type an {operator} followed by the {text object} to execute the operator on that object.
Tip: Think of ‘a’ as around and ‘i’ as inside.
diw: Delete word that cursor is in, keeping surrounding whitespace (Think: “delete inside word”)daw: Delete word that cursor is in as well as surrounding whitespace (Think: “delete around word”)di(: Delete everything within parenthesis surrounding cursor, keeping the surrounding parenthesis (Think: “delete inside parenthesis”)da(: Delete everything within parenthesis surrounding cursor as well as the surrounding parenthesis (Think: “delete around parenthesis”)di": Delete everything within double quotes surrounding cursor, keeping the surrounding double quotes (Think: “delete inside double quotes”)da": Delete everything within double quotes surrounding cursor as well as the surrounding double quotes (Think: “delete around double quotes”)dit": Delete everything within tags surrounding cursor, keeping the surrounding tags (Think: “delete inside tags”)dat: Delete everything within tags surrounding cursor as well as the surrounding tags (Think: “delete around tags”)Of course, these examples from above can be applied to other operators like c or y for changing, copying, etc… or using v instead of an operator to select the text object in visual mode.
>{motion}: Indent text that the {motion} command moves over, to the right>>: Indent the whole current line to the rightTip: You can use {number} before > and >> to execute the indentation that {number} of times. For example, use 2>> to indent the current line and the line below it.
Tip #2: You can also use text objects with >.