Vim Commands Cheat Sheet - Essential Text Editing Shortcuts

Master Vim with our comprehensive cheat sheet. Learn essential commands for copying, deleting, replacing, and commenting lines to boost your text editing efficiency.

Vim Commands Cheat Sheet

This cheat sheet provides essential Vim commands to enhance your text editing productivity. Learn shortcuts for common tasks like copying, deleting, replacing, and commenting lines efficiently.

External Resources:

Copying Lines

To duplicate a line you are currently on:

yyp

To copy a block of text:

  1. Enter visual mode by pressing v.
  2. Use direction arrows to highlight the desired text.
  3. Press y to copy the highlighted selection.
  4. Move to the desired insertion point.
  5. Press p to paste.

Deleting Text

To delete the current line:

dd

To delete all text from the cursor to the end of the file:

dG

To delete all text from the cursor to the beginning of the file:

dH

To delete a specified number of lines below the cursor (e.g., 10 lines):

10dd

To delete the first N characters of every line (e.g., first 4 characters):

:%s/^.\{0,4\}//

Example:

>>> class HashTable:
...     def __init__(self):
...         self.size = 256
...         self.slots = [None for i in range(self.size)]
...         self.count = 0

After applying the command:

class HashTable:
    def __init__(self):
        self.size = 256
        self.slots = [None for i in range(self.size)]
        self.count = 0

Replacing Characters and Text

Normal Mode Function: Find and Replace

Consider the following dataset:

123. 123
233. 123

To replace the period (.) with a comma (,) on each line:

:%norm f.C,

Result:

123, 123
233, 123

Commenting Lines

  1. Enter visual line mode by pressing Shift + v.
  2. Move down using arrow keys to select the lines you want to comment.
  3. Press Shift + I to enter insert mode at the beginning of each selected line.
  4. Type the comment character (e.g., #).
  5. Press Esc to exit insert mode and apply the comment to all selected lines.

Search and Replace (e.g., replace 'true' with 'false')

:%s/true/false/g

Press Enter to execute the replacement globally on all lines.