Skip to content

Instantly share code, notes, and snippets.

@basperheim
Last active May 11, 2024 17:32
Show Gist options
  • Save basperheim/20d6b404f2404ada05567a32022f9b8b to your computer and use it in GitHub Desktop.
Save basperheim/20d6b404f2404ada05567a32022f9b8b to your computer and use it in GitHub Desktop.

VIM Cheat Sheet

Navigation

  • h: Move cursor left.
  • j: Move cursor down.
  • k: Move cursor up.
  • l: Move cursor right.
  • 0 or ^: Move cursor to the beginning of the line.
  • $: Move cursor to the end of the line.
  • gg: Move cursor to the beginning of the file.
  • G: Move cursor to the end of the file.
  • :n: Move cursor to line number 'n'.

Editing

  • i: Insert mode before the cursor.
  • a: Insert mode after the cursor.
  • I: Insert mode at the beginning of the line.
  • A: Insert mode at the end of the line.
  • o: Open a new line below the current line.
  • O: Open a new line above the current line.
  • x: Delete character under the cursor.
  • dd: Delete the current line.
  • yy: Yank (copy) the current line.
  • p: Paste after the cursor.
  • P: Paste before the cursor.
  • u: Undo last change.
  • Ctrl + r: Redo last undo.

Visual Mode

v: Enter visual mode. V: Enter visual line mode. Ctrl + v: Enter visual block mode.

Searching

/pattern: Search forward for 'pattern'. ?pattern: Search backward for 'pattern'. n: Move to the next match. N: Move to the previous match.

Saving and Quitting

:w: Save changes. :q: Quit Vim. :q!: Quit Vim without saving (force quit). :wq or :x: Save changes and quit.

Miscellaneous

.: Repeat the last change. Ctrl + g: Show current file name and status. Ctrl + c: Abort the current command. Ctrl + o: Switch to normal mode for one command, then return to previous mode. Ctrl + w: Window commands (e.g., Ctrl + w, w to switch between windows).

Clear the contents of a file with VIM

In Vim, you can clear a file by deleting its content. Here's how you can achieve it:

Open the file in Vim.

Press gg to move the cursor to the beginning of the file. Press dG to delete all lines from the current cursor position to the end of the file. This sequence will effectively clear the contents of the file.

To select multiple lines to indent them, you can use Vim's visual mode. Here's how:

  • Move the cursor to the first line you want to indent.
  • Press V (uppercase V) to enter visual line mode. This will select the entire line.
  • Move the cursor down (using arrow keys or j).
  • Once you've selected all the lines you want to indent, press > to indent them.
  • Press Esc to exit visual mode.

Alternatively, you can also use visual block mode for more precise indentation:

  • Move the cursor to the first character of the first line you want to indent.
  • Press Ctrl + V to enter visual block mode.
  • Move the cursor down (using arrow keys or j) to select the block of text you want to indent.
  • Press > to indent the selected block.
  • Press Esc to exit visual block mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment