A Beginner's Guide To Using Vi/vim Editor
A Step by Step Guide to Setting up and Using vi/vim Editor

A Seasoned Software Engineer and Technical Writer.
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 filenamefor vi, andvim filenamefor 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+EnterThis saves the latest changes made to the file and exits.:wq+EnterThis also saves the latest changes made to the file and exits the editor, it does the same work as the:x+Enter:q+EnterThis 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!+EnterThis 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:
jor thedown-arrowkey moves the cursor down by 1 line.korup-arrowkey moves the cursor up by 1 line.horleft-arrowkey moves the cursor left by 1 character.lorright-arrowkey 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.wkey moves the cursor to the beginning of the next word.bkey moves the cursor to the beginning of the preceding word.:0moves the cursor to the first line in the file.:nmoves 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
iinsert text before the cursor.Iinsert text at the beginning of the current line.ainsert/append text after the cursor.Ainsert/appends text at the end of the current line.oputs the text in a new line below the current line.Oputs the text in a new line above the current line.
Changing Text
rchanges the character presently under the cursor.Rchanges the character under the cursor from the point theRis pressed until TheEsckey is pressed.cwchanges the current word with new text until TheEsckey is pressed.cNwchangesN(number) of words starting from the word under the cursor until TheEsckey is pressed. For example,c3wchanges three words starting from the word that is under the cursor.Cchanges/replaces all characters in the current line starting from the character under the cursor until TheEsckey is pressed.ccreplaces the entire line irrespective of the position of the cursor, until TheEsckey is pressed.NccorcNcreplacesNnumber of lines starting from the line the cursor is untilEscis hit. For example,3ccorc3creplaces three lines starting from the line the cursor is presently on.
Deleting Text
xdeletes a single character under the cursor.NxdeletesNnumber of characters, starting from the one under the cursor. For example,8xdeletes8characters, starting from the one under the cursor.dwdelete the single word under the cursor.dNwdeletes N number of words starting from the word under the cursor. For example,d5wdeletes5words starting from the word under the cursor.Ddelete the remainder of the line, starting with the line under the cursor.dddeletes the entire line on which the cursor is presently on.NddordNddeletes N number of lines starting from the line the cursor is positioned. For example,8ddord8ddeletes8lines from the line the cursor is positioned.
Copying, Cutting and Pasting Text
Copying Text
yycopies the current line with the cursor.NyyoryNycopies N number of lines, starting from the line with the cursor. For example,3yyory3ycopies3lines, starting with the line that has the cursor.
Cutting Text
ddcuts the entire line with the cursor.dwcuts the single word under the cursor.dNwcuts N number of words starting from the word under the cursor. For example,d5wcuts5words starting from the word under the cursor.Dcuts the remainder of the line, starting with the line under the cursor.NddordNdcuts N number of lines starting from the line the cursor is positioned. For example,8ddord8dcuts8lines from the line the cursor is positioned.
Pasting Text
To paste a text that has been copied or cut, simply use the
pkey.
Searching Text and Determining Line Number
Searching Text
/strsearches for the occurrence of the wordstrgoing forward from the position of the cursor.?strsearches for the occurrence of the wordstrgoing backward from the position of the cursor.nmoves forward to the next occurrence of the searched word/string.Nmoves 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+gthis 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 numberThis 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 filenameWhile 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.:wwrites/saves changes made to the present file you are working on.:w filenameWrites the contents of the present file you are working on to a new file namedfilename. Thefilenamewill be created automatically and the contents will be copied into the file.:n, Nw filenameWrites the content of the present file you are working on in the editor starting from line numbernto line numberNto a new file namedfilename. This file will be created automatically. For example,5, 15w Gideonwill 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! filenameThis command writes the contents of the present file one is working on in the editor to an already existing file namedfilename.




