A Beginner's Guide To Using Vi/vim Editor
A Step by Step Guide to Setting up and Using vi/vim Editor
Table of contents
What is vi/vim
Vi/Vim is a text editor that can be used in the command line. What makes this text editor very popular amongst developers is because When software developers/engineers work on servers, they often only have access to command-line editors, and in the command line, most 3rd party code editors like Atom, VS Code, etc. won't work. This is where terminal-based editors like vi/vim, Emacs, Less, Nano, etc. come into play. Additionally, vi/vim has two modes: Command Mode and Editor Mode, which makes it versatile and efficient for developers/engineers to write code and scripts. It is called vi (Visual Editor) or vim (Vi IMproved). originally, it was developed to work on UNIX-based operating systems, but as of now, it can also be installed as a standalone application on Windows Operating System.
How to Set up vi/vim
on most UNIX-based Operating Systems, like Linux (Ubuntu). the vi editor comes pre-installed, but for the Windows Operating system, the vi can be downloaded and installed as a standalone application, you can download it from the link below:
Download Vi/Vim for Windows Operating System
while for those wanting to use the vi improved (vim), on Ubuntu you will have to install it using the Terminal, by running the command:
sudo apt install vim
vim will be installed once the command runs successfully, and make sure your computer is connected to the internet.
Modes of Operation
We can't talk about using vi/vim without talking about the two modes of operation the vi/vim editor operates on. The two modes of operation are the:
The Command Mode
The Insert Mode
The Command Mode
This mode is the default mode of the text editor. It is the mode that is used to execute various commands that cause different actions to be taken on a file. In the command mode, every character typed does something to the file and these characters range from certain keys on the keyboard or a combination of different keys along with the Ctrl
, Alt
or Shift
key. While in this mode, some keys might cause the editor to enter the Insert Mode, so in the case where you mistakenly enter the Insert mode, you can easily exit back to the command mode using the Esc
key.
The Insert Mode
This mode can be entered from the command mode by pressing the key i
on the keyboard. It is in this mode that text can be entered in a file. Every character you type here is added to the text in the file. After entering text into the file using the insert mode, one can go back to the command mode by using the Esc
key on the keyboard.
How To Use the Vi/Vim Editor
Unlike the 3rd party and GUI (Graphical User Interface) based editors such as VS Code, Atom etc. that make use of the computer mouse to move around and perform some actions like moving the cursor, selecting the text etc.
In this Command line-based editor, the mouse is almost not of use, you have to make use of your keyboard constantly to carry out commands and perform various functions. Here I will talk about the commands that are most often used, starting from the basic ones down to the advanced ones. Also, make sure you are in command mode to be able to carry out most of the operations below.
Start and Exit vi/vim
To Start the vi/vim
Use the command:
vi filename
for vi, andvim filename
for vim. After running this command on the terminal, the file with the name filename will be open if it exists, in a situation where a file with that name does not exist, it will create a file with that exact file name. The filename can be replaced here with any name that you wish to name your file with.To Exit the vi/vim
You can use the following commands, depending on your needs:
:x
+Enter
This saves the latest changes made to the file and exits.:wq
+Enter
This also saves the latest changes made to the file and exits the editor, it does the same work as the:x
+Enter
:q
+Enter
This command quits or exits the editor, in the case where some changes were made, it will alert you, giving you the option to either save the latest changes first before you exit or to exit without saving the latest changes that you made to the file.:q!
+Enter
This command forces the editor to quit even when the latest changes made to the file have not been saved.
Navigating the Editor
Just like I mentioned earlier, the mouse has almost no use in the vi/vim editor and the reason is that when this editor was developed, it was done with the QWERTY keyboard in mind. Another thing that can be used to move around is the arrow keys on the keyboard, which can be used to move left
, right
, up
and down
while in the editor. However, sometimes these arrow keys give some strange effects, so it is advisable to make use of the various keys from the keyboard, and they are:
j
or thedown-arrow
key moves the cursor down by 1 line.k
orup-arrow
key moves the cursor up by 1 line.h
orleft-arrow
key moves the cursor left by 1 character.l
orright-arrow
key moves the cursor right by 1 character.0(zero)
key moves the cursor to the beginning of the line on which the cursor is presently on.$
key moves the cursor to the end of the line.w
key moves the cursor to the beginning of the next word.b
key moves the cursor to the beginning of the preceding word.:0
moves the cursor to the first line in the file.:n
moves the cursor to the line number, n, where n can be any number.:$
moves the cursor to the last line of the file.
Adding, Changing and Deleting Text
Adding Text
i
insert text before the cursor.I
insert text at the beginning of the current line.a
insert/append text after the cursor.A
insert/appends text at the end of the current line.o
puts the text in a new line below the current line.O
puts the text in a new line above the current line.
Changing Text
r
changes the character presently under the cursor.R
changes the character under the cursor from the point theR
is pressed until TheEsc
key is pressed.cw
changes the current word with new text until TheEsc
key is pressed.cNw
changesN
(number) of words starting from the word under the cursor until TheEsc
key is pressed. For example,c3w
changes three words starting from the word that is under the cursor.C
changes/replaces all characters in the current line starting from the character under the cursor until TheEsc
key is pressed.cc
replaces the entire line irrespective of the position of the cursor, until TheEsc
key is pressed.Ncc
orcNc
replacesN
number of lines starting from the line the cursor is untilEsc
is hit. For example,3cc
orc3c
replaces three lines starting from the line the cursor is presently on.
Deleting Text
x
deletes a single character under the cursor.Nx
deletesN
number of characters, starting from the one under the cursor. For example,8x
deletes8
characters, starting from the one under the cursor.dw
delete the single word under the cursor.dNw
deletes N number of words starting from the word under the cursor. For example,d5w
deletes5
words starting from the word under the cursor.D
delete the remainder of the line, starting with the line under the cursor.dd
deletes the entire line on which the cursor is presently on.Ndd
ordNd
deletes N number of lines starting from the line the cursor is positioned. For example,8dd
ord8d
deletes8
lines from the line the cursor is positioned.
Copying, Cutting and Pasting Text
Copying Text
yy
copies the current line with the cursor.Nyy
oryNy
copies N number of lines, starting from the line with the cursor. For example,3yy
ory3y
copies3
lines, starting with the line that has the cursor.
Cutting Text
dd
cuts the entire line with the cursor.dw
cuts the single word under the cursor.dNw
cuts N number of words starting from the word under the cursor. For example,d5w
cuts5
words starting from the word under the cursor.D
cuts the remainder of the line, starting with the line under the cursor.Ndd
ordNd
cuts N number of lines starting from the line the cursor is positioned. For example,8dd
ord8d
cuts8
lines from the line the cursor is positioned.
Pasting Text
To paste a text that has been copied or cut, simply use the
p
key.
Searching Text and Determining Line Number
Searching Text
/str
searches for the occurrence of the wordstr
going forward from the position of the cursor.?str
searches for the occurrence of the wordstr
going backward from the position of the cursor.n
moves forward to the next occurrence of the searched word/string.N
moves backward to the next occurrence of the searched word/string.
Determining Line Number
:.=
returns the line number with the cursor at the bottom of the screen.:=
returns the total number of lines in the file at the bottom of the screen.Ctrl
+g
this command carries out the two commands above at the same time, that is it returns the line number with the cursor and also the total number of lines in the file at the bottom of the screen.:set number
This command shows the number of each line at the beginning of the lines in the file you are working in.
Saving (Writing) and Reading Files
This command helps one to inputs and output files other than the file you are working on. For instance, you can bring in the content of a file into the file you are presently working on without leaving the present file you are working on, and in some instances, you can also save the current file you are working on as another file (i.e with a different name) without leaving the present file you are working on. Below are some of these commands:
:r filename
While inside a file, this command helps you bring in the content of the file named filename into the present file you are working on. It brings it in after the line with the cursor.:w
writes/saves changes made to the present file you are working on.:w filename
Writes the contents of the present file you are working on to a new file namedfilename
. Thefilename
will be created automatically and the contents will be copied into the file.:n, Nw filename
Writes the content of the present file you are working on in the editor starting from line numbern
to line numberN
to a new file namedfilename
. This file will be created automatically. For example,5, 15w Gideon
will write the content of the file I am presently working on in the vi/vim editor to a new file that will be namedGideon
.:w! filename
This command writes the contents of the present file one is working on in the editor to an already existing file namedfilename
.