How to use Vi terminal text editor in Linux

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.

KeyDirection of cursor movement
hMove to left by one character
jMove to down by one character
kMove to up by one character
lMove to right by one character
0 (Zero)Move to beginning of line
$Move to end of line
WMove cursor to next word
BMove to previous word
(Move to beginning of current sentence
)Move to end of current sentence
HMove to top of screen
LMove to bottom of screen
nHMove to nth line from top
nLMove 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.

KeyAction
iInsert mode will be activated  with option write before cursor
aInsert mode will be activated with option write after cursor
AInsert mode will be activated with option write at end of line
IInsert mode will be activated with option write before first non blank character
oInsert mode will be activated with option write new line above current line
OInsert mode will be activated with option write new line after current line
ymotionYank (Copy) text over which cursor moves
dmotionDelete (Cut) text over which cursor moves
yyYank (Copy) one line
ddDelete (Cut) one line
4ddDelete 4 lines. 4 can be replaced by any number
DDelete line after cursor
cmotionDelete text over which cursor moves and enters into insert mode
cwChange word
uUndo last change
UUndo changes of entire line
xDelete character on which cursor is placed
rReplace 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.

KeyAction
:wWrite / save file and quit
:qQuit vi without saving file
:wqWrite/save file and quit vi