Vim cheatsheet | navigation, editing, insert mode, visual mode

ยท 1390 words ยท 7 minute read

Vim is an efficient text editor used in terminal (CLI). Vim is mostly used by programmers.

There is a new VIM fork called NeoVIM or nvim. It is great and modern with better capabilities. All things in this cheatsheet works on vim and nvim.

Exiting Vim ๐Ÿ”—

  • :q : close file (if it is the only file in vim, vim will be exited too). (a.k.a quit vim)
  • :qa: close all files.
  • :w: save. (a.k.a write file to the disk/storage)
  • :wq or :x: save and close file.
  • ZZ: save and quit.
  • ZQ: Quit without checking changes.
  • :q!: Quit without saving any changes.

Arrow keys ๐Ÿ”—

normal arrow keys works fine, but VIM has its own arrow keys which are in near places to your fingers while you are typing.

  • h: left arrow. move one character to the left.

  • j: down arrow. move one line downward (to the bottom).

  • k: up arrow. move one line upward (to the top).

  • l: right arrow. move one character to the right.

  • <C-U> (CTRL + u): move half-page up.

  • <C-D> (CTRL + d): move half-page down.

  • <C-B> (CTRL + b): page up. move one screen height up in the current file.

  • <C-F> (CTRL + f): page down. move one screen height down in the current file.

  • b: previous word.

  • w: next word.

  • ge: previous end of word.

  • e: next end of word.

  • 0 (zero): start of line.
  • ^ (carret): start of line (after whitespace).
  • $: end of line.
  • fc: go forward to character c. For example: ff means go forward to the char f.
  • Fc (capital F small c): go backward to character c. For example: Fd means go backward to the char d.
  • gg: First line.
  • G: Last line.
  • :{number}: Go to line {number}. For example: :221 means go to line number 221 in the current document / file.
  • {number}G: Go to line {number}.
  • {number}j: Go down {number} lines.
  • {number}k: Go up {number} lines.
  • zz: Center this line.
  • zt: Top this line.
  • zb: Bottom this line.
  • H: Move to top of screen.
  • M: Move to middle of screen.
  • L: Move to bottom of screen.
  • n: Next matching search pattern.
  • N: Previous match.
  • *: Next whole word under cursor.
  • #: Previous whole word under cursor.
  • :tabedit [file]: Edit file in a new tab.
  • :tabfind [file]: Open file if exists in new tab.
  • :tabclose: Close current tab.
  • :tabs: List all tabs.
  • :tabfirst: Go to first tab.
  • :tablast: Go to last tab.
  • :tabn: Go to next tab.
  • :tabp: Go to previous tab.

Editing in Vim ๐Ÿ”—

  • a: Append.
  • A: Append from end of line.
  • i: Insert.
  • o: Next line.
  • O: Previous line.
  • s: Delete char and insert.
  • S: Delete line and insert.
  • C: Delete until end of line and insert.
  • r: Replace one character.
  • R: Enter Replace mode.
  • u: Undo changes.
  • <C-R>: Redo changes.

Visual mode in Vim ๐Ÿ”—

  • v: Enter visual mode.
  • V: Enter visual line mode.
  • <C-V> (CTRL+v): Enter visual block mode.
  • In: visual mode.
  • d or x: Delete selection.
  • s: Replace selection.
  • y: Yank selection (Copy).

See Operators โ†“ for other things you can do.

Operators ๐Ÿ”—

Usage ๐Ÿ”—

Operators let you operate in a range of text (defined by motion). These are performed in normal mode.

  • d: do the motion of w.

Operators list ๐Ÿ”—

  • d: Delete.
  • y: Yank (copy).
  • c: Change (delete then insert).
  • >: Indent right.
  • <: Indent left.
  • =: Autoindent.
  • g~: Swap case.
  • gU: Uppercase.
  • gu: Lowercase.
  • !: Filter through external program.

See :help operator.

Examples of operators usage ๐Ÿ”—

Combine operators with motions to use them.

  • dd(repeat the letter): Delete current line.
  • dw: Delete to next word.
  • db: Delete to beginning of word.
  • 2dd: Delete 2 lines.
  • dip: Delete a text object (inside paragraph).
  • (in visual mode) d: Delete selection.

See: :help motion.txt.

Text objects ๐Ÿ”—

Usage : how to use ๐Ÿ”—

Text objects let you operate (with an operator) in or around text blocks (objects).

Operator[i]nside or [a]roundText object

