A Beginners Guide to vi / vim text editor

vi (visual editor) is a terminal based text editing application, originally created for the Unix operating system. It is a fast, powerful and most popular and classic text editor in the Linux family. It is available in almost all Linux and Unix Distributions, and works the same across different platforms and Distributions. Vi does not contain any menu, instead it uses combinations of keystrokes to accomplish an action. It requires very few resources to run. An improved version of the vi editor which is called the VIM (Vi IMproved) has also been made available now. The vi editor is a full screen editor and has two modes of operation:

  • Command mode :  This is the default mode when you open the vi. In command mode whatever the user type is interpreted as a  command. This mode enables user to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting and pasting the lines or words, as well as finding and replacing it. Command mode Commands are case sensitive, so you should need to use the right letter case.
  • Insert Mode : This mode is for inserting text in the file. The user can switch to the Insert mode from the command mode  by pressing 'i' on the keyboard. In the insert mode, every character typed is interpreted as input and placed in the file. To return to the command mode you need to press the Esc key. 

Note : In most Linux Systems By default vi is installed. To upgrade it into vim you need to install vim by :

To start the vi editor just open the terminal and type vi or vim on terminal.
 $ vi

The above picture shows the default screen when we start vi standalone. At startup the vi editor is in command mode. Now in order to go to insert mode just press i , and you are in insert mode, which is shown in the bottom-left side of the terminal screen.

after typing something, to go back in the command mode press the Esc button. You can also press the Esc button twice to make sure you are in command mode. Now to type a command in command mode press ' :( shift + ; ) and your command then hit enter.  For example to save our typed text

        :w file.txt

where w for write the data in file and file.txt is file name where the data would be written. You can also notice the ~(tilde) lines on the left side of the screen which indicate the unused lines of the current buffer or file. We can also open an existing a file or create a new one at startup of vi then pass the file name as argument
 $ vi test.txt
now to save the modifications and changes of the file, we just need to use :w command. And to quit the vi editor use the :q command. To open a file in read only mode put -R flag in the arguments
 $ vi -R test.txt
or we can also view
 $ view test.txt

2. Commands To Save and exit VI editor :

To save file after editing and modifications, go to command mode and use below commands :

Command Details
:wsave the file (write all the modifications and changes in it.)
:wqsave the file and quit the editor
:qquit or exit the vi editor
:q!quit or exit the vi editor without saving the modifications and changes in file.
:xsave the file and quit the editor (same as :wq command)
Shift+zzSave the file and quit

3. Text Editing (Adding and Inserting text) :

The following commands allow you to insert and add text in current you are working with. Each of these commands puts the vi editor into insert mode.

Command Details
iinsert text before cursor
Iinsert text at beginneing of the current line
aappend text after cursor
Aappend text to end of current line
oopen and put text in a new line below current line
Oopen and put text in a new line above current line

Note : To jump from Insert mode to command mode press Esc button.

4. Cursor movement :

The following commands allow you to move cursor

Command Details
Up-arrow or kmoving cursor up
Down-arrow or jmoving cursor down
Left-arrow or hmoving cursor left
Right-arrow or lmoving cursor right
gg or :0move cursor to the first line of file
shift+g or :$move cursor to the last line of file
0move cursor to the beginning of a line
shift+$move cursor to the end of a line

5. Deleting Text : 

The following commands allow you to delete text from buffer

Command Details
xDelete single character under cursor
NxDelete N characters, starting with character under cursor,
e.g. 10x delete 10 characters
dwDelete the single word beginning with character under cursor
dNwDelete N words beginning with character under cursor,
e.g, d8w delete 8 words
ddDelete entire current line
Ndd or dNdDelete N lines, beginning with the current line,
e.g. 7dd deletes 5 lines
DDelete contents of a line after the cursor
CDelete contents of a line after the cursor and insert new text.
Press ESC key to end insertion.

6. Replacing Text :

Command Details
cwChange word : delete the current word and go to
insert mode to put the new word
rReplace the character
Roverwrite characters from cursor onward
sSubstitute one character under cursor and go to insert mode
SSubstitute entire line and begin to insert at the
beginning of the line
~Change the case( Lower to upper / Upper to Lower)
of individual character

7. Undo and Redo text : 

The following command is used to unto and redo

Command Details
uundo last change
Ctrl+RRedo changes which were undone

8. Copying and Pasting Text :

The following commands allow you to copy and paste text. 

Command Details
yyCopy (yank, cut) the current line
Nyy or yNyCopy (yank, cut) the next N lines, including the current line
pPaste the copied text after the cursor (lowercase p)
PPaste the copied text before the cursor (uppercase P)

9. Searching Text :

The following commands allow you to search text :

Command Details
/stringsearch forword for occurence of string in text
?stringsearch backword for occurence of string in text
nmove to next occurence of search string
Nmove to next occurence of search string in opposite direction

10. Insert another file into Current opened file :

 The following commands allow you to Insert another file into Current opened file.

Command Details
:r filenameinsert the content of file [filename] after
the current line
:w newfilewrite the content of the currently opened file
on [newfile]
:M,Nw filenamewrite the content from line M to line N on a
new file named filename, e.g :2,9w code.c
:w! filenamewrite current contents over a pre-existing
file named filename
:s/search/replace/gThe :s/ (substitution command) enables you to
quickly replace words or group of words in file.
e.g. :s/bricks/walls/g
at this example bricks is replaced with walls, where the last g
stands for global.

11. Control Commands for screen manipulation :

The following commands can be used with the Control Key to performs various screen-related functions :

Command Details
Ctrl + fmove forward one screen
Ctrl + bmove backward one screen
Ctrl + dmove down (forward) one half screen
Ctrl + umove up (back) one half screen
Ctrl + lredraws the screen
Ctrl + rredraws the screen, removing deleted lines

12. Opening multiple files with Splited screen :

In vi we can also open multiple files with splitted screen. Commands for screen splitting :

Command Details
:spFor horizontal split
:vspFor vertical split

For demonstration let we have three files data1.txt, data2.txt and a c file. Now first we gonna open data1.txt
 $ vi data1.txt
 after that in command mode open tempr.c file in vertical split mode

         :vsp tempr.c

 now again we are going to open data2.txt file by

          :sp data2.txt

 To jump from one file to another press Control + w, where you need to press w twice.

13. Opening multiple files within Tabs :

To open files in Tabs, use -p flag at startup of vi editor. For example
 $ vi -p code.c data1.txt data2.txt
this will open all three file with tree tabs

To jump from one file to another tab press Ctrl +Alt + PageUp/PageDown Button.

14. Configuring vimrc file :

vimrc is a configuration file used by vim editor to save settings and configs which is used by the editor at startup. vimrc stands for vim Run-time Configuration. In Unix-based systems (Linux, Mac OS X, etc.), vimrc files are saved to the user's home directory as a hidden file with no filename prefix (.)  ~/.vimrc, and in Windows Vim editor, uses a file with the name _vimrc instead of .vimrc. Some of the basic configuration of vimrc file are :

Command Details
set ts=4set tabs 4 space long
colo delekset theme as delek
set numbershow line numbwer

To try it out create a .vimrc file at your home directory and put those lines on your .vimrc file then launch vim again, and now you will see the changes.

 So, these are some very basics about vi/vim editor. And I hope you like it.