VI Editor : Important Keys to Remember While Using in Linux Servers


What is Vi editor and how does it operate?

VI or VIM is a modal editor. Vi editor is used to edit or create a file. It has two main modes of operation. First one is the “command mode” and the second one is “insert mode“.

Vi starts with the Command Mode and when “i” is pressed it is switched to Insert mode & when Esc is pressed it is again switched back to command mode from insert mode.

Modal editor means the buttons and keys role changes depending on the mode selected (Command mode or insert mode).

In insert mode, typed text is inserted in the document. Press Esc from the insert mode to switch in the command mode, in which keys correspond to the cursor or editing functions. Keys typed in command mode are not included in the text.

In command mode, many operations can be performed in series with the sequences of simple keys.

The best part is that it also allows the user to permanently keep his hands on the keyboard.

Suggested Article: Configure JBoss on Linux Server [Step by Step]

What is the function of the important keys in the Vi editor?

a) Open a file to launch vi:

vi file

b) Saving a file:

:wq!                     Save the file and quit vi
:q!                      Quit vi without saving changes
:e!                      Ignore the changes and reload the file
:w!                      Saves the file without quiting

c) Movements in vi:

[CTRL] f       Descend a screen
[CTRL] b       Up One screen
[CTRL] d       Descent to 1/2 screen
[CTRL] u       Move up half screen

d) Cursor positioning:

H                     Top of the screen
M                     In the middle of the screen
L                     bottom of the screen
h                     Shifted by one character to the left
j                     Down one line
$                     At the end of the line
w                     At the beginning of next word
b                     Move backward by one word

e) Inserting text:

i        Enables insert mode to insert text
I        Inserts at the beginning of the line
A        Inserts at the end of the line
O        Inserts a line above the cursor and pass through the insert mode
o        Inserts a row below cursor and pass through the insert mode
[ESC]    Ends insert mode & enables Command Mode

f) Inserting and replacing text:

r         Replaces the character at the cursor position 
cw        Replaces only the word at the cursor position ([ESC] to complete)
cnw       Replaces n words ([ESC] to complete)
C         Replaces the complete line ([ESC] to complete)

g) Deleting text:

x                   Deletes a character
dw                  Deletes a word
dnw                 Deletes n words
dd                  Deletes a line
ndd                 Deletes n lines

h) Copy & paste the complete line:

Y                   Copy a line
nY                  Copy n lines
p                   Paste lines before the cursor
P                   Paste lines after the cursor

i) Find & Search text

/ string            Search text "string" down
n                   Repeat the last search
// [RETURN KEY]     Same as n

j) Undo / Redo changes

u              Undo the last change
U              Cancels all changes made on the current line
.              Repeat last changes

This article will give you an idea about vi editor commands and key commands which are sufficient for the daily use and will save your time in editing a file. Now you are good to go to use Vi editor.

Please leave your comments and feedback in the comment box if you find it beneficial or in case of any doubts.

Leave a Reply

Be the First to Comment!

Notify of