Skip to content

Vim - Getting familiar

This chapter introduces the basics of Vim, including how to start it, understand its modes, and perform fundamental file operations. Let’s dive in and get your hands dirty with Vim.

  • Starting Vim (Terminal and GUI)
  • Understanding Vim’s Modes
  • Creating a new file
  • Viewing a file in read-only mode
  • Editing an existing file

Vim can be launched from a terminal or as a separate GUI application (like GVim or MacVim).

If you have a GUI version of Vim installed (e.g., GVim on Linux/Windows, MacVim on macOS), you can usually start it by clicking its application icon. This will open a new Vim window.

A GVim window typically looks like a standard text editor but operates with Vim’s modal editing paradigm.

The most common way to use Vim is from the terminal. This works consistently across Linux, macOS, and Windows (e.g., in Command Prompt, PowerShell, or Windows Terminal).

  1. Open your terminal application.
  2. Type vim and press Enter:
$ vim
  1. This will start Vim, often displaying a welcome screen.
  2. To quit Vim, ensure you are in Normal mode (press Esc a few times if unsure), then type :q and press Enter. If you have unsaved changes, Vim will warn you. Use :q! to quit without saving.

The command :q will appear at the bottom-left of the terminal as you type it.

Vim’s power comes from its modal nature. Different modes are used for different tasks. Here are the most important ones:

This is the default mode when Vim starts. In Normal mode, keys on the keyboard act as commands for navigation, deletion, copying, pasting, etc. You don’t type text directly here. Press Esc from any other mode to return to Normal mode.

Tip: If you’re ever unsure what mode you’re in, press Esc. It will either take you to Normal mode or do nothing if you’re already there.

This mode is for typing and editing text, similar to most other editors. To enter Insert mode from Normal mode, common commands include:

  • i (insert before cursor)
  • a (append after cursor)
  • I (insert at the beginning of the line)
  • A (append at the end of the line)
  • o (open a new line below and enter insert mode)
  • O (open a new line above and enter insert mode)

When in Insert mode, Vim usually displays -- INSERT -- at the bottom-left. Press Esc to return to Normal mode.

This mode is used for entering more complex commands, such as saving files (:w), quitting (:q), searching and replacing (:%s/old/new/g), or setting options (:set number).

To enter Command-Line mode from Normal mode, type : (colon). The cursor will move to the bottom of the screen, where you can type the command. Press Enter to execute it. Press Esc to cancel and return to Normal mode.

You can also enter this mode from Insert mode by first pressing Esc (to go to Normal mode) and then :.

Visual mode is used to select text. Once text is selected, you can perform operations on it (e.g., delete, copy, change case).

  • v (character-wise visual mode): Start selecting character by character.
  • V (linewise visual mode): Start selecting whole lines.
  • Ctrl-v (blockwise visual mode): Start selecting a rectangular block of text.

Enter Visual mode from Normal mode by pressing one of these keys. Move the cursor to expand the selection. Vim usually displays -- VISUAL --, -- VISUAL LINE --, or -- VISUAL BLOCK -- at the bottom. Press Esc to exit Visual mode.

Follow these steps to create a new file, add text, and save it:

  1. Open Vim with a filename (even if it doesn’t exist yet):
$ vim mydocument.txt
  1. Alternatively, start Vim ($ vim) and then use the edit command:
  2. :edit mydocument.txt (or :e mydocument.txt)
  3. Vim is now in Normal mode, editing mydocument.txt (which is currently empty).
  4. Press i to switch to Insert mode.
  5. Type some text, for example: Hello, Vim world!
  6. Press Esc to return to Normal mode.
  7. To save the file, type :w (for ‘write’) and press Enter. The file mydocument.txt is now saved to disk.
  8. To quit Vim, type :q and press Enter.

To open a file for viewing without the risk of accidentally modifying it, use the -R option:

$ vim -R mydocument.txt

Alternatively, you can use the view command, which is equivalent:

$ view mydocument.txt

When a file is opened in read-only mode, Vim will usually indicate this (e.g., [readonly] in the status line) and prevent writes unless you force them (e.g., with :w!).

To edit an existing file:

  1. Open the file with Vim:
$ vim mydocument.txt
  1. Navigate to where you want to make changes (using h,j,k,l or arrow keys in Normal mode).
  2. Switch to Insert mode (e.g., press i) and make your edits.
  3. Press Esc to return to Normal mode.
  4. To save changes and quit: type :wq and press Enter. (Alternatively, :x does the same if changes were made).
  5. To quit without saving changes: type :q! and press Enter.