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

📄 ch30_19.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.19 Modelines: Bug or Feature?</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:33Z"><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_18.htm"TITLE="30.18 Setting vi Options Automatically for Individual Files"><LINKREL="next"HREF="ch30_20.htm"TITLE="30.20 Multiple Editor Setup Files; Starting with a Search "></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_18.htm"TITLE="30.18 Setting vi Options Automatically for Individual Files"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.18 Setting vi Options Automatically for Individual Files"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_20.htm"TITLE="30.20 Multiple Editor Setup Files; Starting with a Search "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.20 Multiple Editor Setup Files; Starting with a Search "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5210">30.19 Modelines: Bug or Feature?</A></H2><PCLASS="para">Some versions of <EMCLASS="emphasis">vi</EM> and <EMCLASS="emphasis">ex</EM> have a <EMCLASS="emphasis">modeline</EM> or<EMCLASS="emphasis">modelines</EM> option.<ACLASS="indexterm"NAME="AUTOID-33588"></A><ACLASS="indexterm"NAME="AUTOID-33590"></A><ACLASS="indexterm"NAME="AUTOID-33593"></A>When that option is set in your<SPANCLASS="link"><EMCLASS="emphasis">.exrc</EM> file (<ACLASS="linkend"HREF="ch30_06.htm"TITLE="Local Settings for vi and ex ">30.6</A>)</SPAN>,you can store setup commands at the top or bottom of any file you edit.When you start the editor, it will read the setup commands and run them.This is a lot like having a separate<EMCLASS="emphasis">.exrc</EM>for each file you edit.</P><BLOCKQUOTECLASS="caution"><PCLASS="para"><STRONG>CAUTION:</STRONG> Modelines can be a security problem.If any troublemakers should edit your files and change your modelines, thinkabout the difficulties they can cause you.Most newer versions of <EMCLASS="emphasis">vi</EM> disable modelines by default.</P></BLOCKQUOTE><PCLASS="para">Here's a sample file&nbsp;- a<SPANCLASS="link">shell script (<ACLASS="linkend"HREF="ch44_01.htm#UPT-ART-0065"TITLE="Everyone Should Learn Some Shell Programming ">44.1</A>)</SPAN>with a modeline on the second line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#! /bin/sh# vi:set number wrapmargin=0 autoindent showmatch:while read linedo   ...</PRE></BLOCKQUOTE></P><PCLASS="para">The modeline has <CODECLASS="literal">#</CODE>, the shell's comment character, at the startof it&nbsp;- so, the shell will ignore the line but <EMCLASS="emphasis">vi</EM> will still read it.This is only necessary in a shell script, but it demonstrates that themodeline need not start in column 1. The modeline itselfconsists of a space or tab, the string <EMCLASS="emphasis">vi:</EM> or <EMCLASS="emphasis">ex:</EM>, thecommands to be executed, and a closing colon. Both the space or tabbefore the modeline, and the closing colon are important&nbsp;- theytell the editor where the modeline begins and ends. Youcan put modelines on the first five or last five lines of a file(or both).</P><PCLASS="para">When you start <EMCLASS="emphasis">vi</EM> on the file shown in the example above, it sets the options <EMCLASS="emphasis">number,</EM><EMCLASS="emphasis">wrapmargin=0</EM>, <EMCLASS="emphasis">autoindent</EM>, and <EMCLASS="emphasis">showmatch</EM>.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> Any time you open a file with a modeline, <EMCLASS="emphasis">vi</EM> changes the filestatus to &quot;modified&quot;-even if you haven't actually made any changes.To leave the file without writing it, you have to use the <CODECLASS="literal">:q!</CODE>command.This is a hassle when you use UNIX tools that depend on a file'smodification time, like<SPANCLASS="link"><EMCLASS="emphasis">make</EM> (<ACLASS="linkend"HREF="ch28_13.htm"TITLE="make Isn't Just for Programmers! ">28.13</A>)</SPAN>,especially if you also have the <EMCLASS="emphasis">autowrite</EM> option set.</P></BLOCKQUOTE><PCLASS="para">To find out whether your version of <EMCLASS="emphasis">vi</EM> supports modelines&nbsp;- andwhether the option is called <EMCLASS="emphasis">modeline</EM> or <EMCLASS="emphasis">modelines</EM>-geta list of all options with the command <CODECLASS="literal">:set</CODE> <CODECLASS="literal">all</CODE>.If the option is available, but not set, you'll see <EMCLASS="emphasis">nomodeline</EM>(or <EMCLASS="emphasis">nomodelines</EM>)as one of the options. Put the command</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set modeline(s)</PRE></BLOCKQUOTE></P><PCLASS="para">in your <EMCLASS="emphasis">.exrc</EM> file to enable the option.Unfortunately, some versions list the option but don't support it!</P><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_18.htm"TITLE="30.18 Setting vi Options Automatically for Individual Files"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.18 Setting vi Options Automatically for Individual Files"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_20.htm"TITLE="30.20 Multiple Editor Setup Files; Starting with a Search "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.20 Multiple Editor Setup Files; Starting with a Search "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.18 Setting vi Options Automatically for Individual Files</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.20 Multiple Editor Setup Files; Starting with a Search </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 + -