📄 ch31_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 31] 31.6 Protecting Keys from Interpretation by ex </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:46: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="ch31_01.htm"TITLE="31. Creating Custom Commands in vi"><LINKREL="prev"HREF="ch31_05.htm"TITLE="31.5 Keymaps for Pasting into a Window Running vi "><LINKREL="next"HREF="ch31_07.htm"TITLE="31.7 Maps for Repeated Edits "></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="ch31_05.htm"TITLE="31.5 Keymaps for Pasting into a Window Running vi "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.5 Keymaps for Pasting into a Window Running vi "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 31<BR>Creating Custom Commands in vi</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch31_07.htm"TITLE="31.7 Maps for Repeated Edits "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.7 Maps for Repeated Edits "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6370">31.6 Protecting Keys from Interpretation by ex </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-6370-IX-CONTROL-CHARACTERS-EX-AND-VI-EDITORS-AND"></A>Note that when defining a map, you cannot simply type certain keys,such asRETURN,ESC,BACKSPACE,andDELETEas part of the command to be mapped, because these keys already have meaning within <EMCLASS="emphasis">ex</EM>.If you want to include one of these keys as part of the commandsequence, you must escape the normal meaning by preceding the key with <CODECLASS="literal">^V</CODE> ( CTRL-v).A carriage return after CTRL-v appears as <CODECLASS="literal">^M</CODE>, escape as <CODECLASS="literal">^[</CODE>, backspace as<CODECLASS="literal">^H</CODE>, and so on.</P><PCLASS="para">On the other hand, if you want to use a control character as thecharacter to be mapped,in most cases all you have to do is hold down theCTRLkey and press the letter key at the same time.So, for example, all you need to do in order to map <CODECLASS="literal">^A</CODE>(CTRL-a) is to type:<ACLASS="indexterm"NAME="AUTOID-35254"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:map <KBDCLASS="keycap">[CTRL-a]</KBD> <CODECLASS="replaceable"><I>sequence</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">There are, however, a few other control characters that must be escaped with a <CODECLASS="literal">^V</CODE>.One is <CODECLASS="literal">^T</CODE>.The others are:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">The characters that your account uses for erasing parts of theinput you type at a command line,<CODECLASS="literal">^W</CODE> for erasing wordsand <CODECLASS="literal">^U</CODE> for erasing lines(see article<ACLASS="xref"HREF="ch09_02.htm"TITLE="Fix a Line Faster with Line-Kill and Word-Erase ">9.2</A>).</P></LI><LICLASS="listitem"><PCLASS="para">The characters for<SPANCLASS="link">interrupting jobs (<ACLASS="linkend"HREF="ch38_09.htm"TITLE="Killing Foreground Jobs ">38.9</A>)</SPAN>and<SPANCLASS="link">stopping jobs (<ACLASS="linkend"HREF="ch12_08.htm"TITLE="Job Control in a Nutshell ">12.8</A>)</SPAN>.</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-6370-IX-VI-EDITOR-MAP-COMMANDS"></A><ACLASS="indexterm"NAME="UPT-ART-6370-IX-VI-EDITOR-QUOTING-CONTROL-CHARACTERS"></A><ACLASS="indexterm"NAME="UPT-ART-6370-IX-KEYBOARD-SHORTCUTS-VI-MAP-COMMANDS"></A><ACLASS="indexterm"NAME="AUTOID-35284"></A>So, for example, if you want to map <CODECLASS="literal">^T</CODE>, you must type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:map <KBDCLASS="keycap">[CTRL-v]</KBD> <KBDCLASS="keycap">[CTRL-t]</KBD> <CODECLASS="replaceable"><I>sequence</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The use of CTRL-vapplies to any <EMCLASS="emphasis">ex</EM> command, not just a map command.This means that you can type a carriage return in an<SPANCLASS="link">abbreviation (<ACLASS="linkend"HREF="ch30_31.htm"TITLE="vi Word Abbreviation ">30.31</A>)</SPAN>or a substitution command. For example, the abbreviation:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:ab 123 one^Mtwo^Mthree</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">expands to this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">onetwothree</PRE></BLOCKQUOTE></P><PCLASS="para">(The sequence <KBDCLASS="keycap">[CTRL-v]</KBD> <KBDCLASS="keycap">[RETURN]</KBD>is shown as it appears on your screen, <CODECLASS="literal">^M</CODE>.)</P><PCLASS="para">You can also add lines globally at certain locations.The command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/^Section/s//As you recall, in^M&/</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">inserts a phrase on a separate line before any line beginning with the word <EMCLASS="emphasis">Section</EM>. The <CODECLASS="literal">&</CODE> restores the search pattern.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-35313"></A>The vertical bar (<CODECLASS="literal">|</CODE>) is used as a separator of multiple <EMCLASS="emphasis">ex</EM>commands; it's especially difficult to quote.Because a map is interpreted when it's stored and again when it's used,you need enough CTRL-v characters to protect the vertical barfrom each interpretation.You also need to protect stored CTRL-v characters by adding aCTRL-v before each one!The worst case is a text-input mode map(<SPANCLASS="link"><CODECLASS="literal">map!</CODE> (<ACLASS="linkend"HREF="ch31_02.htm"TITLE="Save Time and Typing with the vi map Commands ">31.2</A>)</SPAN>)-it needs threeCTRL-v characters, which means you need to type <EMCLASS="emphasis">six</EM>CTRL-v characters before you type the vertical bar.For example, the following map will make your<SPANCLASS="link">function key F1 (<ACLASS="linkend"HREF="ch31_02.htm"TITLE="Save Time and Typing with the vi map Commands ">31.2</A>)</SPAN>insert the string <CODECLASS="literal">{x|y}</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>map! #1 {x^V^V^V|y}</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you ask for a list of text-input mode maps,you should see a single stored CTRL-v:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:map!</B></CODE>f1 ^[OP {x^V|y}</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LL</SPAN>, <SPANCLASS="authorinitials">DG</SPAN>, <SPANCLASS="authorinitials">JP</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Learning the vi Editor</CITE>, Chapter 7</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="ch31_05.htm"TITLE="31.5 Keymaps for Pasting into a Window Running vi "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.5 Keymaps for Pasting into a Window Running vi "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="ch31_07.htm"TITLE="31.7 Maps for Repeated Edits "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.7 Maps for Repeated Edits "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">31.5 Keymaps for Pasting into a Window Running vi </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">31.7 Maps for Repeated Edits </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 + -