Vi Commands

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Vi Commands

Linux vi editor is different from other editors. You have to use different keys to use different functions. Although, it's quite easy and interesting to use vi editor.

The vi editor commands are case sensitive.

Have a look at the vi commands in the following table.

To switch from command to insert mode:

Command Action
i Start typing before the current character
I Start typing at the start of current line
a Start typing after the current character
A Start typing at the end of current line
o Start typing on a new line after the current line
O Start typing on a new line before the current line

To move around a file:

Commands Action
j To move down
k To move up
h To move left
l To move right

To jump lines:

Commands Action
G Will direct you at the last line of the file
`` Will direct you to your last position in the file

To delete:

Commands Action
x Delete the current character
X Delete the character before the cursor
r Replace the current character
xp Switch two characters
dd Delete the current line
D Delete the current line from current character to the end of the line
dG delete from the current line to the end of the file

To repeat and undo:

Commands Action
u Undo the last command
. Repeat the last command

Command to cut, copy and paste:

Commands Action
dd Delete a line
yy (yank yank) copy a line
p Paste after the current line
P Paste before the current line

Command to cut, copy and paste in blocks:

Commands Action
dd Delete the specified n number of lines
yy Copy the specified n number of lines

Start and end of line:

Commands Action
θ Bring at the start of the current line
^ Bring at the start of the current line
$ Bring at the end of the current line
Delete till start of a line
d$ Delete till end of a line

Joining lines:

Commands Action
J Join two lines
yyp Repeat the current line
ddp Swap two lines

Move forward or backward:

Commands Action
w Move one word forward
b Move one word backward
w Move specified number of words forward
dw Delete one word
yw Copy one word
dw Delete specified number of words

Search a string:

Commands Action
/string Forward search for given string
?string Backward search for given string
/^string Forward search string at beginning of a line
/string$ Forward search string at end of a line
n Go to next occurrence of searched string
/ Search for the word he (and not for there, here, etc.)
/pl[abc]ce Search for place, plbce, and plcce

Replace all


:<startLine,endLine> s/<oldString>/<newString>/g 


Commands Action
:1,$ s/readable/changed/ Replace forward with backward from first line to the last line
:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth line

Text buffers:

Commands Action
"add Delete current line and put text in buffer a
"ap Paste the line from buffer a



:ab <abbreviation> <abbreviatedWord>


Commands Action
:ab au abbrevition and unabbreviation Abbreviate au to be 'abbrevition and unabbreviation'
:una au  Un - abbreviate au