Absolute Essentials You Need to Know to Survive Vi Editor
Vi is on almost every Unix and Linux distribution, so why not take advantage of it?
VI, pronounced as distinct letters /หviหหaษช/ it’s a terminal-based text editor. One of the most common tools in Unix, VI is extremely powerful for text manipulation. Although it could be a little bit challenging. And that’s why I am listing the absolute basics of the Vi editor commands in this article.
Why you should learn Vi?
Here are five reasons why I recommend learning Vi and Vim:
- Vi/Vim is free and open source. And remember this is it’s foss!!
- Vi is always available since it’s required by POSIX.
- Vi/Vim is well documented. And it also has its own user manual; you only need to type
:h
in command mode. I’ll discuss command mode later in this guide. - Vi/Vim has a lot of plugins. Vim Awesome is one of the most popular websites to download extensions.
- It does not consume a lot of system resources, and you could do a lot of tasks, even write novels in Vim.
Launch Vi
To execute the program, you must type vi:
vi
Also, you could open a file by providing its name. It will open the file for editing if it exists, or create a new one if it does not exist.
vi your_file.txt
Vi modes
You must understand that Vi has 2 different modes:
- Normal or command mode: This is the mode you use for navigating and copy-pasting
- Insert mode: This is the editing mode where you actually type text
Using Normal mode in Vi
The Normal mode is used for actions like navigation, copy, paste, delete, text substitution (not editing), etc. You always could go back to this mode by pressing <Esc>.
1. Movement commands
These are the movement keys:
h
: Left.j
: Down.k
: Up.l
: Right.
2. Deletion commands
x
: It’s like the delete key. Delete the character under the cursor.dd
: Deletes the current line.
3. Copy and paste
y
: Yarn (Copy) command. Copies the selected text.yy
: Yarn (Copy) command. Copies the current line.p
: Paste. After using a copy command, it pastes the content after the cursor.
(Command Mode)
In normal mode, you could use commands by typing :
.
For example, if you want to save your text and exit Vi, you can type:
:wq
Other common Vi commands you can use in normal/command mode:
:h
: Help:%
: Means all the lines:s
: Used for search (and substitutions):/foo/
: Regex to find items to replace:/bar/
: Regex to replace items with:u
: Undo last action:w
: Save:q
: Quit!
: Forces order
I have added :
in front of each command so that it is easier to note that you have to use :
to use the commands.
Insert mode
You could enter this mode by pressing the letter i
in Normal Mode and start typing whatever you want.
i
: Enter in Insert mode. Lets you insert, before the current cursor position.I
: Lets you insert at the beginning of the line.a
: Lets you append after the cursor.A
: Lets you append at the end of the line.
Visual Mode (only in Vim)
You could enter this mode by pressing the letter:
v
: Character modeV
: Line ModeCtrl+V
: Block mode
Learn more about the visual mode in Vim here.
A “vi” bit of history and trivia
It was developed in 1976 by Bill Joy as a visual mode of the ex line editor, also cowritten by Bill Joy.
A 2009 survey of Linux Journal readers found that vi was the most widely used text editor, beating the second Gedit, by nearly a factor of two (36% to 19%).
It was not until 2002 that VI was released as an open-source program under the BSD-style license.
Vim (VI Improved) it’s a free and open-source clone of Stevie (ST Editor for VI Enthusiasts), developed in 1991 by Bram Moolenaar. It has a huge number of extensions.
Conclusion
Among all the terminal based text editors, I prefer the Vi ecosystem.
VI/Vim is omnipresent in Unix-like operating systems due to its POSIX syntax, and when you invest a little time to unwrap its real power, you could master one of the best text-editor.
Also, you could keep growing; you could use NeoVim and its myriad of extensions and add-ons to arrive at a full IDE. The sky (and your Lua programming knowledge) is the limit.
Author Info
Jose Antonio Tenรฉs
A Communication engineer by education, and Linux user by passion. In my spare time, I play chess, do you dare?