⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch31_04.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 31] 31.4 vi @-Functions </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:46:12Z"><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_03.htm"TITLE="31.3 What You Lose When You Use map! "><LINKREL="next"HREF="ch31_05.htm"TITLE="31.5 Keymaps for Pasting into a Window Running 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_03.htm"TITLE="31.3 What You Lose When You Use map! "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 31.3 What You Lose When You Use map! "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_05.htm"TITLE="31.5 Keymaps for Pasting into a Window Running vi "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 31.5 Keymaps for Pasting into a Window Running vi "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-9913">31.4 vi @-Functions </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-9913-IX-VI-EDITOR-FUNCTIONS-AT-FUNCTIONS"></A><ACLASS="indexterm"NAME="UPT-ART-9913-IX-FUNCTIONS-VI-AT-FUNCTIONS"></A>The <EMCLASS="emphasis">vi</EM><SPANCLASS="link"><EMCLASS="emphasis">map</EM> command (<ACLASS="linkend"HREF="ch31_02.htm"TITLE="Save Time and Typing with the vi map Commands ">31.2</A>)</SPAN>lets you define keymaps:short names for a series of one or more other commands.You can enter <CODECLASS="literal">:map</CODE> to define a keymap while you're editing afile with <EMCLASS="emphasis">vi</EM>.But if you make a mistake, you usually have to re-enter the whole <CODECLASS="literal">:map</CODE>command to correct the problem.</P><PCLASS="para"><CODECLASS="literal">@</CODE>-functions (say &quot;at-functions&quot;)give you another way to define complex commands.You can define 26 <CODECLASS="literal">@</CODE>-functions named <CODECLASS="literal">@a</CODE> through <CODECLASS="literal">@z</CODE>.They're stored in<SPANCLASS="link">named buffers (<ACLASS="linkend"HREF="ch30_05.htm"TITLE="Edits Between Files ">30.5</A>)</SPAN>.So if you're also using named buffers for copying and pasting text, you'llneed to share them with your <CODECLASS="literal">@</CODE>-functions.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-9913-SECT-1.1">31.4.1 Defining and Using Simple @-Functions </A></H3><PCLASS="para">To define an <CODECLASS="literal">@</CODE>-function:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Enter the command(s) you want to execute onto one or more lines of thefile you're editing.</P></LI><LICLASS="listitem"><PCLASS="para">Yank or delete the line(s) into a named buffer with a command like<CODECLASS="literal">&quot;ay$</CODE> or <CODECLASS="literal">&quot;bD</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para">To use the function, type a command like <CODECLASS="literal">@a</CODE> or <CODECLASS="literal">@b</CODE>.You can repeat the function by typing <CODECLASS="literal">@@</CODE> or a dot (<CODECLASS="literal">.</CODE>).Use <CODECLASS="literal">u</CODE> or <CODECLASS="literal">U</CODE> to undo the effects of the <CODECLASS="literal">@</CODE>-function.</P></LI></OL><PCLASS="para">Here's an example.You're editing a long HTML file with lines like these:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">&lt;STRONG&gt;<CODECLASS="replaceable"><I>Some heading here</I></CODE>&lt;/STRONG&gt;&lt;STRONG&gt;<CODECLASS="replaceable"><I>Another heading here</I></CODE>&lt;/STRONG&gt;</PRE></BLOCKQUOTE></P><PCLASS="para">When you see one of those lines, you need to change the <CODECLASS="literal">STRONG</CODE>sto either <CODECLASS="literal">H3</CODE> or <CODECLASS="literal">H4</CODE>.A global substitution with <CODECLASS="literal">:%s</CODE> won't do the job becausesome lines need <CODECLASS="literal">H3</CODE> and others need <CODECLASS="literal">H4</CODE>; you have todecide line-by-line as you work through the file.So you define the function <CODECLASS="literal">@a</CODE> to change a line to <CODECLASS="literal">H3</CODE>,and <CODECLASS="literal">@b</CODE> to change to <CODECLASS="literal">H4</CODE>.</P><PCLASS="para">To design an <CODECLASS="literal">@</CODE>-function, start by thinking how you'dmake the changes by hand.You'd probably move to the start of the line with <CODECLASS="literal">0</CODE>, move tothe right one character with <CODECLASS="literal">l</CODE>, type <CODECLASS="literal">cw</CODE> to change theword <CODECLASS="literal">STRONG</CODE>, and type in <CODECLASS="literal">H3</CODE> (or <CODECLASS="literal">H4</CODE>).Then press ESC to return to command mode.Go to the end of the line with <CODECLASS="literal">$</CODE>, move to the character afterthe slash with <CODECLASS="literal">T/</CODE>, then change the second <CODECLASS="literal">STRONG</CODE> theway you fixed the first one.</P><PCLASS="para">To define the function, open a new empty line of your file(go into text-input mode).Then type the keystrokes that will make the <CODECLASS="literal">H3</CODE> changes;<SPANCLASS="link">type CTRL-v before each ESC or RETURN (<ACLASS="linkend"HREF="ch31_06.htm"TITLE="Protecting Keys from Interpretation by ex ">31.6</A>)</SPAN>.When you're done, press ESC again to go to command mode.Because the commands for the <CODECLASS="literal">H4</CODE> change are similar, the easiestway to make them is by copying and pasting the line for <CODECLASS="literal">H3</CODE>(by typing <CODECLASS="literal">yy</CODE> and <CODECLASS="literal">p</CODE>)-then edit the copy.The pair of command lines should look like this (where <CODECLASS="literal">^[</CODE> standsfor the <KBDCLASS="keycap">[CTRL-v]</KBD> <KBDCLASS="keycap">[ESC]</KBD> keys):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">0lcwH3^[$T/cwH3^[0lcwH4^[$T/cwH4^[</PRE></BLOCKQUOTE></P><PCLASS="para">Move to the start of the first line and delete it into the <EMCLASS="emphasis">a</EM>buffer by typing <CODECLASS="literal">&quot;aD</CODE>.Go to the next line and type <CODECLASS="literal">&quot;bD</CODE>.(This will leave two empty lines; delete them with <CODECLASS="literal">dd</CODE> if you'dlike.)Now, when you type <CODECLASS="literal">@a</CODE>, it will execute the commands to change aline to <CODECLASS="literal">H3</CODE>; typing <CODECLASS="literal">@b</CODE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -