UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
Through the years, the command structure of vi has been applied to many applications and programs. If you do not know how to move around in vi you might have difficulties using some applications.
Have you ever sent mail the old-fashioned way using the original mail command that can be found in /usr/bsd/mail, /usr/ucb/mail, /bin/mail, or /usr/sbin/mail. Although mail accepts data through STDIN, it can be used in conjunction with vi for sending mail.
Flavors: AT&T, BSD
Shells: All
Syntax:
mail address
There are a couple ways that a file created with vi can be sent through the mail program. You can use vi directly within the mail program or redirect the data into mail. Both are simple to use and make sendmail mail faster then using the GUI mail programs that are on the market today.
The process of using the vi editor from within the mail program is an easy one. When you address an email to a recipient, the mail program reads text from standard input. However, there is a command structure that the mail program accepts. One of the commands puts you into a vi session ( ~v). While in the vi session, you can use all the commands and functions that the vi editor has to offer. When you are finished typing, you must write and quit ( wq!) your session for the mail program to consider the completed vi session as part of standard in. When you have left the vi session, you are dropped back to where the program reads only standard input. Type a Ctrl-d or a period ( .) on a line by itself and your mail is sent.
% mail foo@ugu.com Subject: This is a test ~v create letter in vi :wq! <ctrl-d> Mail is sent.
The only thing keeping this from working properly is if your terminal emulation is set incorrectly. If so, the vi session does not perform as it is intended. There are some who don't like to use the mail program at all. They avoid it at all costs. If this is the case, another method is available.
Syntax:
:!mail address < %
You can mail the actual file from within the vi editor itself. Simply issue a mail command from the command line mode to send the file you are working on to a recipient.
Open a vi session:
% vi /tmp/letter
Create the letter to be sent. When you are finished, write the file, but don't quit. Go to the command line mode and redirect the file through the mail program:
:!mail foo@ugu.com < %
The command reads Execute ( !) and send mail containing the current file ( %) to the address foo@ugu.com. This is also a quick way to send mail to someone without the lag time of loading windows, loading a mail client, opening a message, attaching a file, and clicking the Send button. You can judge for yourself which method is faster.
Some of the newer flavors of UNIX apply vi commands to the man pages when you view them. The older flavors do not have this capability. Viewing the man pages without all the escape sequences that are embedded within a man page is possible if you use the right command.
Flavors: AT&T, BSD
Shells: All
Syntax:
man - ManPage | col -b > file; vi file
A command to strip out the control characters can be applied straight from a shell, an alias in your startup shell, or directly from within vi. If you don't filter the control characters when you bring a man page straight in, the file might play havoc with your terminal emulation. If you're lucky, it will only make it illegible.
clear(1) User Commands clear(1) NAME clear - clear the terminal screen SYNOPSIS _^Hc^Hc^Hc^Hc_^Hl^Hl^Hl^Hl_^He^He^He^He_^Ha^Ha^Ha^Ha_^Hr^Hr^Hr^Hr DESCRIPTION _^Hc^Hc^Hc^Hc_^Hl^Hl^Hl^Hl_^He^He^He^He_^Ha^Ha^Ha^Ha__^Hr^Hr^Hr^Hr clears your screen if this is possible. It looks in the environment for the terminal type and then in the terminfo database to figure out how to clear the screen.
To make this work, use the col command to filter out the control characters
from the man pages and send it to a file. When written, edit the file with vi.
% man - clear | col -b > /tmp/man.txt; vi /tmp/man.txt
It would be easier to set up an alias called viman in one of your login scripts. Then all you have to do is type a simple command and the man page is brought straight into a vi editor for you.
alias viman 'man - \8* | col -b > /tmp/man.txt; vi /tmp/man.txt% viman clears
If you're already in vi and want to bring a man page into the file you are working on, you can do this from the command line mode with a similar command:
:r8man - clear | col -b > %
Read (r) in the results of the command executed (!) from the man page of clear, which is filtered through the col command and sent to the current file that is being edited with the current version of vi.
It is often that you as an administrator view files ranging in size from the smallest one-line configuration files to files excessively large. These files, when brought in to the vi editor, can take awhile to allocate memory and swap just to view them. In any of these cases, it is nice to be able to view the file with the more command and move forward and backward in the file. On some of the newer flavors of UNIX, this is becoming possible. What if you want to edit the file while you are looking at it with more? The more command offers that capability depending on how you use the command.
While looking at a file with the more command press the letter v. This sends the file directly into the vi editor. Not only are you able to move around forward and backward and search for patterns, but if you have write permissions to the file, you can write out your changes if want to keep the changes you made.
% more /etc/hosts
Pressing the letter v and going into vi works only when the file is directly accessed by the more program. As you exit the vi session, the same screen that was displayed when you went into the editor is redisplayed.
If you try to pipe ( |) a file or the output of a command to the program more, you will not be able go into the editor with this method. It will not work.
% cat /etc/hosts | more
The Korn shell, like other shells, has the ability to maintain a history of commands that have been previously executed. The main difference with the Korn shell, however, is that it enables you to use all the same keys to move around, perform simple searches, and switch between command and insert modes, as in the vi editor.
Inside the Korn shell are options that can be set up; one of them is the vi option to put the shell into edit mode.
$ set -o vi
With this option set to On ( -o), the command line editing at the shell prompt can take place. To enter the vi edit mode from the Korn shell, simply press the Esc key. When this is finished, the following keystroke commands are accessible:
When you are finished and you can press the Enter key to execute the command that is on the current command line. As you learn and get comfortable with the commands, you can do away with typing redundant commands.
There are many reasons to learn vi. You never know when or where it will pop up. The editor commands are universally accepted in all flavors of UNIX and provide programmers with more than enough reasons to use them in applications they develop.
I recently ran into a candidate for a position I had open. In casual conversation, I asked him his editor of choice. He responded with, " vi, always". I tried to play dumb about some basic commands to see how fluent he truly was in the editor, out of curiosity. When I asked how to move the cursor around he was ignorant of any of the commands. I was shocked. He quickly changed the subject to shells and how he liked to work in the Korn shell. When I asked why, he responded, "I like the fact that I can move around, search, delete, and make changes to past commands". Now I was really confused. He knew all the vi commands to process the Korn shell history off the command prompt, but didn't know vi. Remember, know the basic operation and commands of vi--your job could depend on it!
UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.