📄 ch11_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 11] 11.13 Shell Command-Line Editing </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:36:15Z"><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="ch11_01.htm"TITLE="11. The Lessons of History"><LINKREL="prev"HREF="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "><LINKREL="next"HREF="ch11_14.htm"TITLE="11.14 More Ways to Do Interactive History Editing "></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="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.12 Pass History to Another Shell "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 11<BR>The Lessons of History</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch11_14.htm"TITLE="11.14 More Ways to Do Interactive History Editing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.14 More Ways to Do Interactive History Editing "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2200">11.13 Shell Command-Line Editing </A></H2><PCLASS="para">[This article was written for the Korn Shell.<ACLASS="indexterm"NAME="AUTOID-13151"></A><ACLASS="indexterm"NAME="AUTOID-13154"></A>It also applies to <EMCLASS="emphasis">bash</EM> and <EMCLASS="emphasis">tcsh</EM>, though there are somedifferences.Check your shell's manpage. <ICLASS="filename">-JP]</I></P><PCLASS="para">If you make a typing mistake in your shell command line and press RETURN, it's gone. After you get the pesky error message, you'llhave to type the whole thing in again - unless you happen to beusing the C shell <EMCLASS="emphasis">and</EM> happen to remember its<SPANCLASS="link">Byzantine "history" commands (<ACLASS="linkend"HREF="ch11_02.htm"TITLE="History in a Nutshell ">11.2</A>)</SPAN>.If you use the Korn shell, you may know that it givesyou a <EMCLASS="emphasis">vi</EM>-like editing capability; if you've actually triedthis, you probably agree that <EMCLASS="emphasis">vi</EM> makes a lousy command-lineeditor.[I don't agree. <CODECLASS="literal">:-)</CODE>If you know <EMCLASS="emphasis">vi</EM>, hit ESC on a command line to go to <EMCLASS="emphasis">vi</EM>command mode; <EMCLASS="emphasis">a</EM> or <EMCLASS="emphasis">i</EM> takes you to input mode, as always.Hit RETURN from either mode to execute a command line - orCTRL-c to cancel it. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para">However, fewer people know that the Korn shell has another editingmode, one that emulates the<SPANCLASS="link">Emacs (<ACLASS="linkend"HREF="ch32_01.htm#UPT-ART-5540"TITLE="Emacs: The Other Editor ">32.1</A>)</SPAN>editor.The emacsmode editing commands act like a natural extension to traditional,simple shell editing commands (like Delete or Backspacefor character erase), so even if youaren't familiar with the <EMCLASS="emphasis">emacs</EM> editor, you should find emacsmode useful. To use emacs mode, put this line in your <EMCLASS="emphasis">.profile</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set -o emacs</PRE></BLOCKQUOTE></P><PCLASS="para">We'll cover only the most useful emacs mode commands here. Fora more complete description, see O'Reilly & Associates' <EMCLASS="emphasis">Learning the Korn Shell</EM>, by Bill Rosenblatt.</P><PCLASS="para">Emacs mode figures out what your<SPANCLASS="link">character-erase key (<ACLASS="linkend"HREF="ch05_09.htm"TITLE="Setting Your Erase, Kill, and Interrupt Characters ">5.9</A>)</SPAN>is and lets youuse it in the same way.In addition, it gives you thebasic commands for editing a line listed in<ACLASS="indexterm"NAME="AUTOID-13181"></A><ACLASS="indexterm"NAME="AUTOID-13184"></A><ACLASS="xref"HREF="ch11_13.htm#UPT-ART-2200-TAB-0"TITLE="Korn Shell Emacs Mode Line Editing Commands">Table 11.1</A>.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-2200-TAB-0">Table 11.1: Korn Shell Emacs Mode Line Editing Commands</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Command</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Function</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-b</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Move backward one character (without deleting).</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-f</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Move forward one character.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-d</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Delete one character forward.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-z</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Move to beginning of line.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-e</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Move to end of line.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-k</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Delete ("kill") forward to end of line.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-w</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Delete ("wipe") backward to beginning of line.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-y</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Retrieve ("yank") last deleted item.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-c</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Delete entire line.</TD></TR></TBODY></TABLE><PCLASS="para">In addition, emacs mode maintains a history file that enables youto recall previous commands.The commands in<ACLASS="xref"HREF="ch11_13.htm#UPT-ART-2200-TAB-1"TITLE="Korn Shell Emacs Mode History Commands">Table 11.2</A>are the mostimportant of those that let you navigate the history file.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-2200-TAB-1">Table 11.2: Korn Shell Emacs Mode History Commands</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Command</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Function</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-p</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Go to previous command.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-n</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Go to next command.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">CTRL-r<EMCLASS="emphasis">string</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Search backward for command containing <EMCLASS="emphasis">string</EM>.</TD></TR></TBODY></TABLE><PCLASS="para">The first of these is the most useful by far - it's the "I madea mistake, so I'll go back and fix it" key. The search capabilitylets you bring back specific commands you may have typed awhileago without having to go through the history file line by linewith CTRL-p. Just enter CTRL-r followed by a search string and RETURN,and the Korn shell will bring back the <EMCLASS="emphasis">most recent</EM> commandthat contains the search string (or beep at you if it finds no match).Assuming it is the command you want, you would then press RETURNagain to run the command.If you begin your search string with a caret (<CODECLASS="literal">^</CODE>), it will only matchcommands that begin with the search string; this will be familiarbehavior to users of such UNIX tools as <EMCLASS="emphasis">grep</EM>, <EMCLASS="emphasis">sed</EM>, and<EMCLASS="emphasis">awk</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13251"></A><ACLASS="indexterm"NAME="AUTOID-13254"></A>Another extremely useful feature of emacs mode is the <EMCLASS="emphasis">filenamecompletion</EM> facility, which should be familiar to C shell expertsas well as Emacs users (see article<ACLASS="xref"HREF="ch09_08.htm"TITLE="Filename Completion: Faster Filename Typing ">9.8</A>).Emacs mode supports two completion commands, the most useful of whichis ESC ESC (the Escape key pressed twice).If you type in a word and press ESC ESC, the shell will search for a filenamethat begins with what you typed and try to finish it. If there isonly one filename that begins with your word, the shell will completethe name. If there is more than one, it will only complete out asfar as it can without having to make a choice.</P><PCLASS="para">For example, if your directory contained the file <EMCLASS="emphasis">program.c</EM>and you typed <CODECLASS="literal">pro</CODE> followed by ESC ESC, the shell would automaticallycomplete the filename and leave you an extra space. But if yourdirectory also contained the file <EMCLASS="emphasis">program.o</EM>, the shell wouldonly complete out to <CODECLASS="literal">program.</CODE> and let you complete the filenameyourself.</P><PCLASS="para">[Another way to do history editing is with your own editor:the<SPANCLASS="link"><EMCLASS="emphasis">fc</EM> command (<ACLASS="linkend"HREF="ch11_14.htm"TITLE="More Ways to Do Interactive History Editing ">11.14</A>)</SPAN>.<EMCLASS="emphasis">-JP</EM> ]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">BR</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="ch11_12.htm"TITLE="11.12 Pass History to Another Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.12 Pass History to Another Shell "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="ch11_14.htm"TITLE="11.14 More Ways to Do Interactive History Editing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.14 More Ways to Do Interactive History Editing "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">11.12 Pass History to Another Shell </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">11.14 More Ways to Do Interactive History Editing </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 + -