My rediscovery of vi Oracle Headquarters Redwood Shores1 e1698667100526

My rediscovery of vi

I am sure I am one of those humans that are using something for a long long time, and suddenly find out they have been doing things in a not so optimal way… One easily gets used to a personal style of working with (software) tools or devices. Nothing wrong with that (as long as you achieve what you want or need), but sometimes it can be useful to read the manual to find out how the designers meant it to be.

While cleaning up my office at home I bumped into a reference manual for vi. I am a frequent vi-user, so I thought, let’s see if vi holds some secrets for me still (the manual was about 50 pages. "Hmmm, does vi hold so much functionality to fill a 50 page manual? I am sure there is something to learn here…). Not that I am an advanced vi user, like – I reckon – most of us are. I usually don’t go any further than the basic editing functionalities. So, normally, I do get around pretty well in vi, but I found some very interesting information, especially for developers / system administrators.

The manual pointed me again to the nice to know facts of ^ being the first character of the line for search/replace operations ($ is the end, but I already had that in my ‘system’. I knew about ^, but had kind of forgotten it). Another one that I wasn’t aware of is that you can not only copy/paste (yy/p) but it is also possible to cut and paste. To cut/paste a number of lines, enter d4d (to cut 4 lines) and paste them with p/P. All in all, the majority of the manual was already known to me, of which some of the information had to be refreshed.

Here is a couple of my findings:

Opening files in vi

vi +$ will open up the file and automatically put the cursor on the last line of the file. To be more precise: you can put any command after the + while opening a file to edit: vi +10 will open the file and put the cursor on the 10th line. vi +/sometext/ will open the file and put the cursor on the first line that contains "sometext" (trivial, I know, but it is fun to play around with).

EX command ‘r’

You can copy the contents of another file into any location of the file you are editing (independent of the cursor-position): To do this, type ‘:r ‘. This will read the file specified and put it at the location of the cursor. To put the contents of this file to a specific location (e.g. line 25), type ‘:25r ‘. You can also insert the output of a specific OS command into the file you are editing: ‘:r !ls’ will insert the output of the unix command ‘ls’ into the file at the current cursor position.

Sorting through a filter

to sort the 3 next lines from the current cursor position (which is considered as line 1), enter !2jsort When you press !, nothing seems to happen. Actually, nothing will seem to happen unless you have entered a cursor-move command (2j, in this case, which means 2 lines down from the current position). Only then the ! will appear at the bottom of your screen, and you will be able to enter your command. The unix command sort will take the three lines as input, sort them and the output is sent back to vi, which puts it all back at the appropriate lines.


Although I am not a developer, I think this one is especially useful for developers (seriously!): When developing code in vi, one can easily become confused by the number of opening/closing brackets e.g. (, ), [, ], { and }. It is very easy to determine the corresponding closing bracket for an opening bracket, by placing the cursor on the opening bracket and pressing %. The cursor then moves to the corresponding closing bracket. When the cursor moves to a location you didn’t expect, you might have missed one or more brackets. The same applies for finding an opening bracket for a closing bracket. Simply put your cursor on the closing bracket, press % and the cursor will move backwards to the corresponding opening bracket. It also works when you have nested loops/procedures. Can you imagine the usefulness of this while checking a load balanced JDBC url referring to a RAC database with 4 servers?


Using Tags to easily find procedures and functions

When you have multiple files in a directory (e.g. .pls files or .sql files) it is also easy to forget where you put what procedure in which file. An easy to use instrument for this is tags.

I have a directory /development/scripts that holds all of my .pls scripts

cd /development/scripts

here, I enter the following command:

ctags *.pls

This will create a tagfile for me with information what to find in which file and on which location in the file. Now, I have a procedure called DispPageDialog, and I wish to edit it. I just have to type the following command:

vi -t DispPageDialog

and vi will automatically open the correct file for me and put the cursor at the beginning of my procedure called DispPageDialog.

Ain’t that nice!?

Advanced search and replace

This is in the category "How-to-spend-your-time-when-you-have-absolutely-nothing-else-to-do", but it is fun anyway. It can be useful, but only on very rare occasions, so make sure you learn the next command by heart;-)!

How to change

01011972 Johnson, Alan 
04191980 Farmer, John 
12061543 Nicholas, Saint 
09061940 Freeman, Arthur 
06262000 Roth, Aline 
08092003 Roth, Job 
03202006 Roth, Thomas 


Alan Johnson was born on 01-01 in the year 1972 
John Farmer was born on 19-04 in the year 1980 
Saint Nicholas was born on 06-12 in the year 1543 
Arthur Freeman was born on 06-09 in the year 1940 
Aline Roth was born on 26-06 in the year 2000 
Job Roth was born on 09-08 in the year 2003 
Thomas Roth was born on 20-03 in the year 2006 

To do this, enter the following command (yeah right!:-):

:1,$s#\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9][0-9][0-9]\) \(.*\),\ \(.*\)#\5 \4 was born on \2-\1 in the year \3#

If you have more tricks or tips for using vi, you are welcome to share them here.


  1. Ruben July 8, 2007
  2. Marco Gralike July 8, 2007
  3. Patrick Sinke July 7, 2007
  4. Bruce Snyder July 7, 2007