UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
Like a personal alias table of abbreviated commands that you can set up for your working shell, vi enables similar capabilities from either the .exrc file or manually from the command-line mode.
Flavors: AT&T, BSD
Shells: All
Syntax:
ab name definition
As you work in the insert mode of the vi editor and you finish typing one of the abbreviated words you have declared in your list, the abbreviated word switches to the definition you have set up in your abbreviation table.
Place your definitions into the .exrc file so they will always load when you start up the vi editor. If you want to set up a definition to replace the letters myadd with the cyour current email address, the definition is simple:
" This abbreviation is for Anthony's email address ab athaddr anthony.t.howard@seattle.west.ugu.com
When the word athaddr is typed in the insert mode, the email address anthony.t.howard@seattle.west.ugu.com is displayed. The uses of abbreviations are almost endless, ranging from programming to administration, authoring HTML files, correcting basic typographical errors, and saving the time it takes typing long words. There are, however, some basic rules that you need to be aware of when you create the abbreviations:
Remove all empty and blank lines from the .exrc file.
Do not begin abbreviations with a number.
When these rules are not followed the .exrc file breaks at the last line that was valid. After that line, the remaining lines of the file are never accessed or read by vi.
Flavors: AT&T, BSD
Shells: sh, ksh
Defining various abbreviations can help to write your code more quickly if you know where and when to use your definitions. Here is a list of sample entries that might work very well for you.
ab psh #! /bin/sh ab pksh #! /bin/ksh ab pperl #! /usr/local/bin/perl ab wloop while [ 1 ]; do ab wread while read line; do ab ffiles find . -print -type f ab dir for LIST in `ls -1`; do ab ex exit ab dn done ab nroot if [ `whoami` -ne "root" ]; then ab yroot if [ `whoami` -eq "root" ]; then ab eroot echo; "ERROR: You must be root to run this"; echo ab ctape tar -cvf /dev/tape ab rtape tar -xvf /dev/tape ab c750 chmod 750 ab croot chown root
A script can then be created quickly by applying the abbreviated definitions. Can you figure out what this script does?
# vi change_perm
psh # Define the shell yroot # Test if we are root eroot # Inform that must be root ex # Exit fi ffiles | wread # find and process just files echo $line # display the name of the file c750 $line # change the permissions of the file croot $line # change the ownership of the file dn ex # Exit the program
Although this does look nice and cryptic at first glance, when you are typing the abbreviations out, it will all make sense to you.
#! /bin/sh if [ `whoami` -ne "root" ]; then echo; "ERROR: You must be ROOT to run this"; echo exit fi find . -print -type f | while read line; do echo $line chmod 750 $line chown root $line done exit
The fully unabbreviated version of the script is almost three times the size of the abbreviated version. If you set up the abbreviations and get into the habit of using them, you could increase your time almost threefold. For this sample script alone, the abbreviated version has only 67 characters. If you were to type the entire script without using a single abbreviation, you would type 167 characters.
You, like most programmers, probably have a certain set of variable name conventions that you like to follow. These can also be abbreviated.
ab tot0 $TOTAL=0; ab tot $TOTAL ab zero0 $ZERO=0; ab max100 $MAXCNT=100; ab max $MAXCNT ab zero $ZERO ab tpath $TMP="/tmp"; ab tmp $TMP ab ofile $OUTFILE="/usr2/data/cnt.dbasefile";
The variables can then be applied to everyday programming definitions that you run into.
ab floop for ($CNT = zero ; $CNT < max; $CNT++) ab openo open (OUTPUT, "> tmp/ofile "); ab po print OUTPUT ab cl close
The following example demonstrates how a script can be created in Perl. It also shows how you can make use of embedded abbreviations. Take a look at the floop and openo definitions. The abbreviated words zero, max, tmp, and ofile all reference a preexisting abbreviation that has already been defined.
pperl # Define the shell zero tot0 max100 # Set values to the variables tpath # Set the tmp path ofilep # Output Patch floop # Loop until we reach the max { tot = tot + $CNT } # add up the numbers 1-100 openo # open a file for writing { po tot } # Write the results to the file cl # Close the file
This is a simple script that adds up the numbers from 1 to 100 and writes the total out to a file. As a result of the embedded definitions, you can see that the four variables, $ZERO, $MAXCNT, $TMP, and $OUTFILE are all the intended variables to be used.
#! /usr/local/bin/perl $ZERO=0; $TOTAL=0; $MAXCNT=100; $TMP="/$TMP"; $OUTFILE="/usr2/data/dbasefile"; for ($CNT = $ZERO ; $CNT < $MAXCNT ; $CNT++) { $TOTAL = $TOTAL + $CNT } open (OUTPUT, "> $TMP/$OUTFILE "); { print OUTPUT $TOTAL } close
This entire nonabbreviated script totals 204 characters. If all the abbreviated words were used instead, you would have to type only 63 characters. Writing in abbreviated words really has the potential of being more widely used than it already is, if properly exploited.
Flavors: AT&T, BSD
Shells: All
In the world of administration, you can apply similar rule sets for the abbreviations to help in your daily tasks and duties that you perform. You work in many configuration files throughout the system. You can use the abbreviations to handle some redundant typing that takes place in the host table, DNS table, resolving DNS, the password file, notification entries, and basic system information.
These two abbreviations will enable you make quicker entries into the /etc/hosts table.
ab sub10 134.129.10 ab wdom west.ugu.com
Typing sub10 followed by a period ( .) causes the domain and subnet to appear. All you have to do is add the last field of the IP address for the node. If you want to add a hostname entry with a domain name attached to it, after the hostname is entered type a period ( .) wdom and the domain is appended to the end of the entry.
Type this in vi:
sub10.22 pluto pluto.wdom sub10.40 mars mars.wdom
The result is
134.129.10.22 pluto pluto.west.ugu.com 134.129.10.41 mars mars.west.ugu.com
In configuring DNS, a number of modifications are taking place. You create abbreviations that can be applied to the named.hosts file.
ab seadom seattle.west.ugu.com ab sub10 134.129.10 ab ina IN A ab cname IN CNAME an mx IN MX 5 mars.seadom.
By typing seadom, ina, sub10, and the node number for the system, you can achieve a DNS entry. Conatical names too can be configured as well as MX records to get you through the file in the shortest amount of time.
Type this in vi:
seadom. ina sub10.22 pluto cname seadom seadom. mx
The result is
seattle.west.ugu.com. IN A 134.129.10.22 pluto CNAME A seattle.west.ugu.com seattle.west.ugu.com. IN MX 5 mars.seattle.west.ugu.com
As new systems are added to the environment the DNS, NIS, and domain will typically not change. You can hard code the hostname resolver information into the .exrc file for the /etc/resolv.conf file:
ab order hostresorder nis bind local ab dom domain dev.foo.com ab dns1 nameserver 134.129.19.254 ab dns2 nameserver 134.129.70.254
As you see it is really straightforward. Type four simple words, order, dom, dns1 and dns2.
Type this in vi:
order dom dns1 dns2
The result is
hostresorder nis bind local domain dev.foo.com nameserver 134.129.19.254 nameserver 134.129.70.254
When multiple groups exist in the password file /etc/passwd, you can set up an abbreviation for each group that needs a password entry for any new users that need to be added. It works like this:
ab pwddev ID:*:UID:20:NAME:/home/developer/ID:/bin/csh ab pwdsup ID:*:UID:30:NAME:/home/support/ID:/bin/csh ab pwdmgr ID:*:UID:40:NAME:/home/manager/ID:/bin/csh
There are three groups (developers, support people, and managers). When a new support person needs to be added to the system, you only have to type pwdsup. A password entry with the basic information filled-in is generated. All you have to do is perform the vi command change word ( cw) on four fields, User ID, UID, real name, and the home directory. When all the fields are filled in, the password can be updated with the UNIX command passwd. Keep in mind this is for those flavors that are not shadowing passwords.
Scheduled outages can always be taking place, but not that much. When they do you always send out notifications to the users and to everyone in the environment. You can set up predefined abbreviations that can be applied to the /etc/motd file or to a mail message that you create using vi.
ab outag THERE IS A SCHEDULE OUTAGE THAT WILL BE TAKING PLACE AT: ab dfail Due to a Disk Failure that needs replacing. ab mfail Due to bad memory that has to be swapped out. ab pfail Due to a power supply that died and will be replaced. ab rboot A reboot of the system must be performed to fix some problems. ab 15min The system will be down for 15 minutes. ab 30min The system will be down for 30 minutes. ab 1hour The system will be down for 1 hour. ab allday At this time the system will be offline all day. ab QA If there are any QUESTIONS or PROBLEMS always feel free to call: ab sharon Sharon Garnet (x1234) ab todd Todd Crisby (x6789)
With these abbreviations in place, you only have to enter nine words instead of an entire paragraph. It is a real timesaver when you have better things to do than write notifications to users.
outag 6:00pm, Tuesday - 1/5/99 rboot 15min QA sharon
The result is
THERE IS A SCHEDULE OUTAGE THAT WILL BE TAKING PLACE AT: 6:00pm Tuesday, 1/5/99 A reboot of the system must be performed to fix some problems. The system will be down for 15 minutes. If there are any QUESTIONS or PROBLEMS always feel free to call: Sharon Garnet (x1234)
You can configure the .exrc if you want to maintain a listing of the various system information. You can have the information at the touch of the keyboard. In this case every system that provides some type of information begins with the letter I followed by the hostname.
ab Ipluto pluto - Sun Sparc 20 Solaris 2.5 / Serial #531F0677 - 134.129.10.22 ab Imars mars - SGI Impact IRIX 6.2 / Serial #080069075b2d - 134.129.10.41
When you have an abbreviation configured with system information, you can then make embedded calls to existing abbreviations from new ones. You will constantly save time in the end when it comes to accessing and getting the system information you need to know.
ab dns DNS (Ipluto) ab yp NIS/YP Master (Imars)
When you type in vi
dns yp
the result is
DNS (pluto - Sun Sparc 20 Solaris 2.5 / Serial #531F0677 - 134.129.10.22) NIS/YP Master (mars - SGI Origin 2000 IRIX 6.5 / Serial #S34123 - 134.129.10.41)
If you don't need all the serial numbers and operating system versions for each host, you can take the information directly out of the /etc/hosts table pretty easily if you only need the hostname and IP address of each one on your network.
ab IPpluto 134.129.10.22 pluto pluto.seattle.west.ugu.com ab IPmars 134.129.10.41 mars mars.seattle.west.ugu.com
When you type in vi
IPpluto IPmars
the result is
134.129.10.22 pluto pluto.seattle.west.ugu.com 134.129.10.41 mars mars.seattle.west.ugu.com
You can join a couple of commands that help you port the contents of the host table into the .exrc file. Assuming that the /etc/hosts table consists of using the following format of IP address, hostname, hostname.domain, you should not have any problems.
134.129.10.22 pluto pluto.seattle.west.ugu.com 134.129.10.41 mars mars.seattle.west.ugu.com
Run this one-line command against the system's host table to put it the appropriate abbreviated format. Occasionally the second and third fields are swapped in /etc/hosts. If this is the case in your hosts table, swap $2 and $3 around in the awk portion of the command.
% grep -v "#" /etc/hosts | awk 'length > 1 {print "ab IP"$2" "$1" "$2" "$3 }'
This command removes all the commented (#) lines from the system. If the line has a length greater then one character, process the line. Finally create the abbreviated command from the contents of the line.
ab IPlocalhost 127.0.0.1 localhost ab IPpluto 134.129.10.22 pluto pluto.seattle.west.ugu.com ab IPmars 134.129.10.41 mars mars.seattle.west.ugu.com
You now have perfectly formatted abbreviation entries to append to the bottom of the .exrc file.
A great number of people still use the vi editor for the generating Hypertext Markup Language (HTML) files to build Web pages for the Internet. UGU was built entirely from the use of vi. As you know, HTML is text with different types of open and closed embedded commands. For those who don't remember, the beginning concepts of word processors started out the same way.
Because the actual embedded commands have clumsy and time-consuming brackets ( <>), you can abbreviate most of the embedded commands to build the Web pages faster. There are two methods that can be approached.
The first way is to have an abbreviation for the entire HTML command. On the plus side, the entire command is displayed at once; on the downside, you have to go back and edit, insert, and possibly delete text that goes within the embedded commands. This will probably slow a person down.
ab ttl <TITLE> </TITLE> ab bld <b> </B> Ab h2 <H2> </H2> ab cnt <CENTER> </CENTER> ab ital <I> </I> ab href <A HREF=" "> </A> ab img <A IMG SRC=" "> </A> ab tbl <TABLE cellpadding=3 cellspacing=3 border=1> </TABLE> ab tr <TR> </TR> ab td <TD> </TD>
ttl h img tbl
the result is
<TITLE> </TITLE> <H2> </H2> <A IMG SRC=" "> </A> <TABLE cellpadding=3 cellspacing=3 border=1> </TABLE>
The major problem with the method is that you have to go back and insert more commands around the ones you just did. You will in most cases be backtracking. You might be able to find one or two commands that this would be useful for, as the markup language continues to grow.
The other option is to split the HTML command up into two abbreviated definitions. This will mean there will be a definition for each embedded command that is wrapped around a piece of text. This second method enables the most flexible use of the HTML commands. There are only a couple of places that you need to backtrack and insert text. Using the abbreviations does have some limitations.
ab ottl <TITLE> ab cttl </TITLE> ab bdy <body bgcolor="#ffffff" text="#000000" link="#0000ee" vlink="551a8b" alink="ff0000"> ab ocnt <CENTER> ab ccnt </CENTER> ab otbl <TABLE cellpadding=3 cellspacing=3 border=1> ab ctbl </TABLE> ab otr <TR> ab ctr </TR> ab otd <TD> ab ctd </TD> ab ohref <A HREF=" "> ab oimg <A IMG SRC=" " alt="logo"> ab ca </A>
ottl This is a test cttl bdy ocnt THIS IS A TEST ccnt otbl otr otd ohref COLUMN 1 oimg ca ctd otd COLUMN 2 ctd ctr ctbl
the result is
<TITLE> This is a test </TITLE> <body bgcolor="#ffffff" text="#000000" link="#0000ee" vlink="551a8b" alink="ff00 00"> <CENTER> THIS IS A TEST </CENTER> <TABLE cellpadding=3 cellspacing=3 border=1> <tr> <td> <A HREF=" "> COLUMN1 <A IMG SRC=" " alt="logo"> </A> </td> <td> COLUMN 2 </td> </tr> </TABLE>
You should now have an understanding of how the abbreviations can be used in working with HTML. If you are not familiar with HTML, some excellent introductory books can explain the process to you. You can build off this list with your own set of HTML commands if you are familiar enough with them and use them regularly when building Web pages.
Syntax:
ab name :!command
Abbreviations work not only in insert mode, but also in command line mode. After the definition receives a name for the command to abbreviate, start the actual command off with a colon ( :) and an exclamation point ( !) followed by the full command you want to execute.
ab sp !spell ab c755 chmod 755 ab pwd !pwd ab ll !ls -al ab dt !date ab setvt !set term=vt100 ab psf !ps -ef ab psa !ps -aux ab man !man
Any command you use on a regular basis within vi can be abbreviated. Do not try to pipe your commands together within an abbreviation; it will not work. If you try to set the abbreviation from within vi and not from the .exrc file, you might never be able to unabbreviate ( unab) when it is set.
This has got to be one of the most practical uses for the ability to abbreviate. The way it works is like this: The vi editor believes that you are abbreviating a word, but the reality is that you are accidentally misspelling words.
You know what words you always backspace over to correct. Words such as mroe, recieve, and teh are constantly popping up. If you create abbreviations for them, the next time you type the word mroe the vi will replace it with more.
ab mroe more ab mkae make ab teh the ab adn and ab recieve receive ab peice piece ab maint maintenance ab maintanence maintenance ab maintainence maintenance
You can also set up an abbreviation to check the spelling of the file. The best part is that you never have to exit vi to do it.
ab sp !spell %
When you type from vi
:sp
this runs the UNIX spelling checker program against the file you are working on, displaying a list of words that are misspelled. Make sure that the file is saved before running spelling checker; otherwise it will not work.
Use abbreviations to shorten the long words, phrases or sentences that you might find yourself typing repeatedly during the many sessions that you are in vi. Scan some of your documents and watch for key words.
ab nam John Fredrick Doe ab adm Senior UNIX Administrtaor/I.S. Dept. ab sig1 These are my personal views and do not reflect that of the company. ab sig2 I said beam me up Scotty! I have a date with an Alien tonight! Hurry! ab co The UNIX Guru Universe, Inc. ab ph Voice: (234) 555-1234 FAX: (234) 555-2345 Pager: (800) 555-0123 ab sig1 These are my personal views and do not reflect that of the company. ab sig2 I said beam me up Scotty! I have a date with an Alien tonight! Hurry! ab hr ----------------------------------------------------------
Through the use of abbreviations, you can build a formal signature entry or a casual friendly signature entry within vi. It would only take a few words to type.
hr nam adm co ph sig1 hr
the result will be
---------------------------------------------------------------- John Fredrick Doe Senior UNIX Administrator/I.S. Dept. The UNIX Guru Universe, Inc. Voice: (234) 555-1234 FAX: (234) 555-2345 Pager: (800) 555-0123 ab sig1 These are my personal views and do not reflect that of the company. ----------------------------------------------------------------
There many words, titles, Web sites, organizations, and company names that you can shorten by abbreviating them if you find yourself using them regularly.
ab unsub unsubscribe this SPAM, stop sending me this, UNSUBSCRIBE! ab rem remove me from this mailing list! REMOVE ME NOW! ab adminlist steve@foo.com, mike@bar.com, gloria@ugu.comm, rick@foobar.com ab ss20w Sun Sparc 20 Workstation ab sue2 Sun Ultra Enterprise 2 Server ab o2k SGI Origian 20000 Server ab k460 HP9000 K420 Server ab ugu UNIX Guru Universe (http://www.ugu.com) ab u911 http://www.UNIX911.com ab myse don.rocket@losangeles.west.sgi.com ab sa UNIX System Administrations ab Ysuper supercalifragilisticexpialidocious ab ibm International Business Machines
UNIX has many abbreviations built in to let you quickly access what you need. Having the power to create many types of abbreviations enables you to move around faster within the environment and its editors.
A major pitfall to be aware of is mixing up the abbreviated words with real words. It is easy to forget that there is already a reserved English word for some abbreviated words you might try to use. You might find yourself typing something in a hurry and stumble across one of these conflicts. For example, you receive an email from a user who is having trouble loading a tape. So you reply,
I got your message, I'll </B> right over.
In this example, something as little as the word be was defined as an abbreviation for the HTML bold ending . The only way to get the word "be" back is to use :unab from the command line mode, or remove the definition from the .exrc file.
UNIX often gets the cold shoulder from those ignorant of its power. Occasionally I'll have a coworker or manager in my office watching my screen, and I'll tell them I need to write a simple script that will take only a second. I bring up vi and write a simple script. Because I already have the commands for the script abbreviated, in seconds a few words turns into a lengthy script. The response produced is priceless; anything from "How did you do that?" to "You typed all that so fast?". What's more fun is simply responding, "Hey it's not me, it's UNIX!"
ab sh #! /bin/sh ab lp while [ 1 ]; do # Endless Loop ab cl clear # Clear Screen ab df df -kl # Check Filesystem ab sl sleep 3 # Sleep ab dn done # finish loop if ever ab ex exit # exit ab ch755 :!chmod 755
You can create and run the script called dfmon to monitor the local filesystems, without ever leaving vi.
% vi dfmon
sh lp cl df sl dn ex
: c755 dfmon :!dfmon
As you type the abbreviations, the script begins to appear before your eyes. Not only will it appear with the source code to the script but it will be fully commented!
#! /bin/sh while [ 1 ]; do # Endless Loop clear # Clear Screen df -kl # Check Filesystem sleep 3 # Sleep done # finish loop if ever exit # exit
:!chmod 755 dfmon :!dfmon
Man pages:
vi, ex
World Wide Web:
UNIX is a four letter word-- http://www.linuxbox.com/~taylor/4ltrwrd/VRsearch.html
The vi Powered-- http://www.darryl.com/vi.shtml
UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.