📄 ch31_07.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 31] 31.7 Maps for Repeated Edits </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:16Z"><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_06.htm"TITLE="31.6 Protecting Keys from Interpretation by ex "><LINKREL="next"HREF="ch31_08.htm"TITLE="31.8 More Examples of Mapping Keys in vi "></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_06.htm"TITLE="31.6 Protecting Keys from Interpretation by ex "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.6 Protecting Keys from Interpretation by ex "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_08.htm"TITLE="31.8 More Examples of Mapping Keys in vi "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.8 More Examples of Mapping Keys in vi "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6380">31.7 Maps for Repeated Edits </A></H2><PCLASS="para">[Another way to do this is with<SPANCLASS="link"><CODECLASS="literal">@</CODE>-functions (<ACLASS="linkend"HREF="ch31_04.htm"TITLE="vi @-Functions ">31.4</A>)</SPAN>.-JP]</P><PCLASS="para">Not every keymap is something you want to save in your <EMCLASS="emphasis">.exrc</EM>file. Some maps are handy just because you have to do a repetitiveseries of edits. Developing a complex map to repeat your edits cansave more time than it takes. For example, assume that you have aglossary with entries like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">map - an ex command which allows you to associate a complex command sequence with a single key.</PRE></BLOCKQUOTE></P><PCLASS="para">You want to convert this glossary list to<SPANCLASS="link"><EMCLASS="emphasis">nroff</EM> (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>format, so that it looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.IP "map" 10nAn ex command which allows you to associatea complex command sequence with a single key.</PRE></BLOCKQUOTE></P><PCLASS="para">The best way to define a complex map is to do the edit once manually,writing down each keystroke that you must type.Then re-create these keystrokes as a map.You want to: </P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Insert the <EMCLASS="emphasis">ms</EM> macro for an indented paragraph (<CODECLASS="literal">.IP</CODE>)at the beginning of the line.Insert the first quotation mark as well (<CODECLASS="literal">I.IP <"></CODE>).</P></LI><LICLASS="listitem"><PCLASS="para">Press ESC to terminate text-input mode.</P></LI><LICLASS="listitem"><PCLASS="para">Move to the end of the first word (<CODECLASS="literal">e</CODE>) and add a secondquotation mark, followed by a space and the size of the indent (<CODECLASS="literal">a<"> 10n</CODE>).</P></LI><LICLASS="listitem"><PCLASS="para">Press RETURN to insert a new line.</P></LI><LICLASS="listitem"><PCLASS="para">Press ESC to terminate text-input mode.</P></LI><LICLASS="listitem"><PCLASS="para">Remove the hyphen and two surrounding spaces (<CODECLASS="literal">3x</CODE>) andcapitalize the next word (<CODECLASS="literal">~</CODE>).</P></LI></OL><PCLASS="para">That's quite an editing chore if you have to repeat it more thana few times. With <CODECLASS="literal">map</CODE> you can save the entire sequence so that itcan be re-executed with a single keystroke:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">map g I.IP "^[ea" 10n^M^[3x~</PRE></BLOCKQUOTE></P><PCLASS="para">(To set that option during a <EMCLASS="emphasis">vi</EM> session, type a colon(<CODECLASS="literal">:</CODE>) first.) Note that you have to "quote" both theESC and RETURN characters with <SPANCLASS="link">CTRL-v (<ACLASS="linkend"HREF="ch31_06.htm"TITLE="Protecting Keys from Interpretation by ex ">31.6</A>)</SPAN>.<CODECLASS="literal">^[</CODE> is the sequence that appears when you type<KBDCLASS="keycap">[CTRL-v]</KBD> followed by <KBDCLASS="keycap">[ESC]</KBD><CODECLASS="literal">^M</CODE> is the sequence shown when you type<KBDCLASS="keycap">[CTRL-v]</KBD> <KBDCLASS="keycap">[RETURN]</KBD>.</P><PCLASS="para">Now, simply typing <CODECLASS="literal">g</CODE> will perform the entire series of edits.At a slow data rate you can actually see the edits happening individually.At a fast data rate it will seem to happen by magic.</P><PCLASS="para">Don't be discouraged if your first attempt at keymapping fails.A small error in defining the map can give very different results from the ones you expect.Type <CODECLASS="literal">u</CODE> to undo the edit, and try again.</P><PCLASS="para">[I like to write a keymap in a temporary file and edit it there untilI get it right.For instance, I write my buffer and type <CODECLASS="literal">:e temp.so</CODE> to openthe temporary file <EMCLASS="emphasis">temp.so</EM>.I make the keymaps, one per line - without a colon (<CODECLASS="literal">:</CODE>) first.I write this map file (<CODECLASS="literal">:w</CODE>), then read it in to the editor(<CODECLASS="literal">:so <SPANCLASS="link">% (<ACLASS="linkend"HREF="ch30_04.htm"TITLE="Editing Multiple Files with vi ">30.4</A>)</SPAN></CODE>).If I don't get an error, I switch to the original file(<CODECLASS="literal">:e <SPANCLASS="link"># (<ACLASS="linkend"HREF="ch30_04.htm"TITLE="Editing Multiple Files with vi ">30.4</A>)</SPAN></CODE>) and try the map.Then I usually go back to the map file (<CODECLASS="literal">:e#</CODE>) again, fix the mistake<CODECLASS="literal">:-)</CODE>, and repeat the process until I get what I wanted. <EMCLASS="emphasis">-JP</EM> ]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</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_06.htm"TITLE="31.6 Protecting Keys from Interpretation by ex "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.6 Protecting Keys from Interpretation by ex "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_08.htm"TITLE="31.8 More Examples of Mapping Keys in vi "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.8 More Examples of Mapping Keys in vi "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">31.6 Protecting Keys from Interpretation by ex </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.8 More Examples of Mapping Keys in vi </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 + -