📄 ch41_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 41] 41.4 How UNIX Handles TAB Characters </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:51:56Z"><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="ch41_01.htm"TITLE="41. Terminal and Serial Line Settings"><LINKREL="prev"HREF="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "><LINKREL="next"HREF="ch41_05.htm"TITLE="41.5 Why Some Systems Backspace over Prompts "></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="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 41.3 Find Out Terminal Settings with stty "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 41<BR>Terminal and Serial Line Settings</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch41_05.htm"TITLE="41.5 Why Some Systems Backspace over Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 41.5 Why Some Systems Backspace over Prompts "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5260">41.4 How UNIX Handles TAB Characters </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45679"></A><ACLASS="indexterm"NAME="AUTOID-45681"></A>TAB characters are used in a lot of places: tables, indented paragraphs,source code for programs, the output of many programs, and so on.UNIX handles TABs in a flexible way that's different from some othercomputer systems.</P><PCLASS="para">Most UNIX programs, printers, and terminals are set up to assume tabstopsevery 8 columns.That is, if the terminal or printer gets a TAB character on its input,it moves to the next tabstop position: column 9, 17, 25, etc.The UNIX system(<SPANCLASS="link">kernel, device driver (<ACLASS="linkend"HREF="ch42_01.htm#UPT-ART-1142"TITLE="Making Sense Out of the Terminal Mess ">42.1</A>)</SPAN>)usually doesn't interpret TABcharacters or settabstops; it treats the TABs like any other character, passing them onto utilities or hardware like terminals.</P><PCLASS="para">You might want to use tabstop intervals other than 8.When I write programs, for example, an 8-character indent wastes space,so I use a 4-character indent. If you want to use different tabstops, too, you need to understand how TABsare handled.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5260-SECT-1.1">41.4.1 TAB Is Just Another Character to UNIX </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45690"></A>Typing TABsends a single TAB character to the UNIXsystem.<ACLASS="indexterm"NAME="AUTOID-45692"></A>If you're editing a file, the editor probably puts that single TABcharacter into the file.Later, when you use<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">pr</EM> (<ACLASS="linkend"HREF="ch43_07.htm"TITLE="Quick-and-Dirty Formatting Before Printing ">43.7</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">lp</EM> (<ACLASS="linkend"HREF="ch43_02.htm"TITLE="Introduction to Printing on UNIX ">43.2</A>)</SPAN>,and so on, they readeach TAB and send out that single character to your terminal, printer, orwhatever.The TAB is usually interpreted by the hardware device itself.Before that, it's a single character like any other.(But see the <EMCLASS="emphasis">stty -tabs</EM> command below.)</P><PCLASS="para">If your terminal has a setup mode, enter setup modeand look at the tabstop settings.They're probably set at columns 9, 17, 25, and so on.When your terminal receives a TAB from the UNIX system, the terminal movesthe cursor to the terminal's next tabstop.</P><PCLASS="para">For example, your terminal might have different tabstops - maybe 11, 21, 31,and so on.Let's say that you're <EMCLASS="emphasis">cat</EM>ting a file.The programmer who made the file had her terminal tabstops set at thedefault 8-column intervals.When she typed it, she used TABs (shown as <CODECLASS="literal"><IMGSRC="../chars/rtri.gif"ALT=">"></CODE> in this example) to indentthe lines.Her terminal showed the file this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat prog</B></CODE>while read line; do<IMGSRC="../chars/rtri.gif"ALT=">"> set $line<IMGSRC="../chars/rtri.gif"ALT=">"> for word in line; do<IMGSRC="../chars/rtri.gif"ALT=">"> <IMGSRC="../chars/rtri.gif"ALT=">"> case "$1" in...</PRE></BLOCKQUOTE></P><PCLASS="para">If your terminal has tabstops set at 12 characters, the same file wouldlook like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat prog</B></CODE>while read line; do<IMGSRC="../chars/rtri.gif"ALT=">"> set $line<IMGSRC="../chars/rtri.gif"ALT=">"> for word in line; do<IMGSRC="../chars/rtri.gif"ALT=">"> <IMGSRC="../chars/rtri.gif"ALT=">"> case "$1" in...</PRE></BLOCKQUOTE></P><PCLASS="para">Neither the UNIX kernel nor the <EMCLASS="emphasis">cat</EM> program did anything different to the <EMCLASS="emphasis">prog</EM> file.The terminal interprets the TABs.</P><PCLASS="para">If you want to display or edit a file that has different tabstops thanyour terminal, what can you do?</P><ULCLASS="itemizedlist"><LICLASS="listitem"><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="expand">expand</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-45720"></A><ACLASS="indexterm"NAME="AUTOID-45723"></A>Use a UNIX utility that "expands" (converts) TABs into spaces.On BSD systems, <EMCLASS="emphasis">expand</EM> does the job.The terminal never sees the TABs in this case, so its tabstop settingsare never used:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>expand prog</B></CODE>while read line; do set $line for word in line; do case "$1" in...</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45731"></A>On System V, use <EMCLASS="emphasis">pr</EM> with its <EMCLASS="emphasis">-t -e</EM> options:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>pr -t -e prog</B></CODE>while read line; do set $line for word in line; do case "$1" in...</PRE></BLOCKQUOTE></P><PCLASS="para">So, no matter what your terminal tabstops are, the file will look normal.</P><PCLASS="para">If you want to use other tabstops, use an option.For instance, get 4-character tabstops with <EMCLASS="emphasis">expand -4</EM> or<EMCLASS="emphasis">pr -t -e4</EM>.</P></LI><LICLASS="listitem"><PCLASS="para">Tell your text editor to use different tabstops.The editor will probably "expand" the TABs into spaces before it sendsthem to your terminal.For instance, in <EMCLASS="emphasis">vi</EM>, type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:set tabstop=4</PRE></BLOCKQUOTE></P><PCLASS="para">to edit programs with 4-character tabstops.The <EMCLASS="emphasis">prog</EM> file from above would look like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>vi prog</B></CODE>while read line; do<IMGSRC="../chars/rtri.gif"ALT=">"> set $line<IMGSRC="../chars/rtri.gif"ALT=">"> for word in line; do<IMGSRC="../chars/rtri.gif"ALT=">"> <IMGSRC="../chars/rtri.gif"ALT=">"> case "$1" in...</PRE></BLOCKQUOTE></P><PCLASS="para">although it has TAB characters in exactly the same places.If you have a text editor handy, try changing its tabstops while youdisplay a file.</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45754"></A>If you make a file with non-standard tabstops, it's a good ideato put a comment in the file so people will know.Or, before you save the file for the last time, expand the TABs intospaces.With <EMCLASS="emphasis">vi</EM> on a system that has <EMCLASS="emphasis">expand</EM>, for example, this commandwould convert TABs to spaces at 4-column tabstops:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch30_22.htm"TITLE="30.22 Filtering Text Through a UNIX Command ">:%!</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">:%!expand -4</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45763"></A>The opposite of <EMCLASS="emphasis">expand</EM> is, naturally enough,<SPANCLASS="link"><EMCLASS="emphasis">unexpand</EM> (<ACLASS="linkend"HREF="ch24_06.htm"TITLE="Save Space with Tab Characters ">24.6</A>)</SPAN>.It converts spaces back to TABs at 8-column tabstops.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5260-SECT-1.2">41.4.2 Telling UNIX to Expand TABs </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45771"></A>I said above that the UNIX kernel and device drivers don't usuallyexpand TABs into spaces.Sometimes, if you're having a lot of trouble with funny-looking tabstopsettings, you might ask the device driver to expand the TABs into spaces.The command is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>stty -tabs</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Now in most cases, UNIX will not send TABs to your terminal.It's better to fix your terminal's tabstops, if you can.</P></DIV><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="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 41.3 Find Out Terminal Settings with stty "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="ch41_05.htm"TITLE="41.5 Why Some Systems Backspace over Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 41.5 Why Some Systems Backspace over Prompts "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">41.3 Find Out Terminal Settings with stty </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">41.5 Why Some Systems Backspace over Prompts </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 + -