Vi is widely used Linux text editor as its easy to use and available across all Linux platforms. Vi text editor has both command mode and edit or insert mode, which ensure flexibility as well as ease in use.
Command mode
When you open vi editor it enters in command mode first. In command 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 / Edit mode
In Insert mode you can edit or insert text in a file. To enter into insert mode from command mode simply press “i” . To return to command mode simply press “Esc” key.
How to start / launch Vi text editor from terminal
To get into vi text editor from command mode use below command, with this command you can open both existing or create new file.
To create a new file with vi text editor in Linux
Syntax
vi <new_filename>
Example
vi test
This command will create file with name test
Open existing file with vi text editor in Linux
Syntax
vi <existing_filename>
Example
vi test
This command will open file with name test.
Note: you will have to provide complete path for file if you are executing command from some other directory.
Moving cursor in File in vi text editor linux
In older unix like system these keys were used to move the cursor around in vi text editor. In most of the modern unix like systems same can be achieved with arrow keys.
Note: you must be in command mode to use these keys.
Key | Direction of cursor movement |
h | Move to left by one character |
j | Move to down by one character |
k | Move to up by one character |
l | Move to right by one character |
0 (Zero) | Move to beginning of line |
$ | Move to end of line |
W | Move cursor to next word |
B | Move to previous word |
( | Move to beginning of current sentence |
) | Move to end of current sentence |
H | Move to top of screen |
L | Move to bottom of screen |
nH | Move to nth line from top |
nL | Move to nth line from bottom |
Editing a File in vi text editor Linux
The vi text editor provides command to make changes in your document. As in command mode all commands are case sensitive.
Key | Action |
i | Insert mode will be activated with option write before cursor |
a | Insert mode will be activated with option write after cursor |
A | Insert mode will be activated with option write at end of line |
I | Insert mode will be activated with option write before first non blank character |
o | Insert mode will be activated with option write new line above current line |
O | Insert mode will be activated with option write new line after current line |
ymotion | Yank (Copy) text over which cursor moves |
dmotion | Delete (Cut) text over which cursor moves |
yy | Yank (Copy) one line |
dd | Delete (Cut) one line |
4dd | Delete 4 lines. 4 can be replaced by any number |
D | Delete line after cursor |
cmotion | Delete text over which cursor moves and enters into insert mode |
cw | Change word |
u | Undo last change |
U | Undo changes of entire line |
x | Delete character on which cursor is placed |
r | Replace character |
Saving or Closing file in vi text editor Linux
Any text file created will need to be saved. There are specific command for these also.
Key | Action |
:w | Write / save file and quit |
:q | Quit vi without saving file |
:wq | Write/save file and quit vi |