VI Editor

Most of the variant UNIX comes up with a default editor VI (visual editor). It is a test editor only and we can’t include graphics to it.

VI editor has 3 modes of operation:

Insert Mode: This mode is for updating content in a file and it comes when you type vi filename and press enter in command prompt and when the file is opened type ’i’ inserting text.

Command Mode: This mode comes when you press ‘esc’ button to leave insert mode. In this mode you can move the cursor with arrow keys in file without causing any data modification as some variant of UNIX modifies the rows in insert mode when cursor is moved with arrow keys. In this mode the ‘x’ key will delete individual character. ’dd’ key will delete entire line.

Command-line Mode: This mode is used to make changes to file with the editor through typing command. To enter this mode type ‘:’ while in command mode. The ‘:’ will get displayed at the bottom of the screen. You can type your command on this line after ‘:’.

Most of the UNIX variant now allows cursor movement with the arrow keys so cursor movement using commands is less popular now.

Commands used in 2 different modes of operation in VI editor:

Command Mode commands

yy – yank or copy lines which will be pasted in any other row with command “p” (put)

x – delete single character

nx – delete ‘n’ characters from the cursor

X – to delete one character before cursor

dd – delete line

2dd – delete 2 lines

dw – delete single word

5dw – delete 5words

D – to delete from cursor position to end of line

db – delete word before cursor

h – move cursor left (same as left arrow key)

j – move cursor down (down arrow key)

k – move cursor up (up arrow key)

l – move cursor right (right arrow key)

$ – move cursor to last column on the current line

0 – move cursor to first column on the current line

^ – move cursor to first non blank column on the current line

e- move cursor to end of next word or punctuation mark

E – move cursor to end of next word skipping punctuation




w- move cursor to the beginning of next word or punctuation

W – move cursor past the next space

b – move to the beginning of the previous word or punctuation mark

B – move to the beginning of previous word , ignore punctuation

L – move cursor to bottom of screen

M – move cursor to middle of screen.

G – move to last line

nG – move to nth line in the file

r – replace character with new character

R – keep replacing character till you halt by pressing ‘esc’

i- insert before cursor

a – append after cursor

A – append at end of line




o- open line below cursor and goes to insert mode

O – open line above cursor and goes to insert mode

. – repeat last command

u- undo last command

U – undo all commands fired on one line

J – joins current line with next line

4J – joins next 4 lines

ma – bookmark current line with a character “a”

‘a – go to the line with bookmark “a”



Command-line Mode commands

From command mode, you can go to command line mode by pressing ‘:” or any other find commands. Press ‘esc’ to come back to command mode. Commands used in command-line mode are:

:wq – to exit vi and save the changes

:w – to save and stay in the same page.

:q! – to exit vi without saving

:q – to quit

? – finds a word going backwards

/ – finds a word going backwards

: set nu – shows lines with line numbers at the left side

<<BACK