📄 ch30_23.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.23 Safer vi Filter-Throughs </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:45:41Z"><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_22.htm"TITLE="30.22 Filtering Text Through a UNIX Command "><LINKREL="next"HREF="ch30_24.htm"TITLE="30.24 vi/ex File Recovery vs. Networked Filesystems "></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_22.htm"TITLE="30.22 Filtering Text Through a UNIX Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.22 Filtering Text Through a UNIX Command "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_24.htm"TITLE="30.24 vi/ex File Recovery vs. Networked Filesystems "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.24 vi/ex File Recovery vs. Networked Filesystems "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0151">30.23 Safer vi Filter-Throughs </A></H2><PCLASS="para">Sometimes when I do a filter-through-especially on some buggy, oldversions of <EMCLASS="emphasis">vi</EM>-it completely scrambles and trashes my text.Things can be so bad that the <EMCLASS="emphasis">u</EM> (undo) command won't work.Unless I've been careful to write my buffer (with <CODECLASS="literal">:w</CODE>)before the filter-through,I can lose the whole file!I've seen the same problem on several versions of <EMCLASS="emphasis">vi</EM> and readarticles on Usenet from people who've been bitten by it.</P><PCLASS="para">If you've been burned, too, you might want to use the<SPANCLASS="link">keymaps (<ACLASS="linkend"HREF="ch31_02.htm"TITLE="Save Time and Typing with the vi map Commands ">31.2</A>)</SPAN>below.Map one of your keys - a numbered function key like F4, if you can - to dofilter-throughs of your whole file.To start a filter-through, press the F4 key - this invokes the firstmap below.Next, type the UNIX command you want to run.To run the filter-through, press F4 again.</P><PCLASS="para">The maps are shown here:enter the control characters <CODECLASS="literal">^M</CODE> <CODECLASS="literal">^V</CODE>, and <CODECLASS="literal">^[</CODE><SPANCLASS="link">by typing CTRL-v first (<ACLASS="linkend"HREF="ch31_06.htm"TITLE="Protecting Keys from Interpretation by ex ">31.6</A>)</SPAN>.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> map #4 :se noaw^M:w^M:%d^M:r !map! #4 ^V '%'^M:1d^[:se aw^[</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-33877"></A>The first one (<CODECLASS="literal">map #4</CODE>) maps the F4 key during command mode toset the <EMCLASS="emphasis">noautowrite</EM> option (<CODECLASS="literal">:se noaw</CODE>), write the buffer toyour file (<CODECLASS="literal">:w</CODE>), delete all lines in the buffer (<CODECLASS="literal">:%d</CODE>),and start a shell command line(<SPANCLASS="link"><CODECLASS="literal">:r !</CODE> (<ACLASS="linkend"HREF="ch33_04.htm"TITLE="Useful ex Commands ">33.4</A>)</SPAN>).After pressing F4 from command mode, your cursor should be on the bottomline, ready for you to type the filter-through:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:r !</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Type the UNIX command (like <CODECLASS="literal">expand</CODE>, <CODECLASS="literal">fmt -75</CODE>, and so on).Then press F4 again.Because you're in text-input mode, the second map above (<CODECLASS="literal">map! #4</CODE>)will be used this time.</P><PCLASS="para">The map starts with CTRL-v and a space; this trick puts a space betweenthe command line you type and the filename.Next, the current filename(<SPANCLASS="link"><CODECLASS="literal">%</CODE> (<ACLASS="linkend"HREF="ch30_04.htm"TITLE="Editing Multiple Files with vi ">30.4</A>)</SPAN>)-in single quotes, to protectspecial characters from the shell - is output, followed by a RETURNto execute the command.Reading a UNIX command with <CODECLASS="literal">:r !</CODE> leaves a blank line above;the <CODECLASS="literal">:1d</CODE> deletes that line (it'll be the line 1 in the buffer).Finally, the <CODECLASS="literal">:se aw</CODE> command turns on the <EMCLASS="emphasis">autowrite</EM> optionagain.</P><BLOCKQUOTECLASS="caution"><PCLASS="para"><STRONG>CAUTION:</STRONG> If you never set the <EMCLASS="emphasis">autowrite</EM> option, you can leave out the<CODECLASS="literal">:se noaw^M</CODE> and <CODECLASS="literal">:se aw^M</CODE>.But be sure - if autowrite is set when you use this map, all lines inyour file will be deleted!(The empty buffer will be auto-written when the shell command runs.)</P></BLOCKQUOTE><PCLASS="para"> I guess it figures <CODECLASS="literal">:-(</CODE>-thistricky set of keymaps doesn't work perfectly onevery version of <EMCLASS="emphasis">vi</EM>.On my old SunOS 4.1 version of <EMCLASS="emphasis">vi</EM>, for instance, it left me in text-input mode;I had to hit ESC after pressing F4 the second time.</P><PCLASS="para">Still, this is a lot more reliable than the normal way to dofilter-throughs on buggy versions of <EMCLASS="emphasis">vi</EM>.It's been especially nice because I've always been able to undo thefilter-through with the <EMCLASS="emphasis">u</EM> (undo) or <CODECLASS="literal">:e!</CODE> commands. Ihaven't used these keymaps in several years, but filter-throughs aresuch an important <EMCLASS="emphasis">vi</EM> feature to me that I'll always keep them handy.</P><ACLASS="indexterm"NAME="AUTOID-33914"></A><ACLASS="indexterm"NAME="AUTOID-33915"></A><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_22.htm"TITLE="30.22 Filtering Text Through a UNIX Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.22 Filtering Text Through a UNIX Command "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_24.htm"TITLE="30.24 vi/ex File Recovery vs. Networked Filesystems "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.24 vi/ex File Recovery vs. Networked Filesystems "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.22 Filtering Text Through a UNIX Command </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.24 vi/ex File Recovery vs. Networked Filesystems </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 + -