If nano is a beginner-friendly text editor, then vim is the professionals' editor. vim is known for its speed and power, but also for its reputation of being difficult to learn. Nevertheless, understanding the basics of vim is invaluable, as this editor is available on almost every Linux system and server, even in environments without nano. This chapter provides a brief introduction to vim: how to open it, its working modes, basic editing commands, as well as how to save and exit. The goal is simply to keep us from getting lost when forced to use it.
20.1 Why is vim Important to Know?
vim stands for Vi Improved, the successor to the vi editor born in the UNIX era. One important point needs to be clarified: what actually comes out of the box in almost all Linux distributions, including Ubuntu 26.04 LTS and Fedora Workstation 44, is only a minimal version of this editor (in Ubuntu it is named the vim-tiny package, in Fedora it is named vim-minimal), which provides the vi command without features like syntax highlighting. The full version, which is vim itself, generally needs to be installed separately. Even so, because its minimal version is always available, we will still encounter a vi-style editor on almost every Linux server in the world. A Sysadmin who has to edit configuration files on a minimal or remote system will most likely find vi as the only available editor, not nano.
The reputation of vim being hard to learn stems from its mode-based operation. We cannot directly type text like in a regular editor. However, once accustomed to it, vim allows us to edit text extremely fast without lifting our hands from the keyboard. For beginners, it is enough to understand three basic concepts: modes, navigation, and how to exit.
To get the full version of vim along with vimtutor, install it first using the package manager.
# on Ubuntu 26.04 LTS
sudo apt install vim
# on Fedora Workstation 44
sudo dnf install vim-enhancedIt should be noted that in Fedora the package name is vim-enhanced, not vim. Fedora does not have a package named vim; the command dnf install vim will fail because the package does not exist in the repository.
20.2 Modes in vim
vim has several modes, but the following three modes are the most important to understand.
20.2.1 Normal Mode
When vim is first opened, we are in Normal Mode. In this mode, the keys we press do not type text; instead, they act as commands. For example, pressing h, j, k, or l will move the cursor. Normal Mode is the primary mode for navigation and commands.
20.2.2 Insert Mode
To type text, we must switch to Insert Mode. The way to do this is by pressing i from Normal Mode. Once in Insert Mode, we can type just like in a regular editor. To return to Normal Mode, press the Esc key.
20.2.3 Command Mode
Command Mode is used to execute commands such as saving and exiting. From Normal Mode, press : to enter Command Mode. The cursor will move to the very bottom line, where we type our command.
20.3 Opening, Editing, Saving, and Exiting
To open or create a file, run the vim command followed by the filename.
vim notes.txtAfter vim opens, press i to enter Insert Mode, then type the text you want to write. After finishing typing, press Esc to return to Normal Mode, then press : to enter Command Mode. Here are the most important commands we need to remember.
| Command | Function |
|---|---|
:w | Save the file (write) |
:q | Exit vim (quit) |
:wq | Save and exit |
:q! | Exit without saving |
As an example, after finishing editing, press Esc, then type :wq and press Enter to save and exit. If we made incorrect changes and want to exit without saving, use :q!.
Just like nano, to edit system files that require administrator privileges, open vim with sudo in front of it.
sudo vim /etc/hostsIf we accidentally opened a system file without sudo and only realized it after finishing editing, we do not need to start over from scratch. From Command Mode, run the following command to save the file with root privileges.
:w !sudo tee %After that, press Enter, then exit vim without saving again using :q! because the file on disk has already been saved via the tee command above.
20.4 Basic Navigation Commands
Navigation in Normal Mode can be done using the arrow keys, but vim users usually use the h, j, k, and l keys due to their proximity to the fingers on the keyboard.
| Key | Function |
|---|---|
h | Move cursor left |
j | Move cursor down |
k | Move cursor up |
l | Move cursor right |
0 | Move to the beginning of the line |
$ | Move to the end of the line |
gg | Move to the beginning of the file |
G | Move to the end of the file |
w | Jump to the start of the next word |
b | Jump to the start of the previous word |
x | Delete character under cursor |
dd | Delete an entire line (and copy it) |
yy | Copy an entire line (yank) |
p | Paste the deleted/copied line or text |
u | Undo |
Ctrl+r | Redo |
In addition, we can search for text by typing /word and pressing Enter. Press n to jump to the next occurrence and N to return to the previous occurrence.
Almost all commands in the table above can be preceded by a number to repeat them multiple times at once. For example, 3dd deletes three lines at once, and 5j moves the cursor five lines down. This habit is one of the main reasons why proficient vim users can edit text much faster than using standard arrow keys.
20.5 Tip: vimtutor for Self-Practice
The best way to learn vim is to practice directly. Fortunately, vim provides an interactive tutorial called vimtutor. This tutorial opens a practice file that guides us step-by-step through practicing each command.
vimtutorRun the command above in the terminal, then follow the instructions displayed on the screen. vimtutor can usually be completed in less than an hour and serves as the best starting step before diving deeper into vim.
vim might feel strange and daunting at first, but we do not need to fully master it right now. Simply understanding the modes, navigation, and how to save and exit will make us capable of surviving when forced to edit files on a system that only provides vi or vim. For daily needs, we can continue using nano, while this basic understanding of vim becomes a valuable asset for the future.
This chapter has only touched upon the three most basic modes. Once comfortable with all three, the next step worth exploring is Visual Mode (activated with the v key), which makes it easier to select blocks of text before copying, deleting, or moving them. If you ever forget a command, complete vim documentation is always available directly inside the editor via the :help command, as discussed in Chapter 17.

