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:
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:
If you forget to have your cursor in the right place and end up
cal's output in the wrong spot, don't forget that
u will undo your
change so you can try again.
! command will run any command and
!! will insert that
command's output into your current file. To insert the contents of another file,
:!! 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:
||to mark the desired text|
||to cut the text|
||to yank (copy) the text|
||to paste the text|
When you're first learning to cut and paste, it is handy to have set
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
"*Command" will appear in the bottom right corner of your screen. It's
idea to practice on a file that is not important to you, but don't
you really muck up your file as you can always exit the vi editor
without saving any of your changes with
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
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
I want the number one buffer (the last deletion);
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
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
', 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:
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:
You'll note that vi will create an empty file called
newfile. If I
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:
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:
To see the results, I can open up
newfile like so:
Or, if I want to send line 23 to the end of
: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.