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.
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
.vimrcfile, 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
Escto 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), oro(new line below). - Visual mode: The mode for selecting text. Press
vfor character selection,Vfor line selection, andCtrl+vfor 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:
| Key | Action |
|---|---|
h | Move left |
j | Move down |
k | Move up |
l | Move right |
w | Move to beginning of next word |
b | Move to beginning of previous word |
0 (zero) | Move to beginning of line |
$ | Move to end of line |
gg | Move to beginning of file |
G | Move to end of file |
:n | Move to line number n (e.g., :10) |
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
: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 file2then:nto go to the next file and:prevto go to the previous. - Split screen:
:splitor:vsplitfor horizontal or vertical splits. - Switch between splits:
Ctrl + wthenwto move between windows. - Quick fixes: If you make a mistake in Insert mode, press
Escto return to Normal mode and useuto undo. - Interactive learning: Run the
vimtutorcommand 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
vimtutorcommand 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
:helpinside Vim. - Read «The Linux Command Line» (available at Linux Library) – it has a full chapter dedicated to Vim.