📄 ch07_05.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 7] 7.5 Multiline Shell Prompts </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:33:46Z"><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="ch07_01.htm"TITLE="7. Setting Your Shell Prompt"><LINKREL="prev"HREF="ch07_04.htm"TITLE="7.4 Faster Prompt Setting with Built-Ins "><LINKREL="next"HREF="ch07_06.htm"TITLE="7.6 Session Information in Your Terminal's Status Line "></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="ch07_04.htm"TITLE="7.4 Faster Prompt Setting with Built-Ins "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.4 Faster Prompt Setting with Built-Ins "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 7<BR>Setting Your Shell Prompt</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch07_06.htm"TITLE="7.6 Session Information in Your Terminal's Status Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.6 Session Information in Your Terminal's Status Line "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1300">7.5 Multiline Shell Prompts </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6821"></A>Lots of people like lots of information in their prompts: hostname,directory name, history number, maybe username.Lots of people have spent lots of time trying to make their prompts shortenough to fit across the screen and still leave room for typinga command longer than <EMCLASS="emphasis">ls</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><elaineq@applefarm> [/usr/elaineq/projects/april/week4] 23 % <CODECLASS="userinput"><B>ls</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Even with fairly short prompts, if you look back at a screen afterrunning a few commands, telling the data from the prompts can be alittle tough(real terminals don't show user input in boldface, so I won't do ithere either):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">'\" DON'T BOLDFACE USER'S INPUT HERE, SO IT BLENDS TOGETHER LIKE REAL SCREEN:<elaineq@applefarm> [~] 56% cd beta<elaineq@applefarm> [~/beta] 57% which prog/usr/tst/applefarm/bin/beta/prog<elaineq@applefarm> [~/beta] 58% prog61,102 units inventoried; 3142 to do<elaineq@applefarm> [~/beta] 59%</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6832"></A>One nice answer is to make a prompt that has more than one line.Here's part of a <EMCLASS="emphasis">.cshrc</EM> file that sets a three-line prompt: one blankline, one line with the hostname and current directory, and a third with thehistory number and a percent sign:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch50_07.htm"TITLE="50.7 How UNIX Systems Remember Their Name ">uname -n</A> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">{..}</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"> set hostname=`uname -n`alias setprompt 'set prompt="\\${hostname}:${cwd}\\\! % "'alias cd 'chdir \!* && setprompt'setprompt # to set the initial prompt</PRE></TD></TR></TABLE></P><PCLASS="para">The prompts look like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">applefarm:/usr/elaineq/projects/april/week423 % prog | tee /dev/tty | mail -s "prog results" bigboss@corpoffice61,102 units inventoried; 3142 to doapplefarm:/usr/elaineq/projects/april/week424 % cd ~/betaapplefarm:/usr/elaineq/beta25 % prog | mail joanne</PRE></BLOCKQUOTE></P><PCLASS="para">The blank lines separate each command - though you may want to save space byomitting them.<ACLASS="indexterm"NAME="AUTOID-6846"></A>For example, Mike Sierra (here at O'Reilly & Associates)uses a row of asterisks:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">***** 23 *** <mike@mymac> *** /home/mike/calendar *****% cd September***** 24 *** <mike@mymac> *** /home/mike/calendar/September *****%</PRE></BLOCKQUOTE></P><PCLASS="para">You don't need a multiline prompt-setting command to get a newline into<EMCLASS="emphasis">bash</EM> prompts.<ACLASS="indexterm"NAME="AUTOID-6852"></A>Just put a <CODECLASS="literal">\n</CODE> (which stands for a newline character)anywhere you want the prompt to break to a new line.</P><PCLASS="para">What I like best about multiline prompts is that you get a lot ofinformation but have the whole screen width for typing. Of course,you can put different information in the prompt than I've shown here.The important idea is: if you want more information and need room totype, try a multiline prompt.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch07_04.htm"TITLE="7.4 Faster Prompt Setting with Built-Ins "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 7.4 Faster Prompt Setting with Built-Ins "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch07_06.htm"TITLE="7.6 Session Information in Your Terminal's Status Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 7.6 Session Information in Your Terminal's Status Line "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">7.4 Faster Prompt Setting with Built-Ins </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">7.6 Session Information in Your Terminal's Status Line </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -