Skip to content

Instantly share code, notes, and snippets.

@ehsan18t
Last active May 20, 2025 09:05
Show Gist options
  • Save ehsan18t/43d6b1bff474f0de8d164697b506f684 to your computer and use it in GitHub Desktop.
Save ehsan18t/43d6b1bff474f0de8d164697b506f684 to your computer and use it in GitHub Desktop.

Vim Cheatsheet for Beginners

These commands are tested in VSCode with Vim plugin.

 

1. Vim Philosophy & Essential Modes

Vim is a modal editor, meaning its behavior changes based on the current mode—no mouse required!

Modes Overview

Mode Enter Exit Purpose
Normal Esc / Ctrl+[ Navigation & commands (default)
Insert i, a, o Esc Typing text
Visual v, V, Ctrl+V Esc Text selection
Replace R Esc Overwrite existing text
Command : Enter Ex commands (:w, :q, etc.)

Mode Context Examples:

  • Normal mode: dd deletes a line, but in Insert mode types "dd"
  • Visual mode: select text with movement keys, then operate on selection
  • Replace mode: typing replaces existing characters (useful for tables)

2. Basic Navigation

2.1 Character & Word Motions

  • h/j/k/l: ← ↓ ↑ →
  • w: jump to start of next word
  • b: jump to start of previous word
  • e: move to end of current word
  • ge: move to end of previous word
  • * / #: search forward/backward for word under cursor
  • W/B/E: same as w/b/e but for WORDS (space-separated)

Examples:

  • In hello_world with cursor at h, w jumps to _, but W jumps to next space

2.2 Line & File Navigation

  • 0 / ^ / $: line start / first non-blank / line end
  • gg / G: first line / last line
  • :n / nG: go to line n (e.g. :5 or 10G)
  • %: jump to matching bracket ((), {}, [])
  • { / }: jump to previous/next paragraph
  • H / M / L: jump to Home/Middle/Last line of visible screen

Examples:

  • In function() {, pressing ^ from start jumps to f
  • With cursor on {, press % to jump to matching }

2.3 Screen Scrolling & Positioning

  • Ctrl+u / Ctrl+d: scroll up/down half-page
  • Ctrl+b / Ctrl+f: scroll up/down full-page
  • zt / zz / zb: move current line to top / center / bottom of screen

2.4 Jump to Character

  • f{c} / F{c}: forward/backward to {c} on line
  • t{c} / T{c}: forward/backward until before {c}
  • ; / ,: repeat last f/t forward/backward

Examples:

  • In function(arg), with cursor at f, type f( to jump to (
  • Then ; to jump to next ( if any on the line

2.5 Jump List Navigation

  • Ctrl+o / Ctrl+i: jump backward/forward through positions
  • :jumps: display jump list

2.6 Advanced Navigation

  • g; / g,: jump to the last change (backward/forward)
  • ]m / [m: jump to the start of the next/previous method or function
  • ] / [: jump to the next/previous unmatched [{ or ]}
  • :help motion.txt: explore all motion commands in Vim's help system

Examples:

  • After editing multiple lines, use g; to revisit the last change.
  • In a code file, use ]m to jump to the next function definition.

3. Editing Basics

3.1 Inserting

  • i / I: insert before cursor / at first non-blank
  • a / A: insert after cursor / at end of line
  • o / O: open new line below / above
  • gi: resume insert at last insertion point

Examples:

  • A + type ; + Esc: add semicolon at end of line
  • O + type // Comment + Esc: add comment above current line

3.2 Deleting

  • x / X: delete character under/before cursor
  • dw / db: delete to start of next/previous word
  • dd / D: delete entire line / to end of line
  • d{motion}: delete text covered by {motion}
  • di{ / da{: delete inside/around curly braces (works with ()[]<>"')

Examples:

  • d$: delete from cursor to end of line
  • d2j: delete current line and 2 lines below
  • dit: delete inside HTML/XML tag

3.3 Yanking & Pasting

  • y{motion} / yy or Y: yank text or line
  • p / P: paste after / before cursor
  • "ay{motion} / "ap: yank to register 'a' / paste from register 'a'
  • :reg: view all registers
  • "0p: paste from yank register (not affected by deletes)

Examples:

  • yiw then move cursor and p: duplicate word
  • "ay3w then "ap: yank 3 words into register 'a', then paste them

3.4 Changing & Replacing

  • c{motion} / C: change text or to end of line
  • s / S: substitute char & insert / substitute line
  • r{c}: replace character under cursor with {c}
  • R: enter replace mode
  • ~: toggle case of character under cursor

Examples:

  • ct): change text until next ) (excl.)
  • cw: change word (like dw then i)
  • cc: change entire line (like dd then i)

3.5 Undo & Redo

  • u / Ctrl+r: undo / redo changes
  • U: undo all changes on line
  • .: repeat last change

4. Visual Mode & Text Objects

4.1 Entering Visual Mode

  • v / V / Ctrl+V: character / line / block selection
  • gv: reselect last visual selection

Visual Block Examples:

  • Ctrl+V → select multiple lines → I → type text → Esc: insert at beginning of multiple lines
  • Ctrl+V → select block → d: delete block
  • Ctrl+V → select block → r{c}: replace with character

4.2 Text Objects

Operate on semantic blocks using operator + text-object:

Object Inner (i) Around (a)
Word iw aw
Sentence is as
Paragraph ip ap
Quotes " i" a"
Parentheses i( a(
Tags it at

Examples:

  • ci" changes inside quotes ("hello""" with cursor between quotes)
  • dap deletes around paragraph (including trailing blank line)
  • vib visually select inside parentheses
  • ya] yank around square brackets (including the brackets)
  • dit delete inside HTML/XML tag (leaves the tags intact)

5. Search & Replace

Vim's search and replace feature is powerful and supports regular expressions.

Basic Commands:

  • /pattern / ?pattern: Search forward / backward for pattern.
  • n / N: Repeat the search forward / backward.
  • :%s/old/new/g: Replace all occurrences of old with new in the file.
  • :%s/old/new/gc: Replace all occurrences with confirmation.
  • :%s/old/new/gi: Replace all occurrences, case-insensitive.

Advanced Examples:

  • /\<word\>: Search for the exact word word (not part of another word).
  • /\d\+: Search for one or more digits.
  • /^\s*function: Search for lines starting with function (ignoring leading spaces).
  • :%s/\(\w\+\)/"\1"/g: Surround every word with quotes.
  • :g/TODO/d: Delete all lines containing "TODO".

Tip for Beginners: Use :set hlsearch to highlight search results and :noh to clear the highlights.


6. Marks & Jumps

  • ma: set mark a at cursor
  • `a / 'a: jump to exact position / line of mark a
  • `. / '.: jump to last edit position / line
  • `[ / `]: jump to start/end of last change or yank
  • `< / `>: jump to start/end of last visual selection
  • `0 through `9: jump to position when Vim was last closed

Examples:

  • ma at start of function, mz at end, then `a and `z to navigate
  • d'a: delete from current line to line marked with a
  • ya'b: yank text from mark ato markb`

7. Registers

7.1 Register Types

Registers are like "named clipboards" in Vim. They store text for yanking, deleting, or copying. Here are the main types:

  • Unnamed Register (""): Stores the last deleted or yanked text. This is the default register used when no specific register is mentioned.
  • Numbered Registers ("0 to "9):
    • "0: Stores the last yanked text.
    • "1 to "9: Store the last nine deletions, with "1 being the most recent.
  • Named Registers ("a to "z): Manually store text using "ay{motion} (e.g., "ayw yanks a word into register a).
  • System Clipboard ("+): Interact with the system clipboard. Use "+y to copy to the clipboard and "+p to paste from it.
  • Black Hole Register ("_): Discards text without saving it to any register. Useful for deleting without overwriting the unnamed register.

Examples:

  • Yank text into a named register: "ayw (yank a word into register a).
  • Paste from the system clipboard: "+p.
  • Delete without saving: "_dd (deletes the current line without affecting registers).

7.2 Register Commands

  • "{reg}y{motion}: yank into register {reg}
  • "{reg}p: paste from register {reg}
  • "+y{motion} / "+p: yank to / paste from system clipboard
  • "_d{motion}: delete without saving to register (black hole)
  • "0p: paste from yank register (not affected by d or c)
  • :reg: view register contents

Examples:

  • "ayy then "ap: copy line to register a, then paste it
  • "+yG: yank from cursor to end of file to system clipboard
  • "_dd: delete line without affecting registers (useful before pasting)

8. Macros

Macros allow you to record a sequence of commands and replay them.

Commands:

  • q{reg}: Start recording a macro into register {reg} (e.g., qa starts recording into register a).
  • q: Stop recording.
  • @{reg}: Execute the macro stored in register {reg}.
  • @@: Repeat the last executed macro.
  • {count}@{reg}: Execute the macro {count} times.

Examples:

  1. Add a prefix to multiple lines:

    • qaI// Escjq (record macro to add // at the start of a line).
    • @a to apply the macro once, or 10@a to apply it to 10 lines.
  2. Format a list:

    • qa0i- Escjq (record macro to add - at the start of a line).
    • Use @a to repeat for a list of items.

Tip for Beginners: If you make a mistake while recording, press q to stop and start over.

8.1 Practical Macro Examples

  1. Add a prefix to multiple lines:

    • qaI// Escjq
    • @a to apply once, or 10@a to apply to 10 lines.
  2. Format a list:

    • qa0i- Escjq
    • Use @a to repeat for a list of items.
  3. Swap two words:

    • qadiwePq
    • Use @a to repeat the swap.

9. Folding

Folding allows you to collapse sections of text, making it easier to navigate large files.

Commands:

  • zf{motion}: Create a fold for the specified motion (e.g., zf% folds everything between matching brackets).
  • zo / zc: Open / close a fold.
  • zr / zm: Reduce / increase the fold level for the entire file.
  • zR / zM: Open / close all folds.
  • zj / zk: Move to the next / previous fold.

Examples:

  • To fold a function:
    1. Place the cursor at the start of the function.
    2. Use zf% to fold everything between the matching brackets.
  • To open all folds: zR.
  • To close all folds: zM.

Tip for Beginners: If folding doesn't work, ensure :set foldmethod=manual or :set foldmethod=syntax is enabled.


10. Multiple Windows & Buffers

  • :split / :vsplit: horizontal / vertical split
  • Ctrl+w + h/j/k/l: navigate between windows
  • Ctrl+w + _/|: maximize height/width of current window
  • Ctrl+w + =: make all windows equal size
  • :e file: edit file in new buffer
  • :ls: list buffers
  • :bn / :bp: next / previous buffer
  • :bd: delete (close) buffer

Examples:

  • :vsplit index.jsCtrl+w l → edit in right window
  • :e in insert mode completes filenames

11. Practice Exercises

  1. Delete a word: This is a test|dwThis is a |
  2. Change inside quotes: "Learn Vim|"ci" → type Master"Master"
  3. Jump to line 5: 5G or :5
  4. Visual delete paragraph: place cursor in paragraph → vapd
  5. Search & replace: /foo:%s/foo/bar/gc
  6. Multi-line edit: Ctrl+V → select multiple lines → I → type → Esc
  7. Macro for formatting: qa0i- [ ] ^[jq10@a (adds checkboxes)
  8. Double quoted string: HelloviwS""Hello"
  9. swap two words: one twodwelptwo one
  10. Sort lines: :'<,'>sort in visual selection

12. Further Resources

  • vimtutor: built-in interactive tutorial (vimtutor)
  • Vim Adventures: gamified learning at vim-adventures.com
  • Vimcasts: free screencasts by Drew Neil for step-by-step demos (vimcasts.org)
  • vim-wiki: comprehensive wiki at vimhelp.org
  • Practical Vim by Drew Neil: excellent book for intermediate users
  • Cheat Sheet: GitHub Gist with condensed commands
  • vim-awesome.com: directory of plugins

13. VSCode Keybindings for NeoVim

// Custom Keybindings for VS Code

[
  // === Text Transformation Commands ===
  {
    "key": "ctrl+alt+u",
    "command": "editor.action.transformToUppercase",
    "when": "editorTextFocus",
    "description": "Transform selected text to uppercase."
  },
  {
    "key": "ctrl+alt+l",
    "command": "editor.action.transformToLowercase",
    "when": "editorTextFocus",
    "description": "Transform selected text to lowercase."
  },
  {
    "key": "ctrl+alt+t",
    "command": "editor.action.transformToTitlecase",
    "when": "editorTextFocus",
    "description": "Transform selected text to title case."
  },

  // === Replace Functionality ===
  {
    "key": "ctrl+h",
    "command": "editor.action.startFindReplaceAction",
    "when": "editorTextFocus && !inDebugRepl",
    "description": "Open the find and replace dialog."
  },

  // === Line Movement Commands ===
  {
    "key": "alt+k",
    "command": "editor.action.moveLinesUpAction",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Move the current line or selected lines up."
  },
  {
    "key": "alt+j",
    "command": "editor.action.moveLinesDownAction",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Move the current line or selected lines down."
  },
  {
    "key": "alt+up",
    "command": "editor.action.moveLinesUpAction",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Alternative keybinding to move lines up."
  },
  {
    "key": "alt+down",
    "command": "editor.action.moveLinesDownAction",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Alternative keybinding to move lines down."
  },

  // === Editor Navigation Commands ===
  {
    "key": "alt+h",
    "command": "workbench.action.previousEditor",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Switch to the previous editor tab."
  },
  {
    "key": "alt+l",
    "command": "workbench.action.nextEditor",
    "when": "editorTextFocus && !editorReadonly",
    "description": "Switch to the next editor tab."
  },

  // === File and Buffer Management ===
  {
    "key": "ctrl+s",
    "command": "workbench.action.files.save",
    "when": "neovim.mode == 'normal' && editorTextFocus",
    "description": "Save the current file (specific to Neovim mode)."
  },
  {
    "key": "space f",
    "command": "workbench.action.quickOpen",
    "when": "neovim.mode == 'normal' && editorTextFocus",
    "description": "Open the file finder (Space + F)."
  },
  {
    "key": "space b",
    "command": "workbench.action.showAllEditors",
    "when": "neovim.mode == 'normal' && editorTextFocus",
    "description": "Show all open buffers/tabs (Space + B)."
  },

  // === Clipboard and Selection Commands ===
  {
    "key": "ctrl+c",
    "command": "editor.action.clipboardCopyAction",
    "when": "editorTextFocus",
    "description": "Copy selected text to the clipboard."
  },
  {
    "key": "ctrl+a",
    "command": "editor.action.selectAll",
    "when": "editorTextFocus",
    "description": "Select all text in the current editor."
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment