Linux Basics #5 - Vim

Last Edited: 12/17/2024

This blog post introduces the very basics of Vim.

DevOps

In the second article on Linux, Linux Basics #2 - Commands, we learned some commands to work with text files, but none of them included commands to edit files. Vim, or "Vi improved," is a CLI text editor that we can use to create and edit text files. In this article, we will learn the basics of Vim.

Getting Started

In many Linux distributions, Vim is often preinstalled, but in our case, we need to install Vim using apt. To install Vim, we first need to update the package source list with apt update. Then, we can simply run apt install vim. To start Vim, type vim <filename>. If there is no file with the specified filename, it will create a new file. Otherwise, it will open the existing file. Create a new file called test.txt at the root directory with vim test.txt. You should now see an empty text editor.

Insert Mode

In Vim, there are four modes: normal mode, insert mode, visual mode, and command mode. The normal mode is for navigating through the file set by default, the insert mode is for editing, visual mode is for selecting, copying and pasting, and command mode is, well, for running commands. To switch to insert mode, press i. You should see --INSERT-- at the bottom left of the screen. Write Hello World! in the first line of the text file to test the insert mode.

Text Editing
CommandDescription
iInsert text before the cursor.
aAppend text after the cursor.
AAppend text at the end of the line.
oInsert text on below the current line.
OInsert text above the current line.

i is not the only way to switch to insert mode. In fact, i inserts text before the cursor. The table above shows other ways to switch to insert mode. While it’s helpful to remember all of them, beginners can stick to i for now. To return to normal mode, press the Escape key (Esc).

Normal & Visual Modes

To switch to visual mode, press v while in normal mode. In both normal and visual modes, you can navigate the file using motions—key combinations that move the cursor.

Vim Motions
CommandDescription
hMove the cursor to the left.
jMove the cursor down.
kMove the cursor up.
lMove the cursor to the right.
wMove the cursor to the next word.
bMove the cursor to the previous word.

There are many other Vim motions, but I recommend sticking to the above ones first to get used to Vim. The below is the command you can use to edit a file in visual mode.

Vim Editing
CommandDescription
y"yank" or copy the selected text.
yyCopy the current line.
xCut the selected text.
ddDelete the current line.
pPaste the copied or deleted text.
uUndo the previous action. (Normal Mode)
rRedo the previous action. (Normal Mode)

One key point to note is that deleted text also goes to the same buffer as copied text. This means if you delete some text and then paste, the deleted text will be pasted. Using the motions and commands, try copying and pasting Hello World! to make it Hello World!Hello World!.

Command, Count, Motion

You can use motions and commands both individually and together. For example, you can move 5 lines down from your current line with 5j. To delete 2 words, you can use d2w. The combination always follows the order: command → count → motion. Mastering this approach allows you to edit files far more efficiently than using a mouse.

Command Mode

The command mode is used for saving and exiting files in a specific way, and you can typically enter this mode by typing :. The following are the commands associated with command mode:

Command Mode
CommandDescription
:wWrite (save) the file.
:qQuit Vim.
:wqWrite (save) and quit Vim. (You can also achieve this with ZZ.)
:q!Force quit Vim without saving.

Using the commands above, try saving the file and quitting Vim. Congratulations! You’ve now learned the basics of Vim!

Conclusion

Vim requires practice to become efficient, but once you get used to it, you can boost your coding speed and minimize mouse usage. Vim commands and motions are also useful in other IDEs, such as VSCode, where you can activate them using extensions. Since developers often work closely with terminals, I highly recommend practicing Vim. Once you’re comfortable with the basics, check out the video series Vim As Your Editor by ThePrimeagen to take your skills to the next level.

Resources