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

📄 ch30_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.10 Confirming Substitutions in ex and vi</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:25Z"><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_09.htm"TITLE="30.9 Using Search Patterns and Global Commands"><LINKREL="next"HREF="ch30_11.htm"TITLE="30.11 Keep Your Original File, Write to a New File "></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_09.htm"TITLE="30.9 Using Search Patterns and Global Commands"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.9 Using Search Patterns and Global Commands"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_11.htm"TITLE="30.11 Keep Your Original File, Write to a New File "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.11 Keep Your Original File, Write to a New File "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6330">30.10 Confirming Substitutions in ex and vi</A></H2><PCLASS="para">It makes sense to be overly careful when using a search and replace command.<ACLASS="indexterm"NAME="AUTOID-33126"></A><ACLASS="indexterm"NAME="AUTOID-33129"></A><ACLASS="indexterm"NAME="AUTOID-33132"></A><ACLASS="indexterm"NAME="AUTOID-33134"></A><ACLASS="indexterm"NAME="AUTOID-33137"></A>It sometimes happens that what you get is not what you expect.You can undo any search and replacement command byentering <CODECLASS="literal">u</CODE>, provided that the command was intended for the most recentedit you made.But you don't always catch undesired changes until it is too lateto undo them.Another way to protect your edited file is to save the file with<CODECLASS="literal">:w</CODE> before performing a global replacement.Then at least you can quit the file without saving your edits andgo back to where you were before the change was made.You can also read theprevious version of the buffer back in with<SPANCLASS="link"><CODECLASS="literal">:e!</CODE> (<ACLASS="linkend"HREF="ch30_04.htm"TITLE="Editing Multiple Files with vi ">30.4</A>)</SPAN>.</P><PCLASS="para">It's wise to be cautious and know exactly what is going to bechanged in your file.If you'd like to see what the search turns up  and confirm each replacement before it is made, add the <CODECLASS="literal">c</CODE> option (for confirm)at the end of the substitute command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:1,30s/his/the/gc</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This command will display the entire line where the string has been located, and the string will be marked by a seriesof carets (<CODECLASS="literal">^^^^</CODE>).</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">copyists at his school              ^^^_</PRE></BLOCKQUOTE></P><PCLASS="para">If you want to make the replacement, you must enter <CODECLASS="literal">y</CODE> (for yes)'vs 12and press RETURN.If you don't want to make a change, simply pressRETURN.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">this can be used for invitations, signs, and menus.   ^^^_</PRE></BLOCKQUOTE></P><PCLASS="para">The combination of the <EMCLASS="emphasis">vi</EM> commands, <CODECLASS="literal">n</CODE> (repeat last search) and dot (<CODECLASS="literal">.</CODE>)(repeat last command), is also an extraordinarily useful andquick way to page through a file and make repetitive changes thatyou may not want to make globally. So, for example, if your editor has told you that you're using <EMCLASS="emphasis">which</EM> when you should be using <EMCLASS="emphasis">that</EM>, you can spot-checkevery occurrence of <EMCLASS="emphasis">which</EM>, changing only those that are incorrect:<TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">/which</CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Search for <EMCLASS="emphasis">which</EM>.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">cwthat</CODE> <KBDCLASS="keycap">[ESC]</KBD></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Change to <EMCLASS="emphasis">that</EM>.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">n</CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Repeat search.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">.</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Repeat change (if appropriate).</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">.</TD></TR></TBODY></TABLE>This often turns out to be faster than using a globalsubstitution with confirmation.[It also lets you see other lines near the text you're checking. -<EMCLASS="emphasis">JP</EM>&nbsp;]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD, TOR</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_09.htm"TITLE="30.9 Using Search Patterns and Global Commands"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.9 Using Search Patterns and Global Commands"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_11.htm"TITLE="30.11 Keep Your Original File, Write to a New File "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.11 Keep Your Original File, Write to a New File "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.9 Using Search Patterns and Global Commands</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.11 Keep Your Original File, Write to a New File </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 + -