Section 8.7. Aliases


[Page 307 (continued)]

8.7. Aliases

The C shell allows you to create and customize your own commands by using the built-in command alias (Figure 8-14).


[Page 308]

Figure 8-14. Description of the alias shell command.

Shell Command: alias [word [string] ]

alias supports a simple form of command-line customization. If you alias word to be equal to string and then later enter a command beginning with word, the first occurrence of word is replaced by string and then the command is reprocessed.

If you don't supply word or string, a list of all the current shell aliases is displayed. If you only supply word, then the string currently associated with the alias word is displayed. If you supply word and string, the shell adds the specified alias to its collection of aliases. If an alias already exists for word, it is replaced.

If the replacement string begins with word, it is not reprocessed for aliases to prevent infinite loops. If the replacement string contains word elsewhere, an error message is displayed when the alias is executed.


Here's an example of alias in action:

$ alias dir 'ls -aF'    ...register an alias. $ dir                   ...same as typing "ls -aF". ./         main2.c      p.reverse.c       reverse.h ../        main2.o      palindrome.c      reverse.old $ dir *.c               ...same as typing "ls -aF *.c". main2.c     p.reverse.c       palindrome.c $ alias dir   ...look at the value associated with "dir". ls -aF $ _ 


In the following example, I aliased a word in terms of itself:

% alias ls 'ls -aF'   ...define "ls" in terms of itself. % ls *.c              ...same as typing "ls -aF *.c". main2.c     p.reverse.c       palindrome.c % alias dir 'ls'      ...define "dir" in terms of "ls". % dir                 ...same as typing "ls -aF". ./          main2.c     p.reverse.c       reverse.h ../         main2.o     palindrome.c      reverse.old % alias who 'date; who'       ...infinite loop problem. % who Alias loop. % alias who 'date; /usr/bin/who'  ...full path avoids error % who                         ...works fine now. Fri May 13 23:33:37 CST 2005 glass               ttyp0       Feb 13 23:30    (xyplex2) % _ 



[Page 309]

8.7.1. Removing an Alias

To remove an alias, use the built-in command unalias (Figure 8-15).

Figure 8-15. Description of the unalias shell command.

Shell Command: unalias pattern

unalias removes all of the aliases that match pattern. If pattern is *, then all aliases are removed.


8.7.2. Useful Aliases

Figure 8-16 is a list of the useful aliases that I keep in my ".tcshrc" file, together with a brief description.

Figure 8-16. Useful C shell aliases.

Alias

Value

cd

cd \!*; set prompt = "$cwd \! >"; ls

This changes your prompt to contain both the current working directory and the last command number (see history for more details).

ls

ls -F

This causes ls to include extra file information.

rm

rm -i

This causes rm to ask for confirmation.

rm

mv \!* ~/tomb

This causes rm to move a file into a special "tomb" directory instead of removing it.

h

history

This allows you to obtain history information by typing just one letter.

vim

(mesg n; /usr/bin/vim \!*; mesg y)

This stops people from sending you messages while you're in the vim editor.

mroe

more

This corrects a common spelling error.

ls-l

ls -l

This corrects a common spelling error.

ll

ls -l

This allows you to obtain a long directory listing more conveniently.


8.7.3. Sharing Aliases

To make an alias available to a subshell, place its definition in the shell's ".tcshrc" file.


[Page 310]

8.7.4. Parameterized Aliases

An alias may refer to arguments in the original pre-aliased command by using the history mechanism described in the next section. The pre-aliased command is treated as if it were the previous command. The useful alias for cd that I mentioned in the previous section makes good use of this facility; the \!* part of the alias is replaced by all of the arguments in the pre-aliased command. The ! is preceded by a \ to inhibit its special meaning during the assignment of the alias:

alias cd 'cd \!*; set prompt = "$cwd \! > "; ls'





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net