📄 ch31_02.htm
字号:
HREF="ch05_02.htm"TITLE="The Idea of a Terminal Database ">5.2</A>)</SPAN>defines those keys.For example, to make function key F1 transpose words:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">map #1 dwelp</PRE></BLOCKQUOTE></P><PCLASS="para">A final note: map assignments are not really limited to unused keys.You can map keys that are defined as other <EMCLASS="emphasis">vi</EM> commands,but then the key's original meaning is inaccessible.But this is probably okay if the key is tied to a command that you rarely use.There's more information in article<ACLASS="xref"HREF="ch31_14.htm"TITLE="Don't Lose Important Functions with vi Maps: Use noremap">31.14</A>about the <EMCLASS="emphasis">noremap</EM> option.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2770-SECT-1.2">31.2.2 Text-Input Mode Maps </A></H3><PCLASS="para">The <CODECLASS="literal">map!</CODE> command works like <CODECLASS="literal">map</CODE>,but <CODECLASS="literal">map!</CODE> works during text-input mode.You actually set the <CODECLASS="literal">map!</CODE> during command mode, in the sameway as a plain <CODECLASS="literal">map</CODE>:at a colon (<CODECLASS="literal">:</CODE>) prompt, type <CODECLASS="literal">map!</CODE> followed by a space andthe key(s) that activate the map; then type a space and the text thatthe text-input mode map stands for.These text-input mode maps are a lot like<SPANCLASS="link">abbreviations (<ACLASS="linkend"HREF="ch30_31.htm"TITLE="vi Word Abbreviation ">30.31</A>)</SPAN>;the difference is that <CODECLASS="literal">map!</CODE> lets you switch from text-input mode tocommand mode, execute commands, then go back to text-input mode.To go to command mode during a <CODECLASS="literal">map!</CODE>, you'll need to put an ESC key in thevalue of the map by<SPANCLASS="link">typing CTRL-v and then ESC (<ACLASS="linkend"HREF="ch31_06.htm"TITLE="Protecting Keys from Interpretation by ex ">31.6</A>)</SPAN>.After your <CODECLASS="literal">map!</CODE> does whatever in command mode, it can re-entertext-input mode with the usual commands <CODECLASS="literal">a</CODE>, <CODECLASS="literal">i</CODE>, and so on.</P><PCLASS="para">Let's say you normally never type the caret (<CODECLASS="literal">^</CODE>) key duringinput mode.When you're typing along, as you realize that what you're typing isimportant, you want to press the caret key.Then, <EMCLASS="emphasis">vi</EM> should open a line above and insert the phrase"THIS IS IMPORTANT:".Finally, <EMCLASS="emphasis">vi</EM> should return you to text-input mode at the end of the linewhere you pressed the caret key.To do that, go to command mode and enter the following <CODECLASS="literal">map!</CODE>command.The first <CODECLASS="literal">^</CODE> comes from pressing the caret key.Then you'll see two places with <CODECLASS="literal">^[</CODE>; that's made by pressingCTRL-v followed by the ESC key.Finish the map by pressing RETURN:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:map! ^ ^[OTHIS IS IMPORTANT:^[jA</PRE></BLOCKQUOTE></P><PCLASS="para">What does that do?It executes the same <EMCLASS="emphasis">vi</EM> commands you'd use to add those three wordsyourself, manually.During text-input mode, typing a caret (<CODECLASS="literal">^</CODE>) will:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Do ESC to go to command mode,</P></LI><LICLASS="listitem"><PCLASS="para">Use <CODECLASS="literal">O</CODE> to open a new line above (in text-input mode),</P></LI><LICLASS="listitem"><PCLASS="para">Enter the text <CODECLASS="literal">THIS IS IMPORTANT:</CODE></P></LI><LICLASS="listitem"><PCLASS="para">Do another ESC to go back to command mode,</P></LI><LICLASS="listitem"><PCLASS="para">Do <CODECLASS="literal">j</CODE> to go down a line (to the line where you started), and</P></LI><LICLASS="listitem"><PCLASS="para">Do <CODECLASS="literal">A</CODE> to put you at the end of the line, in text-input mode.</P></LI></OL><PCLASS="para">The trick is to use <CODECLASS="literal">map!</CODE> only to redefine keys you'llnever use for anything else during text-input mode.To temporarily disable a text-input mode map, press CTRL-vbefore the key.For example, to put a real caret into your file, type<KBDCLASS="keycap">[CTRL-v]</KBD><KBDCLASS="keycap">[<CODECLASS="literal">^</CODE>]</KBD>To disable an input-mode map for the rest of your <EMCLASS="emphasis">vi</EM> session,type <CODECLASS="literal">:unmap!</CODE> followed by the character(s) that activate themap.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-34976"></A>A more common example is mapping your keyboard's arrow or function keysto do something during text-input mode.These keys send a special series of characters.Normally, without a <CODECLASS="literal">map!</CODE> defined for these keys, the charactersthey send will be put into your editor buffer - just as if you'd typedthe characters they make, yourself, one by one.For instance, my left arrow key sends the characters <CODECLASS="literal">ESC</CODE>, then<CODECLASS="literal">[</CODE> (left bracket), then <CODECLASS="literal">D</CODE>.Without an text-input mode <CODECLASS="literal">map!</CODE> defined for that three-charactersequence, <EMCLASS="emphasis">vi</EM> will be hopelessly confused[1]if I press that arrow key.Many UNIX developers have added text-input mode maps for arrow keys.You can see them when you list all your text-input mode maps by typing<CODECLASS="literal">:map!</CODE> by itself, with nothing after:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] Actually, the <CODECLASS="literal">ESC</CODE> will switch <EMCLASS="emphasis">vi</EM> back to command mode.The first <CODECLASS="literal">[</CODE> will make <EMCLASS="emphasis">vi</EM> think you're about to typethe section-motion command <CODECLASS="literal">[[</CODE>, so the following <CODECLASS="literal">D</CODE>will make <EMCLASS="emphasis">vi</EM> beep.Ugly, eh?</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">up ^[[A ^[kadown ^[[B ^[jaleft ^[[D ^[hiright ^[[C ^[la^ ^ ^[OTHIS IS IMPORTANT:^[jA</PRE></BLOCKQUOTE></P><PCLASS="para">Article<ACLASS="xref"HREF="ch31_03.htm"TITLE="What You Lose When You Use map! ">31.3</A>lists some problems with <CODECLASS="literal">map!</CODE>.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">DG</SPAN>, <SPANCLASS="authorinitials">LL</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_01.htm"TITLE="31.1 Why Type More Than You Have To? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.1 Why Type More Than You Have To? "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_03.htm"TITLE="31.3 What You Lose When You Use map! "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.3 What You Lose When You Use map! "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">31.1 Why Type More Than You Have To? </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.3 What You Lose When You Use map! </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 + -