UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
The vi editor is more powerful and less complicated than many people imagine. Often just mentioning vi makes people shudder and cringe. If you do know the editor, don't be surprised if you hear from people, "Eww, you know vi!" I strongly believe it is plain ignorance, because the complexity of the editor is extremely overrated. However, it is not without some difficulty--if you dive deeply into the powerful features of vi, the commands to manipulate the data can be a bit cryptic and overwhelming.
I tend to believe that people can become so lazy or spoiled by GUIs and mouse-based editors that they fail to educate themselves in the finer points of a text-based editor such as vi. They learn that using the mouse to drag-and-drop or cut-and-paste the data in the file is faster and easier. The reality for me is that it is slower. Moving the cursor around through keyboard commands can be faster.
Syntax:
vi [-r] [filename]
To bring the editor up, use the command vi followed by an optional filename. Always make a copy or backup of any system files if you modify them, in case something gets corrupted or if you plan to make a lot of changes to the file.
When the editor opens a file, you are immediately put into command mode. From command mode, you can manipulate the text in a variety of ways:
Manipulate the position of the cursor
Page through the file
Cut-and-paste text throughout the file
Delete characters, words, or lines
Write and quit the file
If you ever feel lost within the editor, pressing the Esc key will always return you to command mode. See section 8.4, "Configuring vi Parameters," for information on how to set showmode to display the modes as you toggle between the two. W
In command mode, you have the ability to move the cursor around in the file. The four keys you use for this are grouped around the standard typing formation when the right hand rests on the keyboard ( h, j, k, and l).
The keys are case-sensitive and must be lowercase. Uppercase keys perform functions different from their lowercase counterparts. If the keymapping for the terminal you use is configured properly you can use the up, down, left, and right arrows to move the cursor. However, it is a bad habit to rely exclusively on these keys to move your cursor around. As you work on more terminals and various systems, you'll find that the terminal emulation and keymapping are not always compatible with the functions you are attempting to perform.
When you're in command mode and want to move the cursor to the position where you want to insert text, you have several options for entering insert mode. One of them is simply typing the letter i. Here is a list of the basic insertion keys:
When you finish typing your text, switch back into command mode by pressing the Esc key.
Deleting text is done while in command mode. There are several ways that text can be deleted. You can delete individual characters, words, or complete lines:
The vi editor also allows for the deletion of multiple characters, words, and lines at once. Place a number before the type of deletion you want; the command deletes n number of times. If you enter 4x, the command deletes four characters from the line. Take the following phrase and assume that the cursor is currently positioned at the beginning of the first line.
The quick brown fox jumped over the NT system, Finding a UNIX file server on the other side, And lived happily ever after.
In command mode, if you type 4x, the result is that the first word and space ("The ") are deleted.
quick brown fox jumped over the NT system, Finding a UNIX file server on the other side, And lived happily ever after.
If you type the characters 3dw, the next three words ("quick brown fox ") are deleted.
jumped over the NT system, Finding a UNIX file server on the other side, He ended up living happily ever after.
If you type 2dd in command mode, the top two lines are deleted:
He ended up living happily ever after.
Like nearly anything else in UNIX and vi, there is more than one way to do the same task. Exiting a file is no different:
If the file already exists and will be overwritten, use either command to save the file and exit the editor:
So which should you use :x, :wq, or ZZ? ZZ is the quickest way to save the file and exit. A problem I have discovered in using the :x option is that while holding down the Shift key for the colon I would type the x key. As some might realize, this actually can encrypt the file. If the file was to get saved, the original file is now encrypted and the previous version is lost. I personally like knowing that, every time I quit, the file will be written to, which is why I prefer to use the :wq command.
Two commands quit the vi editor without saving any information out to the file:
:q--Quits the vi editor, unless the buffer has been changed. It has been changed, and you are placed back into the command mode of vi.
:q!--Forces a quit without writing any information out to a file.
If you lose your window manager and are dropped into a run level where your existing editors don't work, what can you do then to modify the system files? If you are not fluent in the basic UNIX editors such as vi, how do you comment out a section of a file? How do you delete entries from a file? How do you insert data before and after specific lines of code? How do you do it all smoothly and painlessly? Without ed and vi, you can find yourself in trouble.
When a system is down, users and management are counting the seconds. Remember, too, the larger the system, the longer the boot-up process. System administrators need to get in, fix the problem, and get the system back up and running in the shortest amount of time. Not being able to modify files when you most need to is not a good thing!
If you have a window manager running, open a window with a shell and use the vi editor for simple, everyday tasks in order to get used to typing and moving around. Create a daily "Things To Do" task list, create a phone or address book, or log phone calls from your users. Do what you can to get yourself addicted to the editor.
Man pages:
vi
World Wide Web:
vi Reference-- http://www.cs.wustl.edu/~jxh/vi.html
VI Advanced FAQ-- http://ie.www.ecn.purdue.edu/~kompella/html/vi_faq2.html
VI Powered-- http://www.darryl.com/vi.shtml
UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.