Curious Minds

Clear answers to science and history questions

Vim Cheat Sheet for Essential Commands and Navigation

· 6 min read · Science

A Vim cheat sheet is a reference list of keyboard shortcuts used to navigate and edit text without a mouse. It allows you to move, delete, and change text quickly by treating the keyboard as a control panel rather than a typewriter.

Movement Keys for Faster Navigation

Vim uses the home row for movement to keep your hands stationary. Instead of the arrow keys, you use h, j, k, and l. The h key moves the cursor one character left, j moves it one line down, k moves it one line up, and l moves it one character right. This layout prevents you from reaching for the edges of the keyboard, which reduces wrist strain over long periods of editing.

For larger jumps, you can use word-based movement. Pressing w moves the cursor to the start of the next word, while b moves it back to the start of the previous word. If you need to jump to the very end of a line, press $. To return to the start of the line, press 0. These commands allow you to traverse a line of code or text in a few keystrokes rather than holding down an arrow key.

Vertical movement is handled by the g and Ctrl keys. Pressing gg takes you to the first line of the file, and G takes you to the last line. To move to a specific line number, type the number followed by G. For example, typing 42G jumps the cursor directly to line 42. This is useful when a compiler tells you there is an error on a specific line in a large file.

Modes of Operation and Switching

The most important concept in Vim is the mode system. Vim starts in Normal mode, where keys act as commands rather than typing characters. To enter text, you must switch to Insert mode by pressing i. Once you are finished typing, you must press the Esc key to return to Normal mode. If you do not return to Normal mode, your movement commands will simply be typed into the document as text.

Visual mode is used for highlighting blocks of text. Press v to start character-based selection, or V to select entire lines. Once text is highlighted, you can perform an action on the whole block, such as deleting it with d or copying it with y. This is the primary way to handle large chunks of text without manually selecting them with a mouse.

Command-line mode is accessed by typing a colon : while in Normal mode. This mode allows you to perform file-level operations. To save your work, type :w and press Enter. To quit the editor, type :q and press Enter. To do both at once, use :wq. If you made changes you want to discard, use :q! to force the editor to close without saving.

Editing and Deleting Text

Editing in Vim is based on a grammar of verbs and nouns. The d key is the verb for delete. When combined with a movement, it deletes that specific area. For example, dw deletes from the cursor to the start of the next word. dd deletes the entire current line. This logic applies to other commands as well, such as c for change, which deletes the text and immediately puts you into Insert mode.

The x key deletes a single character under the cursor, similar to the Delete key on a standard keyboard. To delete a character before the cursor, use X. If you make a mistake, the u key undoes the last action. To redo an action you just undid, press Ctrl + r. This allows you to experiment with complex deletions knowing you can revert the file to its previous state instantly.

Copying and pasting use the terms yank and put. Press y to yank text. Like the delete command, this requires a movement; yy yanks the entire current line. To paste the yanked text below the current line, press p. To paste it above the current line, press P. A common rule of thumb is to remember that y is for copying and p is for pasting.

Search and Replace Operations

Searching for text is done in Normal mode using the forward slash / key. Type / followed by the word you are looking for and press Enter. The cursor will jump to the first occurrence. To find the next occurrence of that same word, press n. To find the previous occurrence, press N. This is significantly faster than scanning a page visually for a specific variable or keyword.

Replacing text is handled through a command-line expression. To replace the first occurrence of a word on the current line, type :s/old/new. To replace every occurrence of a word on the current line, add a global flag: :s/old/new/g. To replace every occurrence in the entire file, use the percent sign: :%s/old/new/g. This command is powerful but can be dangerous if the search term is too generic.

A common misconception is that you must use the replace command for every change. In reality, for small changes, it is faster to use the change command. For example, typing cw deletes the current word and lets you type a new one immediately. This avoids the need to enter the command-line mode for simple word swaps.

Advanced Efficiency and Customization

Numbers can be used as multipliers for almost any command in Vim. If you want to delete five lines, you do not need to press dd five times. Instead, type 5dd. If you want to move ten lines down, type 10j. This turns simple movements into precise jumps, allowing you to navigate files with mathematical accuracy.

The dot . command is one of the most useful tools for repetitive tasks. It repeats the last change you made. If you deleted a word using dw, you can move to another word and press . to delete it as well. This eliminates the need to memorize complex macros for simple, repeating edits across a document.

To customize your experience, you can create a .vimrc file in your home directory. This text file stores your preferred settings. For example, adding set number enables line numbers on the left side of the screen. Adding set hlsearch ensures that all search results are highlighted. Labels and settings may differ slightly depending on the version of Vim or the operating system you are using, so check the internal help by typing :help inside the editor.

What if I get stuck in a mode?

If the keyboard stops responding as expected, press the Esc key several times. This ensures you are back in Normal mode. If you see a message at the bottom of the screen that you cannot clear, press the Esc key and then the space bar.

How do I handle indentation?

In Normal mode, you can use the greater-than sign > to indent a line and the less-than sign < to unindent it. For blocks of text, select them in Visual mode first, then press > or <. This keeps your code aligned without requiring manual spacebar presses.

How do I insert text at the end of a line?

While i inserts text at the cursor, pressing A (uppercase) moves the cursor to the very end of the line and enters Insert mode. This is the fastest way to add a semicolon or a closing parenthesis to a line of code.

In this article
  1. Movement Keys for Faster Navigation
  2. Modes of Operation and Switching
  3. Editing and Deleting Text
  4. Search and Replace Operations
  5. Advanced Efficiency and Customization