This tutorial explains the differences and similarities between the Vi and Vim editors using straightforward examples.
Although we are using Ubuntu 20.04 for these demos, you can use its higher version or another Linux distribution.
Table of Contents
Top 5 Similarities between the Vi and Vim editors
Vi, short for Visual editor, is the most famous editor for all Linux distributions. On the other hand, Vim (Vi Improved) is a better version of the Vi editor. Vim has become so popular that certain Linux distributions come with it preinstalled.
Let’s focus on some of the editors’ similarities before finding their differences.
1. Open a file
First, you need to ensure both editors are installed.
vim --version
Otherwise, install it.
sudo apt install vim
Then confirm the installation.
vim --version
Let’s now open a file using each editor.
vi <file to open> vim <file to open>
vi file.txt vim file.txt
If the specified file exists, the editor opens it. Otherwise, it creates a new file.
If you have a problem exiting either editor, type :q
or :q!
:q
OR
:q!
If you have pressed another key and that prevents you from exiting the editor, press esc
followed by :q!
.
2. You see the same initial interface.
One of the reasons Vim’s popularity seems to overtake Vi’s is the ability to customize Vim’s interface. However, before modifying the interface, it resembles that of Vi.
3. Use the same modes.
Vi and Vim exist in states, commonly referred to as modes, to enable you
- type text into a file,
- use the mouse, or
- run commands in the file.
The primary states are the command (the normal) and insert modes. As the name suggests, the command mode enables you to run commands in a file. For example, we just run two commands, :q
and :q!
in the first example.
The insert mode, also known as write mode, lets you type text in the editor. You can switch from the command mode to the insert mode using the following letters.
i
start typing before the current letter.i
denotes insert.a
start typing at the end of the current line.a
denotes append.o
start typing on a new line.o
denotes open.
Here is how to use the above letters.
Open file.txt
using either editor. Next, press i
to enter the write mode. Your editor notifies you that you are in the insert mode. You can confirm that by checking -- INSERT --
at the bottom left corner of the screen.
Type the following words.
Vim is my favorite editor.
Return to the command mode by pressing the escape esc
key. Resume typing on a new line by pressing small o
. Add the following sentence:
This is a new line below the former.
Apart from the command or insert mode, you can use the visual mode. It enables you to use the mouse for typical actions like line selecting.
Let’s return to the command mode, then press v
to enter the visual mode. You can confirm you are in the visual mode by seeing -- VISUAL --
at the bottom left corner of the screen.
Likewise, you can switch from the visual mode to the command mode by pressing v
.
4. Navigate in the same way.
You can use the left, right, top, and bottom arrow keys to navigate the lines in the respective directions. Alternatively, you can use the h, l, k, and j keys to navigate the left, right, top, and bottom of the screen.
5. Search in the same way.
Both Vi and Vim let you search a character or a string of text in the same way. Assume you want to move to every occurrence of is in the file.txt
. First, get into the normal mode.
Type /
followed by the target text, in this case, is
then press enter
. You can move to the next occurrence of the search term by pressing n
.
Here is a summary of some of the commands common to both the Vi and Vim editors.
Command | Result |
---|---|
i | Enter the insert mode |
a | Enter the insert mode (by writing after cursor) |
A | Enter the insert mode (by writing at the end of the line) |
o | Enter the insert mode (by opening a new line) |
ESC | Terminate the insert mode |
:q | Quit without the saving |
:w | Save the file, but DONT quit |
:wq | Save the file and quit it |
shift + zz | Save the file and quit it |
u | Undo the last change |
U | Undo all changes on an entire line. |
dd | Delete one line |
Ndd | Delete a specific number of lines, for instance, 3dd. |
D | Delete line contents after the cursor |
C | Delete line contents after the cursor then insert a new text. |
dw | Delete a character |
Ndw | Delete the specified number of characters, for example 3dw. |
x | Delete a character at the cursor |
r | Replace a character |
R | Overwrite characters after the cursor |
cw | Change a word |
Now that you know the similarities between Vi and Vim and can even able to play with the basic commands, let’s focus on the unique side of Vim.
What Makes Vim a better Editor than Vi? 7 Key Differences between Vi and Vim
There is a bunch of additions making Vim better than the Vi editor. You can find the differences by running the help
command with the vi_diff
option.
:help vi_diff
Here are some of the differences.
1. Vim integrates with the cscope.
Cscope is a tool helping you to browse source codes.
2. Vim enables you modify files using network protocols.
Examples of the network protocols are SSH, and HTTP.
3. Vim ports to wider range of operating systems.
For example, you can use vim on Windows using gvim.
4. Vim is highly customizable
Vim allows you to configure it to your needs. For example, using the Neovim
extension lets you extend and integrate Vim with more plugins.
5. Vim has multilevel undo and redo
Let’s add three lines to our file.txt.
First line added. Second line here. Third as well.
Assume we want to undo the three lines at once. We can do that by returning to the normal mode then typing 3u
6. Vim eases file comparison
You can use vimdiff
to compare files in Vim. Let’s create another file, test.txt, with the same content as file.txt and an extra line, Extra line.
So, file.txt has the following content.
Vim is my favorite editor. This is a new line below the former.
Whereas test.txt has this content.
Vim is my favorite editor. This is a new line below the former. Extra line.
Let’s highlight the difference between the two files by running the vimdiff
command on the terminal.
vimdiff file.txt test.txt
The extra line gets highlighted.
You can exit the screen by running the quitall
command.
:quitall
7. Vim supports multiple languages
Vim supports many languages like C, C++, Python, Perl and Shell. For example, you can turn on/off syntax highlighting for the twoSum.c file as follows.
vim twoSum.c
:syntax on
:syntax off
Syntax off
Syntax on
Besides, Vim lets you to do scripting using languages like Python, Shell and Perl.
Conclusion
Vim has everything in Vi. It is preferred to Vi because of its customizability and support for multiple operating systems, files, and programming languages.
Now that you understand the editors’ differences and similarities, it would help to keep practicing the commands as guided in this tutorial. You can then focus on the more interactive side of Vim by using the NeoVim
extensions.
In essence, the difference sticks to a principle of simplicity, whereas Vim, throwing in the kitchen sick, is an Emacs wannabee.