|
Grasping the vi editorNew to Unix? Need a refresher? This is your reference guide to basic, yet crucial vi commands needed for quick document navigation |
The first of this two-part series on the vi editing tool covers the basic commands required to move around a file. Learn how to move in the absence of arrow and page keys and how to control where exactly you want to go -- whether it be six lines down the page or just to the end of a sentence -- all while marking where you've been. (1,600 words)
Mail this article to a friend |
Welcome to Unix 101. As the title of the column implies, this is an introductory series. In this new monthly column I will attempt to cover key Unix utilities, commands, and concepts in a way that will be helpful to the newer user. And although it is a tough balance to strike, I will also try to present information that is useful to the advanced user. Even advanced users can always benefit from quick reminders, right?
Our opening shot is at vi. In this column I will show you some of the basic editing commands and shortcuts available in the editor for moving around. In a follow-up piece next month I will look at changing vi's behavior with the set command and cut and paste operations. Future articles I have planned revolve around sed, ls, awk, and shell script menus, among others. We may come back to vi in a later column as there are even more features buried in it besides the ones we're starting with here. I would like to hear from you on any Unix subject or command you'd like to see covered in an article.
The Unix default vi editor is often maligned and cursed -- and this is by people who like it. Those who don't like it cannot be quoted. Vi is available on any Unix system unless someone has removed it, and most of those who use vi use only a fraction of what it is capable of doing.
It is time to tame this beast and introduce you to some of its more useful features. I'm not promising that you'll end up loving vi, but you may grow to appreciate some of its features. This article assumes you already have some familiarity with vi.
Different methods of starting vi allow you some options that may speed up your editing job. The most useful option is to open a file and immediately position to a word in the file.
vi +/sasquatch letter.txt
This command opens the file, letter.txt, and positions to the first line containing the word "sasquatch."
You can also position to a phrase of more than one word by enclosing it in quotes as in the following example:
vi +/"and the dish away with" letter.txt
|
|
|
|
Learning alternatives in the absence of
certain keys
Once a file is open, everything you can do with vi can be done on a standard alphanumeric keyboard that includes a Control key and an Escape key. There are keys and control sequences that can replace the arrow keys or page up and down keys. This leads us to a handy feature of vi that overcomes some problems with remote log ins.
Frequently the software used for a remote log in to your main computer is set to emulate a different terminal type, or the software is incorrectly configured so that you lose some of your keys. Commonly you lose the arrow keys and page up and page down keys. I have seen several different configurations in which you end up with the arrow keys but not the page up, page down keys, or vice versa. This can make editing difficult (if not impossible) unless you know your alternatives. You can correct all this by defining your TERM variable; or if you are using communications software on a PC, you can usually re-map your keyboard to send the correct character sequences to vi when the arrow and other keys are pressed. Either of these two options are necessary for frequent log ins. When this is only an occasional problem, however, there is no need to struggle through termcap, terminfo, or your PC telecommunications configuration and software manual. Instead you can mimic the behavior of keys when vi is in command mode.
Vi is always in one of three modes -- command mode, ex escape mode, or insert mode. Command mode is the start-up mode of vi, ex escape mode begins when you enter a command that starts with a colon, and insert mode starts once you have pressed i, a, o (or the uppercase versions) for insert, append, or open line mode, or one of the change text keys. Once you are in insert mode, what you type on the keyboard is added to your editing buffer until you press Escape. These are the changes you see displayed on the screen as you type.
The usual process of editing involves moving around in the document while in command mode and using arrow keys and page keys, and then switching into one of the insert modes to make the changes.
In command mode, the alternatives to the page keys are CONTROL-U and CONTROL-D (hold down the control key and press U or D simultaneously). CONTROL-U will produce a half-page up, and CONTROL-D will produce a half-page down of the screen. CONTROL-B and CONTROL-F will move you backward or forward one screen. These four keys eliminate the need for page up and page down keys (previous and next on some keyboards).
The arrow keys can be replaced by the single characters h, j, k and l which will produce single cursor position movements as follows:
The use of numbers
Whether you are using arrow keys, or the letters, the single step
movement keys can be usefully modified by adding a number prior to the
key. 6j or 6 down arrow, will move the cursor down 6 lines, and 11h or
11 left arrow will move the cursor 11 characters to the left.
These keys only work in command mode. Typing 15h in insert mode will just place the characters "15h" in your document.
There are many additional commands for moving around the document in command mode.
A $ moves the cursor to the end of the current line, and ^ moves it to the beginning of the current line. The $ can be preceded by a count. The $ command assumes a count of 1, so $ and 1$ are equivalent. 5$ will move the cursor to the end of the fifth line from where you are, counting the current line as 1.
The ^ actually moves the cursor to the first non-space character in the line. A 0 (zero) will move the cursor all the way to the left. Counts do not work with ^ and 0.
You can also skip around in larger chunks. The w character will move the cursor to the beginning of the next word, and the b character will move it to the beginning of the current word if the cursor is within a word, or the previous word if it not. Both w and b work with counts, 6w will move you forward 6 words and so on.
Moving by sentences and paragraphs and
marking your spot
You can also move by sentences although I have a personal problem with vi's definition of a sentence. The left parenthesis, (, will move the cursor to the beginning of a sentence, and ) to the end. A sentence is defined as one or more words ending with a period (.), exclamation point (!) or question mark (?) and followed by two spaces. I find this definition of a sentence awkward as few editors require it, and some even consider it old fashioned. One space should be enough, but vi is set in its ways.
There are also paragraph movement keys. Paragraphs are groups of text delimited by blank lines. A { moves to the beginning of a paragraph, and a } moves you to the end of a paragraph.
These movement keys alone would be quite powerful, but they can additionally be modified with counts. Entering 3{ will move the cursor to the beginning of the third paragraph forward from the current cursor position.
The ex escape mode provides some other useful movement commands. You can move directly to a line in a text file by typing a colon followed by the line number of the line you want to go to.
For example, the command :16 will move the cursor to line 16. You can also move to a line offset from your current location. The command :+5 will move you down five lines in the text, and :-2 will move you back two lines.
The :$ (colon followed by a dollar sign) command can be used to jump to the end of the file and :1 will move you to the top of the file. Remember $ moves you to the end of the line and :$ moves you to the end of the file.
It's also possible to move back to a position you've marked. Mark a position in your document in command mode by placing the cursor at the position to mark. Type m followed by a lower case letter a-z. The letters represent 26 marks that can be placed in the document. For example, entering mb will mark the current cursor position as b. The mark does not show up in your edited text, but is hidden there in the edit buffer for later use in that same editing session. The mark is lost when you close the file and exit vi.
You can return to your mark by typing a single quote (') followed by the letter, or back single quote (`) followed by the letter. The 'b version returns you to the beginning of the line containing the mark b. The `b version will move the cursor to the exact position of the b mark. When you are reviewing two or more areas of a text file, simply mark them and flip back and forth using the single quote.
You should be able to race around the vi screen quite quickly now.
In the next article I will cover changing the behavior of vi using the set command and cut and paste.
|
Resources
About the author
Mo Budlong is president of King Computer Services, Inc. and has been involved in Unix development on Sun and other platforms for over 15 years.
Reach Mo at mo.budlong@sunworld.com.
If you have technical problems with this magazine, contact webmaster@sunworld.com
URL: http://www.sunworld.com/swol-02-1997/swol-02-unix101.html
Last modified: