FA

Vim Editor Tutorial

Complete Guide to the Most Powerful Text Editor

June 15, 2023  |  18 min read

Vim (Vi Improved) is one of the most powerful and popular text editors in the Linux world. Vim is installed by default on all Linux distributions and many other operating systems. Learning Vim gives you incredible speed and efficiency when editing text files, code, and configuration files. This article is designed for beginners and will introduce you to Vim from basic to intermediate concepts.

Prerequisite: You only need to be familiar with the terminal environment. It is assumed you have no prior experience with Vim.

1. What is Vim and Why Should You Learn It?

Vim is a modal text editor, meaning it works based on different modes. Unlike conventional editors that are always in insert mode, in Vim you switch between different modes to perform various tasks like typing, navigating, or selecting text. The main advantages of Vim include:

  • High speed: Using keyboard shortcuts, without needing a mouse, you can work very quickly.
  • Universal availability: Installed by default on all Linux servers, so it's essential for server administration.
  • Customizability: With the .vimrc file, you can tailor it exactly to your needs.
  • Open source and free: Completely free with a large community of users and plugins.

2. Installing Vim

On most distributions, a version of Vim (usually vi) is already installed. To install the full version, use the following commands:

# Debian / Ubuntu
sudo apt install vim

# Fedora / RHEL
sudo dnf install vim

# Arch Linux
sudo pacman -S vim

3. Vim Modes

Vim has several modes, each with a specific purpose. The most important ones are:

  • Normal mode: The default mode when you open a file. In this mode, keys are used for navigation and executing commands. Press Esc to return to Normal mode from any other mode.
  • Insert mode: The mode for typing text. Enter this mode by pressing i (insert before cursor), a (append after cursor), or o (new line below).
  • Visual mode: The mode for selecting text. Press v for character selection, V for line selection, and Ctrl+v for block selection.
  • Command-line mode: Press : from Normal mode to enter Command-line mode, where you can execute save, quit, search, and other commands.

4. Navigating in Text

In Normal mode, use the following keys to move quickly through the text:

KeyAction
hMove left
jMove down
kMove up
lMove right
wMove to beginning of next word
bMove to beginning of previous word
0 (zero)Move to beginning of line
$Move to end of line
ggMove to beginning of file
GMove to end of file
:nMove to line number n (e.g., :10)
Tip: You can prefix a number before a movement key to repeat the movement. For example, 5j means move down 5 lines.

5. Basic Editing (Delete, Copy, and Paste)

In Normal mode, these keys are used for quick text editing:

  • x : Delete character under cursor.
  • dd : Delete entire current line.
  • yy : Yank (copy) entire current line.
  • p : Paste copied text below cursor.
  • u : Undo last change.
  • Ctrl + r : Redo undone change.
  • dw : Delete from cursor to end of word.
  • d$ : Delete from cursor to end of line.

To copy or delete multiple lines, you can use Visual mode: press v to start selecting, move with navigation keys, then press y to yank or d to delete.

6. Saving and Exiting Vim

To save and exit, from Normal mode press : to enter Command-line mode, then type one of the following commands:

:w          # Save file
:q          # Quit Vim (fails if changes exist)
:wq         # Write and quit
:x          # Equivalent to :wq
:q!         # Force quit, discard changes
:w newfile  # Save file with a new name
Warning: If you have made changes and exit with :q!, all changes will be lost.

7. Searching and Replacing

In Normal mode, use / to search forward and ? to search backward:

/pattern    # Search forward for pattern
?pattern    # Search backward
n           # Go to next search result
N           # Go to previous search result

To replace text, use the following command in Command-line mode:

:%s/old/new/g     # Replace all occurrences of 'old' with 'new' in the entire file
:%s/old/new/gc    # Replace with confirmation for each
:s/old/new/       # Replace first occurrence in current line

8. Customizing Vim with .vimrc

Create a .vimrc file in your home directory to store permanent settings:

vim ~/.vimrc

Then add the following lines:

" Enable line numbers
set number

" Set tab width to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Enable syntax highlighting
syntax on

" Show matching parentheses
set showmatch

" Enable mouse support
set mouse=a

Every time you open Vim, these settings will be applied.

9. Useful Tips and Shortcuts

  • Opening multiple files: vim file1 file2 then :n to go to the next file and :prev to go to the previous.
  • Split screen: :split or :vsplit for horizontal or vertical splits.
  • Switch between splits: Ctrl + w then w to move between windows.
  • Quick fixes: If you make a mistake in Insert mode, press Esc to return to Normal mode and use u to undo.
  • Interactive learning: Run the vimtutor command in the terminal. It is an interactive step-by-step tutorial for Vim that is installed on your system.

10. Practical Examples

Example 1: Editing a Configuration File

Suppose you want to edit the /etc/hosts file (with root access):

sudo vim /etc/hosts

Then press i to enter Insert mode, make your changes, press Esc and type :wq to save and exit.

Example 2: Replacing Text in Multiple Files

Using vim and argdo, you can apply a substitution across multiple files:

vim file1.txt file2.txt
:argdo %s/old_text/new_text/g | update

Conclusion and Next Steps

In this article, you learned the basic and practical concepts of the Vim editor. Vim is a very deep editor and learning it takes time, but every minute you spend learning it will increase your productivity in the long run. To progress further:

  • Run the vimtutor command in the terminal and go through all its steps.
  • Use Vim every day to edit simple files so that the keys become second nature.
  • Browse the official documentation with :help inside Vim.
  • Read «The Linux Command Line» (available at Linux Library) – it has a full chapter dedicated to Vim.
Remember: Every Vim professional was once a beginner. With consistent practice, your speed and mastery will grow rapidly.