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

📄 ch30_27.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.27 vi Compound Searches </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:45:46Z"><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="ch30_01.htm"TITLE="30. vi Tips and Tricks"><LINKREL="prev"HREF="ch30_26.htm"TITLE="30.26 Shell Escapes: Running One UNIX Command While Using Another"><LINKREL="next"HREF="ch30_28.htm"TITLE="30.28 Keep Track of Functions and Included Files with ctags and tags"></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="ch30_26.htm"TITLE="30.26 Shell Escapes: Running One UNIX Command While Using Another"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.26 Shell Escapes: Running One UNIX Command While Using Another"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 30<BR>vi Tips and Tricks</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_28.htm"TITLE="30.28 Keep Track of Functions and Included Files with ctags and tags"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.28 Keep Track of Functions and Included Files with ctags and tags"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0141">30.27 vi Compound Searches </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-34055"></A><ACLASS="indexterm"NAME="AUTOID-34058"></A><ACLASS="indexterm"NAME="AUTOID-34061"></A>You probably know that you can search for a word or phrase with the <EMCLASS="emphasis">vi</EM>command <CODECLASS="literal">/</CODE> (slash):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>/treasure</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you have a file that uses the same word over and over again, you might want tofind one particular place that the word is used.You can repeat the search with the <EMCLASS="emphasis">n</EM> command until you find theplace you want.That can take time and work, though.</P><PCLASS="para">For example, suppose you want to find the word &quot;treasure&quot;in the sentence that haswords something like &quot;Los Alamos residents...treasure,&quot; butyou can't remember exactly how the sentence is written.You could use wildcards in your<SPANCLASS="link">regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>/Los Alamos.*treasure</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">but then the phrases &quot;Los Alamos&quot; and &quot;treasure&quot; have to be on the same lineof the file you're searching&nbsp;- and they won't always be.Also, you want your cursor on the word <EMCLASS="emphasis">treasure</EM>, but that search wouldput the cursor on <EMCLASS="emphasis">Los</EM> instead.</P><PCLASS="para">&quot;Hmmm,&quot; you say,&quot;How about two separate searches, like this?&quot;</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/Los Alamos/treasure</PRE></BLOCKQUOTE></P><PCLASS="para">The problem there is: the file might have the phrase &quot;Los Alamos&quot; allthrough it; you might have to type <EMCLASS="emphasis">n</EM> over and over until you get tothe sentence with <EMCLASS="emphasis">treasure</EM>.</P><PCLASS="para">Here's the easy way: a compound search.Say your cursor is on line 1 of the following file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">Before the second World War, there was a treasured boys' school inwhat was to become the city of Los Alamos, New Mexico. The school atLos Alamos changed the lives and made a lifelong impression on most boyswho attended. One of the boys who attended the Los Alamos school went onto propose that remote set of mesas as a site for the U.S. Government's   ...Since the war ended, most of the boys' school ranch buildings have been torndown or replaced. But there's one building that Los Alamos residents stilluse and treasure. It's The Lodge, a log building on the edge of what's now   ...</PRE></BLOCKQUOTE></P><PCLASS="para">Type the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>/Los Alamos/;/treasure/</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That means &quot;find the first occurrence of <EMCLASS="emphasis">treasure</EM> just after<EMCLASS="emphasis">Los Alamos</EM>.&quot;Starting at the top of the example above, that search will skip past all the<EMCLASS="emphasis">treasure</EM> and <EMCLASS="emphasis">Los Alamos</EM> words until it finds the word <EMCLASS="emphasis">treasure</EM>on the last line shown.(It's probably smarter to type just <CODECLASS="literal">/Alamos/;/treasure/</CODE> in case the<EMCLASS="emphasis">Los Alamos</EM> is split across two lines of the file.)</P><PCLASS="para">Another example: a C programmer who wants to find the <EMCLASS="emphasis">printf</EM> functioncall just after the line where <EMCLASS="emphasis">i</EM> is incremented by two (<CODECLASS="literal">i&nbsp;+=&nbsp;2</CODE>).She could type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>/i += 2/;/printf/</B></CODE></PRE></BLOCKQUOTE></P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> You can't repeat a compound search by typing <CODECLASS="literal">n</CODE>.The easiest way is to define the search as a<SPANCLASS="link">key map (<ACLASS="linkend"HREF="ch31_02.htm"TITLE="Save Time and Typing with the vi map Commands ">31.2</A>)</SPAN>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch31_06.htm"TITLE="31.6 Protecting Keys from Interpretation by ex ">^M</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:map g /Los Alamos/;/treasure/^M</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">and use (in this case) <CODECLASS="literal">g</CODE> to repeat the search.</P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch30_26.htm"TITLE="30.26 Shell Escapes: Running One UNIX Command While Using Another"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.26 Shell Escapes: Running One UNIX Command While Using Another"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="ch30_28.htm"TITLE="30.28 Keep Track of Functions and Included Files with ctags and tags"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.28 Keep Track of Functions and Included Files with ctags and tags"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.26 Shell Escapes: Running One UNIX Command While Using Another</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">30.28 Keep Track of Functions and Included Files with ctags and tags</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 &amp; 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 + -