all text objects ๐Ÿ”—

  • p: Paragraph.
  • w: Word.
  • s: Sentence.
  • [: A [] block.
  • (: A () block.
  • {: A {} block.
  • <: A <> block.
  • ': A quoted string.
  • ": A quoted string.
  • ```: A quoted string.
  • b: A block [(.
  • B: A block in [{.
  • t: A XML tag block.

Text objects by examples ๐Ÿ”—

  • vip: Select paragraph.
  • vipipipip: Select more paragraphs.
  • yip: Yank inner paragraph. Yank means copy.
  • yap: Yank paragraph (including newline). Yank means copy.
  • dip: Delete inner paragraph.
  • cip: Change inner paragraph.

See Operators โคด for other things you can do.

Folds ๐Ÿ”—

  • zo or zO: Open.
  • zc or zC: Close.
  • za or zA: Toggle.
  • zv: Open folds for this line.
  • zM: Close all.
  • zR: Open all.
  • zm: Fold more (foldlevel += 1).
  • zr: Fold less (foldlevel -= 1).
  • zx: Update folds.

Uppercase ones are recursive (eg, zO is open recursively).

more navigation ๐Ÿ”—

  • %: Nearest/matching {[()]}. Navigate to the end of function.
  • [(: Previous (.
  • [{: Previous {.
  • [<: Previous <.
  • ]): Next ).
  • [m: Previous method start.
  • [M: Previous method end.

Jumping in Vim ๐Ÿ”—

  • <C-O> (CTRL+o): Go back to previous location.
  • <C-I> (CTRL+i): Go forward.
  • gf: Go to file in cursor.

Counters ๐Ÿ”—

  • <C-A>: Increment number.
  • <C-X>: Decrement number.

Windows ๐Ÿ”—

  • z{height}<Cr>: Resize pane to {height} lines tall.

Tags ๐Ÿ”—

  • :tag Classname: Jump to first definition of Classname.
  • <C-]>: Jump to definition.
  • g]: See all definitions.
  • <C-T>: Go back to last tag.
  • <C-O> <C-I>: Back/forward.
  • :tselect Classname: Find definitions of Classname.
  • :tjump Classname: Find definitions of Classname (auto-select 1st).

Case ๐Ÿ”—

You can do these in visual or normal mode.

  • ~: Toggle case (Case => cASE).
  • gU: Uppercase.
  • gu: Lowercase.
  • gUU: Uppercase current line (also gUgU).
  • guu: Lowercase current line (also gugu).

Misc (more) ๐Ÿ”—

  • .: Repeat last command.
  • ]p: Paste under the current indentation level.
  • :set ff=unix: Convert Windows line endings to Unix line endings.

Command line ๐Ÿ”—

  • <C-R><C-W>: Insert current word into the command line.
  • <C-R>": Paste from " register.
  • <C-X><C-F>: Auto-completion of path in insert mode.

Text alignment ๐Ÿ”—

  • :center [width]
  • :right [width]
  • :left

See :help formatting.

Calculator ๐Ÿ”—

Do this in insert mode.

  • <C-R>=128/2: Shows the result of the division : โ€˜64โ€™.

Exiting with an error ๐Ÿ”—

  • :cq
  • :cquit

Works like :qa, but throws an error. Great for aborting Git commands.

Marks ๐Ÿ”—

  • ``^`: Last position of cursor in insert mode.
  • ``.`: Last change in current buffer.
  • ``"`: Last exited current buffer.
  • ``0`: In last file edited.
  • '': Back to line in current buffer where jumped from.
  • ``: Back to position in current buffer where jumped from.
  • ``[`: To beginning of previously changed or yanked text.
  • ``]`: To end of previously changed or yanked text.
  • ``<`: To beginning of last visual selection.
  • ``>`: To end of last visual selection.
  • ma: Mark this cursor position as a.
  • ``a`: Jump to the cursor position a.
  • 'a: Jump to the beginning of the line with position a.
  • d'a: Delete from current line to line of mark a.
  • da`: Delete from current position to position of mark a.
  • c'a: Change text from current line to line of a.
  • ya`: Yank text from current position to position of a.
  • :marks: List all current marks.
  • :delm a: Delete mark a.
  • :delm a-d: Delete marks a, b, c, d.
  • :delm abc: Delete marks a, b, c.

Spell checking ๐Ÿ”—

  • :set spell spelllang=en_us: Turn on US English spell checking.
  • ]s: Move to next misspelled word after the cursor.
  • [s: Move to previous misspelled word before the cursor.
  • z=: Suggest spellings for the word under/after the cursor.
  • zg: Add word to spell list.
  • zw: Mark word as bad/mispelling.
  • zu or C-X (Insert Mode): Suggest words for bad word under cursor from spellfile.

See :help spell.

Common & Useful Examples ๐Ÿ”—

  • $_ : Delete the current line.
  • vi(: highlight/select the text within ().
  • vi": hightlight/select the text between “”.
  • ce or cw: change the word.

More resources & learning sources ๐Ÿ”—

I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .