📄 ch10_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 10] 10.2 Aliases for Common Commands </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:35:43Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch10_01.htm"TITLE="10. Aliases"><LINKREL="prev"HREF="ch10_01.htm"TITLE="10.1 Creating Custom Commands "><LINKREL="next"HREF="ch10_03.htm"TITLE="10.3 C Shell Aliases with Command-Line Arguments "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch10_01.htm"TITLE="10.1 Creating Custom Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.1 Creating Custom Commands "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 10<BR>Aliases</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch10_03.htm"TITLE="10.3 C Shell Aliases with Command-Line Arguments "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.3 C Shell Aliases with Command-Line Arguments "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2340">10.2 Aliases for Common Commands </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11624"></A><ACLASS="indexterm"NAME="AUTOID-11627"></A><ACLASS="indexterm"NAME="AUTOID-11630"></A>The C shells, <EMCLASS="emphasis">ksh</EM> and <EMCLASS="emphasis">bash</EM> have an "alias" facility that lets youdefine abbreviations for commonly used commands. Aliases can get verycomplicated, so we'll give just an introduction here. We'll usethe <EMCLASS="emphasis">csh</EM> alias syntax here;article<ACLASS="xref"HREF="ch10_04.htm"TITLE="Aliases in ksh and bash ">10.4</A>shows <EMCLASS="emphasis">bash</EM> and <EMCLASS="emphasis">ksh</EM>.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2340-SECT-1.1">10.2.1 Simple Aliases </A></H3><PCLASS="para">The simplest kind of alias is simply a new name for an oldcommand. For example, you might want to rename the <EMCLASS="emphasis">ls</EM> commandas <EMCLASS="emphasis">dir</EM> because you're used to DOS or VMS systems. That's easilydone:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias dir ls</PRE></BLOCKQUOTE></P><PCLASS="para"> <EMCLASS="emphasis">dir</EM> is the new name; from now on, typing <CODECLASS="literal">dir</CODE> as a commandis equivalent to typing <CODECLASS="literal">ls</CODE>. Some other commonly used aliasesare:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias la ls -a # include "hidden" files in listingsalias lf ls -F # show whether files are directories, etc.alias lr ls -R # list recursively-show directory contentsalias ri rm -i # ask before deletingalias mi mv -i # ask before moving over an existing file</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11653"></A><ACLASS="indexterm"NAME="AUTOID-11656"></A>In a <EMCLASS="emphasis">.cshrc</EM> file, the hash mark (<CODECLASS="literal">#</CODE>) means thatthe rest of the line is a comment.Describing your aliases can help you remember what they're for.That's an especially good idea for complicated aliases you write - like thealiases in the next section.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2340-SECT-1.2">10.2.2 Using More Complex Aliases </A></H3><PCLASS="para">Here are a few aliases that Ifind useful; you'll have to adapt them to your own circumstances:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias emacs /home/src/emacs/bin/emacs alias clean "rm *~ .*~ core *.bak" alias vtext 'setenv EXINIT "source $HOME/.exrc.text" ; vi' alias vprog 'setenv EXINIT "source $HOME/.exrc.prog" ; vi'</PRE></BLOCKQUOTE></P><PCLASS="para">Let's look at these aliases more closely.The <EMCLASS="emphasis">emacs</EM> alias isn't anything fancy; it's just a way ofremembering a long command name, without having to<SPANCLASS="link">add another directory to your search path (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>for a single command.(I find longsearch paths aesthetically unappealing.They can also slow your system down, although the C shell uses a<SPANCLASS="link">hash table (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>to speed up searching.On the other hand, it takes time to read aliases like <EMCLASS="emphasis">emacs</EM>from your <EMCLASS="emphasis">.cshrc</EM> file into the shell.Defining lots of aliases, instead of simply changing your search path, candelay logins and<SPANCLASS="link">subshells (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>.If you have a fast computer, it may not matter whether you use lots ofaliases or have a long search path.)</P><PCLASS="para">The <EMCLASS="emphasis">clean</EM> alias is great; it deletes GNU Emacs backup files and <SPANCLASS="link"><EMCLASS="emphasis">core</EM> (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>files (which I usually don't keep around) and other miscellany.Rather than have some complex "auto-cleaning" system that runs from<EMCLASS="emphasis">cron</EM>, I just occasionally type <CODECLASS="literal">clean</CODE> in my current directory.Everyone should have an alias like this and doctorit so that it gets rid of as much junk as possible.(A lot of people, though, would tell you not to be so quick to deleteyour editor's backup files. Use your own judgment.)</P><PCLASS="para">The third and fourth aliases are a bit clever, in a primitive sort of way. You type the command <CODECLASS="literal">vtext afile</CODE>; the shell separates the commands atthe semicolon and executes one after the other:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly ">$HOME</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">setenv EXINIT "source $HOME/.exrc.text"vi afile</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11685"></A>The first command sets the<EMCLASS="emphasis">EXINIT</EM><SPANCLASS="link">environment variable (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>;this makes <EMCLASS="emphasis">vi</EM> read a particular<SPANCLASS="link">setup file (<ACLASS="linkend"HREF="ch04_09.htm"TITLE="Setting Up vi with the .exrc File ">4.9</A>)</SPAN>named <EMCLASS="emphasis">.exrc.text</EM>in the home directory.The second command starts <EMCLASS="emphasis">vi</EM> with whatever arguments you type.You aren't limited to just one filename.You can type whatever arguments you want, including more filenamesand <EMCLASS="emphasis">vi</EM> options; they're all tacked on after <CODECLASS="literal">vi</CODE>. There aremore graceful ways to<ACLASS="indexterm"NAME="AUTOID-11695"></A><ACLASS="indexterm"NAME="AUTOID-11698"></A><SPANCLASS="link">get command-line arguments into aliases (<ACLASS="linkend"HREF="ch10_03.htm"TITLE="C Shell Aliases with Command-Line Arguments ">10.3</A>)</SPAN>,but this does the trick when the arguments go on the end of an alias.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11703"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -