What is the VI editor?
The default editor that comes with the UNIX operating system is called vi (visual editor). vi (pronounced as distinct letters, / ˌviːˈaɪ /) is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and POSIX.
Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved.
To Start vi
- To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.
-
vi <filename_NEW> or <filename_EXISTING>
vi filename | edit filename starting at line 1 |
---|---|
vi -r filename | recover filename that was being edited when system crashed |
To Exit vi
- Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.
- Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.
😡<Return> | quit vi, writing out modified file to file named in original invocation |
---|---|
:wq<Return> | quit vi, writing out modified file to file named in original invocation |
:q<Return> | quit (or exit) vi |
:q!<Return> | quit vi even though latest changes have not been saved for this vi call |
Command mode:
- The vi editor opens in this mode, and it only understands commands
- In this mode, you can, move the cursor and cut, copy, paste the text
- This mode also saves the changes you have made to the file
- Commands are case sensitive. You should use the right letter case.
Insert mode:
- You can switch to the Insert mode from the command mode by pressing ‘i’ on the keyboard
- Each of these below commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.
i insert text before cursor, until <Esc> hit I insert text at beginning of current line, until <Esc> hit a append text after cursor, until <Esc> hit A append text to end of current line, until <Esc> hit o open and put text in a new line below current line, until <Esc> hit O open and put text in a new line above current line, until <Esc> hit
vi Editing commands
Note: You should be in the “command mode” to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.
Keystrokes | Action |
---|---|
i | Insert at cursor (goes into insert mode) |
a | Write after cursor (goes into insert mode) |
A | Write at the end of line (goes into insert mode) |
ESC | Terminate insert mode |
u | Undo last change |
U | Undo all changes to the entire line |
o | Open a new line (goes into insert mode) |
dd 3dd |
Delete line Delete 3 lines. |
D | Delete contents of line after the cursor |
C | Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion. |
dw 4dw |
Delete word Delete 4 words |
cw | Change word |
x | Delete character at the cursor |
r | Replace character |
R | Overwrite characters from cursor onward |
s | Substitute one character under cursor continue to insert |
S | Substitute entire line and begin to insert at the beginning of the line |
~ | Change case of individual character |
Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required.
Cut, copy, paste – Visual mode
Before you start, make sure you are in normal mode (text editing/command mode). The best way to do so is to press Esc. This mode allows you to move through the text easily.
- Place the cursor on the line you want to begin cutting.
- Press V to select the entire line, or v to select from where your cursor is.
- Move the cursor to the end of what you want to cut, using h,j,k, or l
- Press y to copy it, or d to cut it.
- Place the cursor where you would like to paste your copied stuff.
- Press P to paste it before your cursor, or p to paste it after the cursor.
Copy, Cut and Paste in Normal Mode
y
key on the keyboard when performing this operation.- To copy an entire line, place the cursor at the beginning of the line and type:
yy
- To copy three (3) lines, move the cursor from where you want to begin copying and type:
3yy
- To cut the entire line in which the cursor is located type:
dd
- To cut three (3) lines, starting from the one where the cursor is located use:
3dd
In Vim terminology, pasting is called putting and the function is utilized with the p
command.
You can paste (or put) text by moving the cursor to the wanted position and pressing:
p
Using this command pastes the selected text after the cursor.
To add text before the cursor, type the capitalized command instead:
P
Saving and Closing the file
These commands permit you to input and output files other than the named file with which you are currently working.
Keystroke | Use |
---|---|
Shift+zz | Save the file and quit |
:w | Save the file but keep it open |
:q | Quit without saving |
:wq | Save the file and quit |
Summary:
- The vi editor is the most popular and commonly used Linux text editor
- It is usually available in all Linux Distributions.
- It works in two modes, Command and Insert
- Command mode takes the user commands, and the Insert mode is for editing text
- You should know the commands to work on your file easily
- Learning to use this editor can benefit you in creating scripts and editing files.