BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Using the vi Editor
Pages: 1, 2

One of the neatest things about the vi editor is that you can run commands without leaving vi and even save the command's output within your vi session. Let's say I'm typing along in my vi session and I need to see this month's calendar. I can type:

:! cal

Don't panic when you see this month's calendar appear at the bottom of your screen instead of your file. Once you're finished looking at the calendar, you can press any key to see your file again.

To insert this month's calendar into your file, place your cursor where you want it to appear, and type:

:!! cal

If you forget to have your cursor in the right place and end up inserting cal's output in the wrong spot, don't forget that u will undo your change so you can try again.

The ! command will run any command and !! will insert that command's output into your current file. To insert the contents of another file, try:

:!! cat /etc/fstab

The vi editor also has powerful cut-and-paste abilities. When you are in a vi session, you have the equivalent of 26 (for the 26 alphabet characters) clipboards available for your use. The vi editor also automagically stores your last nine deletions for you in separate buffers labeled from 1-9. You can save yourself a lot of typing if you remember to use these buffers.

There are only four letters one has to remember to do any cut-and-paste operation:

m to mark the desired text
d or dd to cut the text
y to yank (copy) the text
p to paste the text

When you're first learning to cut and paste, it is handy to have set your showmode as you have to be in Command mode to enter the cut-and-paste commands. You'll always be able to check you are in the right mode as the word "*Command" will appear in the bottom right corner of your screen. It's a good idea to practice on a file that is not important to you, but don't despair if you really muck up your file as you can always exit the vi editor without saving any of your changes with :q!.

Let's try some cut-and-paste operations. Open up or create a file that contains a couple of paragraphs worth of text. Move your cursor to the first line of a paragraph, and we'll manipulate the lines in that paragraph. My paragraph has six lines in it; to cut out those six lines, I'll type from command mode:


You'll note that this operation was silently effective, the lines just disappeared. If I type u, the lines reappear, if I type u again, they re-disappear as those lines are stored in vi's buffer. Because they were my last deletion, they are in buffer number one.

To paste those lines, I'll put my cursor somewhere else in the file, and type:


The quotation mark tells vi to access its buffers; the 1 says I want the number one buffer (the last deletion); and the p says I want to paste those contents at the current cursor position. Again, this is silent, the lines magically appear. If my cursor was in the wrong spot, I can undo the changes with u, move the cursor and try again.

If you forget which buffer your deleted text is in, start with pasting the first buffer, undo that change with u, then repeat the command with the second buffer using . like this:


until you find your text.

You can cycle through all nine buffers this way.

Now let's try copying (yanking) text instead of deleting it. Because the numerical buffers only store deleted text, you instead want to save your copied text to one of the alphabetical clipboards. You can use any clipboard you want, you just have to tell vi which one you want to use.

I'll move to a different paragraph and yank (copy) three of its lines to clipboard letter a like so:


You'll note that the lines didn't disappear this time. To see if it worked, I'll move my cursor to a different part of the file and paste in the contents of the a buffer:


Again, I told vi to access a buffer named a and to paste its contents at the current cursor position.

The contents of a will remain there until I end my vi session. If I want to copy more text, I'll probably send it to clipboard b and so on until I've cycled through all 26 clipboards.

Another way to copy and paste text is to mark the specific text. Move your cursor to any postion in your file; I'll go to the middle of a paragraph and type:


to start a mark named a. Now move your cursor to another position in your file; I'll go down to the end of a line a couple of paragraphs down and type:


This copies all of the marked text between the initial and ending cursor position to an unnamed buffer. Note that unnamed buffers are referred to by ', whereas named buffers are referred to as ". The bottom of my screen noted that I yanked 14 lines. I'll then move to another part of my file and paste it like so:


Now, let's get really fancy by copying the contents of our buffers into another file. Don't exit vi by using the q command or you will erase its buffers. In fact, you can tell vi to open up another file without leaving your current screen by using the e command like so:

:e newfile

If you've made any changes to your current file, vi will complain and tell you to save them first. If you don't want to save your changes, you can still enter the second file if you type:

:e! newfile

You'll note that vi will create an empty file called newfile. If I now type:


the contents of that unnamed buffer will be pasted into this new file. I can also copy in the contents of any named buffer by specifying its letter like so:


And I can even copy in a deletion from the previous file:


Related Reading

UNIX Power ToolsUNIX Power Tools
By Jerry Peek, Tim O'Reilly & Mike Loukides
Table of Contents
Full Description

If you want to get back to the first file, repeat the e command and specify that file's name. No matter what file you are in, if you type q, you will exit vi and lose your buffers.

There are other ways to get text from one file to another using vi. Let's say I'm working in a large file and I want to copy (write) lines 100 to 125 to a new file. This command will do it:

:100,125w newfile

To see the results, I can open up newfile like so:

:e newfile

Or, if I want to send line 23 to the end of existing_file:

:23w >> existing_file

I've barely scratched the surface of what you can do in vi, but hopefully I've piqued your interest in this editor. Besides UGU and Steve Moritsugu's book, you'll also find more vi tips in "Unix Power Tools" (ISBN 1-56592-260-3). And Garrett Hildebrand's vi Page is definitely worth checking out.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.

Read more FreeBSD Basics columns.

Return to the BSD DevCenter.

Sponsored by: