Vi Editor in Linux














































Vi Editor in Linux



A Text Editor is a tool/software used to edit the text in text files. Appending and Deleting text are the most essential features of a Text Editor but most of them go beyond these two features and add a lot more functionality. Text editors do not have tools for text visualization or text styling (like in MS-Word). They focus more on the text itself. A few examples of Text Editors are Atom, Sublime Text, and TextMate.


Linux comes with a handful of useful Text Editors. There are two types of Text Editors in Linux:


1. Command-Line Text Editors

2. GUI Text Editors


A Command-Line Text Editor is one that comes in-built with the Linux Command-Line. Examples of such Text Editors are vi, emacs, nano, and pico. 


A GUI Text Editor is one that comes in a Graphical User Interface(GUI). Examples of GUI Text Editors are edit (for GNOME) and Writer (for KDE).


Vi, also known as Vim (vi improved) is the most popular command-line text editor in Linux. The vi command can be used to create new files as well as edit existing ones.


The syntax of vi is:

This allows the user to edit a file named filename from line 1.


vi runs in three modes: 


i. Command Mode - In the command mode, the letters typed by the user perform editing functions (such as moving the cursor, deleting existing text, etc.) on the file.


ii. Insert Mode - In the insert mode, the characters typed by the user are appended to the file.


iii. Escape Mode - In this mode, the cursor jumps to the end of the file and waits for a command from the user. This mode is useful for operations such as saving the file.







To install vi, the following commands can be used based on the Linux distribution being used:



A few options that can be used with vi are - 


i) -i - This option allows the user to enter text before the current location of the cursor.


ii) -I - This option allows the user to enter text at the beginning of the current line.


iii) -a - This option allows the user to enter text at the end of the current cursor location (this option is used to append text the end of a file).


iv) -A - This option allows the user to enter text at the end of the current line.


v) -o - This option creates a new line to enter text below the current cursor location.


vi) -O - This option creates a new line to enter text above the current cursor location.


After editing a file, the "Esc" button is used to come back to the command mode. After hitting Esc, there are certain options based on which the edited file is saved.


i) q! - This command exits the vi editor and does not save the changes made by the user in the file.


ii) wq - This command is called write and quit. It exits the vi editor and saves all the changes made by the user in the file.


iii) w filename - This command creates a new file named filename and saves the writes the contents of the edited file to this new file. The changes do not reflect in the existing file. This option is similar to "Save As".


iv) w! filename - This command is similar to w filename. In this command though, the file filename is overwritten with the contents of the existing file forcefully.


To move the cursor inside the file, the following commands can be used in command mode -


i) k - This command moves the cursor up by one line.


ii) j - This command moves the cursor down by one line.


iii) h - This command moves the cursor to the left by one character.


iv) l - This command moves the cursor to the right by one character.


v) 0 - This command positions the cursor to the beginning of the current line.


vi) $ - This command positions the cursor at the end of the current line.


vii) W - This command positions the cursors to the next word.


viii) B - This command positions the cursor to the previous word.


ix) :n - This command places the cursor on the line number represented by n.


To copy and paste text, the following commands are used -


i) Yy - This command allows the user to copy the current line (i.e. the line in which the cursor is currently placed).


ii) nyy - This command allows the user to copy (or yank) the current line and the n lines that follow.


iii) p - This command pastes the copied text after the current text position.


iv) P - This command pastes the copied (or yanked) text before the cursor. 


Comments