Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

vi Commands listed below:-


but simply, if you want to edit a text file then remember the following rules


vi file.txt


will open the file, now press the INSERT key on your keyboard, and you'll see that vi says 'INSERT' press the key again and you'll see that it says 'REPLACE'.


Pressing ESC (escape) on your keyboard, tells vi to operate in COMMAND MODE, and that allows you to save or quit your editing, for example, after youve edited your file, and you want to SAVE and QUIT you enter the following


ESC :wq


the ESC forces command mode

the : tells vi to look for the commands following

the w tells vi to write the file (save changes)

the q tells vi to QUIT (close)


more info below:-0











vi filename Starts vi and creates a new file

:q! Quits vi without saving work

p Pastes data in buffer below current line

P Pastes data in buffer above current line

yy Copies current line

:r !command Reads in output of the command

i Puts vi in insert mode


:set autoindent Sets vi to indent automatically

:set showmatch Sets vi to show matching parenthesis

:set nu Sets vi to display line numbers

:set showmode Sets vi to display the mode you're in

ESCAPE Sets vi to command mode


CONTROL-U Erases current line (insert mode)

CONTROL-H Erases on letter (insert mode)

CONTROL-W Erases current word (insert mode)



h, j, k, l Moves cursor left, up, down, right respectively

u Undoes last action

x Deletes a single character

dw Deletes a single word

dd Deletes a single line


ZZ Writes work buffer to disk and exits vi

o inserts line after cursor position

CONTROL-L Redraws screen

:w filename Save work as filename and exits


Control Characters





CONTROL-H or BACKSPACE Erases a character on the command line

CONTROL-U Deletes an entire command line

CONTROL-W Erases a word on the command line

CONTROL-C Aborts program execution

COMMAND-Tab Switches Programs

CONTROL-L or CONTROL-R Refreshes the screen

Control-D, logout or exit Logs you off the system


Some vi tips and tricks.


While in command mode:


1G will take you to the top of the file

G will take you to the bottom of the file

#G will take you to line number # (replace # with a number)


While a lot of people use ":wq" to write and quit (save and exit), a shortcut to this is ":x" which does the same thing.


To quit without saving changes, you can use ":q!" and the "!" tells vi to go ahead and forget the changes.


To write the workspace to another filename, you can ":w filename". If you then use the above quit without saving changes ":q!", you will have created a new file "filename" with your changes and left the old file alone. If "filename" already exists and you want to write over it, you can use the command "w! filename" and the "!" will tell vi to go ahead and write over the existing file.


While in edit mode:


:%s/ */ /

that is :s/{space}{space}*/{space}/

will replace all multiple spaces with a single space.


note: the tab is represented by "\t".


To find all tabs in a document and replce them with a space, the command

:%s/\t/ /g is used.


:g/^$/d will delete all blank lines


The "." is a wildcard inside a search. So, if you want to find the letter "B" followed by any two characters and ending with a "C" and replace it with the word "apple", you would type:




The "g" at the end of the expression means global. If you only want to find the first occurance of the string "B..C" on each line, you would leave off the "g" at the end of the expression.


The "%" at the beginning of my search and replace string above is equivalent to "1,$" or "first line to end of file." The two variations of the expression








can be used interchangably. You are not limited to "first line to end of file" however, you can also use :10,20s/..... to target lines 10 through 20.


For a little more advanced example:


You can also match on and move blocks of text with "()" in your search string. For example, I have a document that has phone numbers followed by names separated with a tab character in a file with a format such as this:


444-444-4444 Bob Jones

555-555-5555 Bill Johnson


If I want to switch the file around so that the names come before the phone numbers, I can use the following search and replace string:




note: the {tab} is an actual keyboard tab.


To walk through the expression:


%s/ substitute on all lines

^ start the match at the beginning of the line

\(...-...-....\) match on 3 characters followed by a dash followed by 3 characters followed by a dash and followed by 4 characters. Enclosing them in the escaped parenthesis causes the match to be rememberd for later.

\t a tab character

\(.*\) match all characters to the end of the line


and replace with:


\2 the characters matched in the 2nd set of parenthesis

{tab} you would actually hit the tab on the keyboard here

\1 the characters that are matched in the first set of parens.


the result would be:


Bob Jones 444-444-4444

Bill Johnson 555-555-5555


This is a brute force example of using a regular expression in vi. There are much more elegant ways to write this same expression that would more exactly match on the strings i have used as an example. For more information on using regular expressions in VI, O'Reilly has two good books that I highly recommend. The first is

"Learning the vi Editor" and the second is "Mastering Regular Expressions".


Have fun.




nice one thanks P38 !!


keep up the good work :)






Not to be picky.. but this is 'vim' that your talking about... not 'vi' :P


However.. wow P38.. I've been using vim ever since I stepped into Linux.. and I learnt something..... :P


Forum Jump:

Users browsing this thread: 1 Guest(s)