vi

What is vi?

Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.

Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.

How to use vi - modes

  • Command Mode: When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we type as commands and thus does not display them in the window. This mode allows us to move through a file, and to delete, copy, or paste a piece of text. To enter into Command Mode from any other mode, it requires pressing the [Esc] key. If we press [Esc] when we are already in Command Mode, then vi will beep or flash the screen.

  • Insert mode: This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and finally, it is put in the file. The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.

  • Last Line Mode(Escape Mode): Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode enables you to perform tasks such as saving files, executing commands.

how to use vi

# syntax 
vi <FILE>

##### open a text file
vi file.txt

##### within the vi shell - command modus - general commands (where you will be in when you startup vi with a file)
k : Moves the cursor up one line.
j : Moves the cursor down one line.
h : Moves the cursor to the left one character position.
l : Moves the cursor to the right one character position.
0 : Positions cursor at beginning of line.
$ : Positions cursor at end of line.
H : Move to top of screen.
M : Move to middle of screen.
L : Move to bottom of screen.

##### within the vi shell - command modus - delete commands (you can also delete in insert modus)
d$ : Deletes from current cursor position to the end of the line.
Dd : Deletes the line the cursor is on.

##### within the vi shell - command modus - copy and paste commands
Yy  : Copies the current line.
9yy : Yank current line and 9 lines below.
p : Puts the copied text after the cursor.
P : Puts the yanked text before the cursor.

##### within the vi shell - command modus - enter insert modus commands
i : Inserts text before current cursor location.
I : Inserts text at beginning of current line.

##### within the vi shell - last line mode - leave and save + more options
# in order to enter last line mode : when in insert mode press [ESC], then when in command modus
# press : and you will enter last line mode
q              : Quit
q!             : Quit without saving changes i.e. discard changes.
r fileName     : Read data from file called fileName.
wq!            : Write and quit (save and exit).
w fileName     : Write to file called fileName (save as).
w! fileName    : Overwrite to file called fileName (save as forcefully).
# for example : !whoami, this will run the shell command : whoami
!cmd           : Runs shell commands and returns to Command mode.
/searchterm    : Searches for the term entered within the file

Last updated