Vim Fundamentals

Vim (Vi IMproved) is a further development of the text editor Vi, which appeared in 1976. It is considered to be extremely flexible and customizable, which is why Vim is very popular even as an IDE.

Characteristic for Vim are the 3 working modes: Insert mode, Command mode and Visual mode (and the "normal" mode). Each mode is designed to fulfill specific purposes.

Modes in Vim

Insert mode - The actual editing mode. In insert mode, any keyboard input will be added onto whatever text is currently in the buffer.

Command mode - In command mode, you can enter various commands to perform actions such as saving files, copying and pasting text, searching for patterns, and more.

Visual mode - In visual mode, you can select and manipulate blocks of text. There are three types of visual mode in Vim:

  • Character-wise visual mode: In this mode, you can select text character by character.
  • Line-wise visual mode: In this mode, you can select entire lines of text.
  • Block-wise visual mode: In this mode, you can select rectangular blocks of text.

Switching the modes in Vim

Starting from the normal mode switch to:

  • insert mode: press the i key
  • command mode: press the : key
  • visual mode: press the v key

If you are not in the the normal mode:

To switch from one mode (like insert or command mode) to another mode (like visual mode), you should go back to normal mode first. This is usually done with the Esc key.

If you are in the command mode, you can cancel the command line and return to normal mode witch Esc or Ctrl + c.

Command Mode

The command mode in Vim is used for executing commands and performing various operations on your document.

  • y y: Copy current line
  • d d: Cut current line
  • p: Paste the copied / cut line again
  • o: Insert a new empty line
  • : 3: Go to line 3
  • : $ Jump to the last line
  • : 1 or 1 + GJump to the first line
  • / devlabs: Search the document for the string 'devlabs'. You can browse through the results with the N or P key.
  • : w Save file
  • : q Close Vim
  • : wq Save the file and close Vim
  • : q! Suppress any warnings and close Vim

Basic Cursor Movement:

  • h: Move the cursor one character to the left.
  • j: Move the cursor one line down.
  • k: Move the cursor one line up.
  • l: Move the cursor one character to the right.

Word-wise Navigation:

  • w: Move the cursor to the beginning of the next word.
  • b: Move the cursor to the beginning of the previous word.
  • e: Move the cursor to the end of the current word.

Line Navigation:

  • 0 (zero): Move the cursor to the beginning of the current line.
  • $: Move the cursor to the end of the current line.
  • gg: Move the cursor to the first line of the document.
  • G: Move the cursor to the last line of the document.
  • 123 + gg or 123 + G: Move the cursor to a 123 line number.

Page Navigation:

  • Ctrl + f: Move the cursor forward one page (scroll down).
  • Ctrl + b: Move the cursor backward one page (scroll up).
  • Ctrl + d: Move the cursor down half a page.
  • Ctrl + u: Move the cursor up half a page.