Linux Vi Editor

vi stands for visual editor and comes as one of default applications in every Linux/Unix system. The vi editor has two modes.

  • Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in command mode.
  • Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.

If you are not sure which mode you are in, press Esc key twice and you’ll be in command mode.

 

Create a file with vi {filename}

vi test.txt
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     

 

Save file

// save and quit
:wq

// save
:w

// quit
:q

// save as filename
:w fname

// save and quit
ZZ

// quit and disregard work
:q!

// quit and save qork
:wq!

 

How to delete on command mode

// delete current character
x

// Replace the current character
r

// Switch two characters
xp

// Delete the current line
dd

// Delete the current line from current character to the end of the line
D

// delete from the current line to the end of the file
dG

 

Repeat and undo

// Undo the last command
u

// repeat the last command
.

 

Copy, Cut, and Paste

// Delete a line
dd

// (yank yank) copy a line
yy

// Paste after the current line
P

// Paste before the current line
p

// Delete the specified n number of lines
{n}dd

// Copy the specified n number of lines
{n}yy


 

Search 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
/{string}$

// Go to next occurrence of searched string
n

// Search for the word he (and not for there, here, etc.)
/\<he\>

// Search for place, plbce, and plcce
/pl[abc]ce

 

Replace all

:{startLine,endLine} s/{oldString}/{newString}/g

// Replace forward with backward from first line to the last line
:1,$ s/readable/changed/

 

  

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.