vi tricks part one

Vi is a very powerful text editor. The only hard part is to remember the command keys and combinations. I will try to explain some of the advanced futures of vi commands.

First of all some key and must know commans of vi.

 ^ or 0 - go start of line

$ - go end of line

i - insert

dd - delete line

x - delete char

o - append line

yy - copy line

p - paste line

G - go end of  text document

~ - lower the upper case character or upper to lower one

dw - delete word

:# - go to give line number

u - undo

ctrl-g - give information like current line and total number of lines in a document

It's possible to run a command more than once.

For example, 2dd command deletes 2 lines, or 5yy copies 5 lines.

Also, capital and lower letters has different meanings.

eg. o - appends line below the current line, but capital O inserts a line above the current one.

Okay, that's enough  for simple vi commands, now we can explain some very handy vi tricks.


Search and Replace

for searching and entry simple give / command and the word to search.

if you want to search hello word all you have to do is


that's all.


 suppose you want to replace all "hello" worlds to "bye" words in text document.


here 1 is the starting line of the document and $ means the last line of documents, simply number

of lines of the document. s means search /hello/ is the word to search and /bye/ is the one which will

replace the searched term. g at the end of the commands means to globally search and replace all the

matching words and not stop with first match. Without g the command will replace the first occuring match

and stopped.

If you will like to be asked on each replace operation you just add "c" character after g command.


Sorting Text

:1,$!sort -r

this command will call any give external command and pass give lines as input to it.

in this example from 1st line to last one ($) and gets the result and replace all lines with it.

this example will simply sort all lines in reverse order.


Replacing text with external commands

like previous example in sort, it's possible to give another program and get  it's input as text document data.

for example


will get current directory listing and put it into vi text editor.

you can also give line ranges like above example sort.