Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » Linux

VI Editor & Commands

Introduction to VI Editor

vi editor is a fully text editor in Linux Operating System, it’s next (improved) version is VIM (vi improved). It allows you to create, edit your text. It’s very useful to create text files, scripts, make files and to write programs in c,c++ and others.

vi has two operating modes, and you need to switch between them to perform the specified operation.

Command mode : It is a default mode, when you open a file, command mode is selected. In this mode user can give the input (command) to the vi editor to do specified task such as save text, quit etc.

Text mode (Insertion mode) : In this mode, you can edit/insert text into the file. To switch vi into text mode, just press the “a”/ “A”/ “I” .

You can switch again into command mode by pressing ESC key.




How to open?

Type vi file-name in the command shell and then press enter.

Basic (save,quit) commands

command description
:q! Quit from vi editor, without saving the changes.
:wq! Quit from vi editor, with saving the changes.
:w! Save the changes
:file-name Save the changes under the specified file-name.

Cursor movement comannds

command description
ARROW KEYS To move cursor at left,top,right and bottom.
h,k,l,j To move cursor at left,top,right and bottom.
0 To move cursor at the starting of current line.
$ To move the cursor at the end of current line.
:0 To move the cursor at the starting of the file.
:$ To move the cursor at the end of the file.
:n To move the cursor at the nth line in the file.

Editing/Insertion commands

command description
u Undo the changes.
a Switch to text/insertion mode, to add text after the cursor position.
A Switch to text/insertion mode, to add text at the end of the current line.
i Switch to text/insertion mode, to insert text before the cursor position.
I Switch to text/insertion mode, to insert text at the starting of current line.
c Change or Replace the current character.
cc Change or Replace the characters from the current character in the current line.
x To delete the current selected (where cursor is pointing) character.
nx To delete n characters from the current character (including current)
dw Delete the current word.
dnw Delete n words from the current word.
dd To delete current line.
dnd To delete n lines from the current (selected) line.

Cut,Copy & Paste commands

command description
yy Copy the current line into the buffer.
nyy OR yny Copy the n lines including current line into the buffer.
p Paste the copied lines from the buffer just after the current line.

Searching the text into vi editor

command description
/text It is used to search text forward in the file.
?text It is used to search text backward in the file.
n Moves to the next occurrence of text.
N Moves to the next occurrence of text in opposite direction.

Line numbers related commands

command description
/:set nu OR :set number Shows the line numbers, before the each line.
:.= Shows the line number of the current line in the bottom of vi editor.
:= Shows the total number of lines of the file in the bottom of vi editor.
:n Moves the cursor on nth line in the file.