123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <HTML>
- <HEAD>
- <META NAME="Description" CONTENT="Learn Unix/Solaris: Learn how to customize the Unix vi editor using the .exrc file. Examples for Unix, Solaris, and Linux users.">
- <META NAME="KEYWORDS" CONTENT="learn, vi, learn vi, solaris, unix, vi tutorial, tutorial, vi tips, macros, learn solaris, learn unix, tips, solaris tips, unix tutorials, editor">
- <TITLE>Learn - Unix - vi - Customizing the vi editor</TITLE>
- </HEAD>
- <BODY BGCOLOR="#ffffff" text="#000000">
- <P><CENTER><B><FONT SIZE=+2>Customizing the vi editor</FONT></B><br>
- <font size=+1><a href="/">Developer's Daily</a></font></CENTER></P>
- <P><B><FONT SIZE=+1>vi background</FONT></B>
- <P>For years Unix/Solaris/Linux users have had a love/hate relationship
- with the vi editor - in general, they've loved to hate it. As the
- standard Unix/Solaris editor, many of
- the problems Unix users have with vi can be alleviated by learning how to set
- a few configuration options in the vi editor's startup file, <TT>.exrc</TT>.
- Unfortunately, many users never learn about this vi startup file, and how much easier it makes the vi editor.
- <p>In this Unix/Solaris/Linux tutorial, we'll learn some of the
- most useful vi editor startup options. We'll also create
- a few vi macros, and learn how to define the <TT>F1</TT> function key to
- display a "Help" screen to make the vi editor much easier to use.
- <BR>
- <!-- AD CODE STARTS -->
- <center>
- <!--#include virtual="/global/unix_ad1.shtml" -->
- </center>
- <!-- AD CODE STOPS -->
- <P><B><FONT SIZE=+1>The vi configuration file</FONT></B></P>
- <P>When you start the vi editor, the editor searches for the environment
- variable <TT>$EXINIT</TT> and uses the contents of the file it points to
- as configuration commands, if it exists. If <TT>EXINIT</TT> is not
- defined, vi looks for the <TT>.exrc</TT> file in your <TT>HOME</TT> directory,
- and uses its configuration commands. Finally, vi looks in your current
- directory for a file named <TT>.exrc</TT> and executes the commands in
- that file, if it exists. In this manner, you can have a different
- vi configuration for each directory or project that you're working on.
- In this article we'll modify the .exrc file in our home directory, which
- will effect all of our vi editing sessions.
- <P>The <TT>.exrc</TT> file can contain comments, last-line commands (those
- entered on the last line of the vi editor and beginning with a ":", such
- as ":set showmode"), and macro definitions.
- <P>Comments in the <TT>.exrc</TT> file are those lines that begin with
- a <TT>"</TT> character (double-quote character). All characters after
- the <TT>"</TT> are ignored by vi. You can include as many comments
- in the <TT>.exrc</TT> file as you like.
- <BR>
- <P><B><FONT SIZE=+1>Showing the current mode</FONT></B>
- <P>One of the biggest complaints about vi is that you never know what mode
- you're in - there's nothing on screen to indicate if you're in "command
- mode" or "insert mode". This complaint is easily cured by the "<TT>set
- showmode</TT>" command.
- <P>When you're in vi, the "<TT>:set showmode</TT>" command shows what mode
- vi is in when you're typing - insert mode, append mode, etc. If you're
- in command mode, like you are when you first enter vi, you won't see anything
- different. But as soon as you enter insert mode, the words "INSERT
- MODE" are displayed in the lower-right corner on your screen.
- <P>You enable this useful feature by putting the "set showmode" command
- in the <TT>.exrc</TT> file, as shown in <B>Figure 1</B>. That's all
- there is to it. The next time you enter the vi editor, the <TT>.exrc</TT>
- file will be read automatically, and this feature will be enabled for you.
- One warning - don't put any blank lines in the <TT>.exrc</TT> file.
- vi doesn't care for blank lines in the configuration file at all, and your
- configuration options may be ignored.
- <BR>
- <TABLE BORDER=0 CELLPADDING=10 BGCOLOR="#CCCCCC" >
- <TR>
- <TD><TT>"</TT>
- <BR><TT>" This line is a comment. Commented lines begin with the
- " character</TT>
- <BR><TT>" in the first column.</TT>
- <BR><TT>"</TT>
- <BR><TT>" Use the 'showmode' command to display what mode I'm in when using
- vi. </TT>
- <BR><TT>"</TT>
- <BR><TT>set showmode</TT>
- <BR><TT>"</TT>
- <BR><TT>" Block messages from other users to keep my display clean.</TT>
- <BR><TT>"</TT>
- <BR><TT>set nomesg</TT>
- <BR><TT>"</TT>
- <BR> </TD>
- </TR>
- </TABLE>
-
- <TABLE CELLSPACING=0 CELLPADDING=0 >
- <TR>
- <TD ALIGN=LEFT VALIGN=TOP NOWRAP><B><FONT COLOR="#000099">Figure 1: </FONT></B></TD>
- <TD ALIGN=LEFT VALIGN=TOP>A sample <TT>.exrc</TT> file. </TD>
- </TR>
- <TR>
- <TD></TD>
- <TD></TD>
- </TR>
- </TABLE>
-
- <P>The "<TT>:set nomesg</TT>" command shown in <B>Figure 1</B> is also
- very useful. It keeps other users from writing information onto your
- screen (such as when using the "talk" command, for example) when you're
- using vi.
- <BR>
- <P><B><FONT SIZE=+1>Map commands</FONT></B>
- <P>"Map" commands let you customize the vi editor by allowing you to redefine
- the meaning of keys when you're in command mode. In <B>Figure 2</B>,
- we define the <TT>F1</TT> function key to be a "Help" key, and the <TT>F2</TT>
- function key to be a shortcut to write our current edit changes to a file,
- similar to the "<TT>:w!</TT>" command, but shorter and easier.
- <BR>
- <BR>
- <TABLE BORDER=0 CELLPADDING=10 BGCOLOR="#CCCCCC" >
- <TR>
- <TD><TT>"</TT>
- <BR><TT>" This line is a comment. Commented lines begin with the
- " character</TT>
- <BR><TT>" in the first column.</TT>
- <BR><TT>"</TT>
- <BR><TT>" Use the 'showmode' command to display what mode I'm in when using
- vi. </TT>
- <BR><TT>"</TT>
- <BR><TT>set showmode</TT>
- <BR><TT>"</TT>
- <BR><TT>" Block messages from other users to keep my display clean.</TT>
- <BR><TT>"</TT>
- <BR><TT>set nomesg</TT>
- <BR><TT>"</TT>
- <BR><TT>" Define the F1 key to show a customized "help" file</TT>
- <BR><TT>"</TT>
- <BR><TT>:map #1 :!more ~/.vi_help^M</TT>
- <BR><TT>"</TT>
- <BR><TT>" Define the F2 key to be a shortcut to save current changes
- to file</TT>
- <BR><TT>" (uses the current filename)</TT>
- <BR><TT>"</TT>
- <BR><TT>:map #2 :w^M</TT>
- <BR><TT>"</TT>
- <BR> </TD>
- </TR>
- </TABLE>
-
- <TABLE CELLSPACING=0 CELLPADDING=0 >
- <TR>
- <TD ALIGN=LEFT VALIGN=TOP NOWRAP><B><FONT COLOR="#000099">Figure 2: </FONT></B></TD>
- <TD ALIGN=LEFT VALIGN=TOP>This sample <TT>.exrc</TT> file shows how to
- change the behavior of your function keys with the <TT>map</TT> command. </TD>
- </TR>
- <TR>
- <TD></TD>
- <TD></TD>
- </TR>
- </TABLE>
-
- <P>Both of these new function key definitions are created with the "<TT>:map</TT>"
- command. Let's break down the "map" commands in our sample file to see
- what they're really doing.
- <P>In the <TT>.exrc</TT> file, #1 refers to function key <TT>F1</TT>, and
- #2 refers to the <TT>F2</TT> function key. So the "<TT>:map #1</TT>"
- portion of the first line means "map the function key <TT>F1</TT>", and
- "<TT>:map #2</TT>" means "map the function key <TT>F2</TT>".
- <P>After the "<TT>:map #1</TT>" portion of the first command, you see the
- character sequence "<TT>:!more ~/.vi_help^M</TT>". If you're familiar
- with vi, you may know that the "<TT>:!</TT>" sequence allows you to run
- any Unix command while you're in the vi editor. For instance, while
- you're in command mode in vi, you can type "<TT>:!ls -al</TT>" (followed
- by the <TT><Enter></TT> key) to get a long listing of all files in your
- current directory. This is great, because you don't have to exit
- vi and then re-enter just to run the "<TT>ls -al</TT>" command, saving
- you a lot of keystrokes.
- <P>That said, you can see how this extends to what we're doing with the
- <TT>F1</TT> key. We're telling vi that every time the user hits the
- <TT>F1</TT> key, run the external Unix command "<TT>more ~/.vi_help</TT>".
- This means "use the Unix 'more' command to display the contents of the
- file '<TT>.vi_help</TT>' located in our <TT>HOME</TT> directory".
- The file "<TT>.vi_help</TT>" is any file you create - our example file
- is shown in <B>Figure 3</B>.
- <BR>
- <BR>
- <TABLE BORDER=0 CELLPADDING=10 BGCOLOR="#CCCCCC" >
- <TR>
- <TD><TT>
- ====================</TT>
- <BR><TT>
- |
- |</TT>
- <BR><TT>
- | vi Help Screen |</TT>
- <BR><TT>
- |
- |</TT>
- <BR><TT>
- ====================</TT>
- <BR>
- <P><TT>Customized Function Keys</TT>
- <BR><TT>------------------------</TT>
- <P><TT> F1 - Help Screen (this
- screen message)</TT>
- <BR><TT> F2 - Save Changes</TT>
- <BR>
- <BR>
- <P><TT>Command-Mode Commands</TT>
- <BR><TT>=====================</TT>
- <BR>
- <P><TT> Commands To Insert Text</TT>
- <BR><TT> -----------------------</TT>
- <P><TT> a - Append after the current
- cursor position</TT>
- <BR><TT> A - Append after the end of
- the current line</TT>
- <BR><TT> i - Insert at current cursor
- position</TT>
- <BR><TT> I - Insert at beginning of
- line</TT>
- <BR><TT> o - Open a new line below
- the current line (lower-case letter o) </TT>
- <BR><TT> O - Open a new line above
- the current line (upper-case letter O)</TT>
- <BR>
- <P><TT> Commands to Delete Text</TT>
- <BR><TT> -----------------------</TT>
- <P><TT> x - Delete the character at
- the current cursor position</TT>
- <BR><TT> dd - Delete the current line</TT>
- <BR><TT> ndd - Delete the next 'n' lines, including
- the current line</TT>
- <BR><TT> d$ - Delete from the current position
- to the end of the line</TT>
- <BR>
- <P><TT> Movement Commands</TT>
- <BR><TT> -----------------</TT>
- <P><TT> 0 - Move to the beginning of
- the current line (number zero)</TT>
- <BR><TT> $ - Move to the end of the
- current line</TT>
- <BR><TT> b - Move backwards one word
- in the current file</TT>
- <BR><TT> w - Move forward to the next
- word</TT>
- <BR><TT> G - Move to the end of the
- current file</TT>
- <BR><TT> nG - Move to line 'n'</TT>
- <BR>
- <P><TT> Cutting and Pasting Commands</TT>
- <BR><TT> ----------------------------</TT>
- <P><TT> yy - Yank the current line (i.e.,
- copy it to buffer)</TT>
- <BR><TT> nyy - Yank the next 'n' lines, including
- the current line</TT>
- <BR><TT> pp - Paste the content of the buffer</TT>
- <BR> </TD>
- </TR>
- </TABLE>
-
- <TABLE CELLSPACING=0 CELLPADDING=0 >
- <TR>
- <TD ALIGN=LEFT VALIGN=TOP NOWRAP><B><FONT COLOR="#000099">Figure 3: </FONT></B></TD>
- <TD ALIGN=LEFT VALIGN=TOP>You can create a <TT>.vi_help</TT> file to act as a help
- screen within the vi editor. </TD>
- </TR>
- <TR>
- <TD></TD>
- <TD></TD>
- </TR>
- </TABLE>
-
- <P>Now, every time the user hits the <TT>F1</TT> key, they will actually
- use the <TT>more</TT> command to view the customized file <TT>.vi_help</TT>
- located in their <TT>HOME</TT> directory. But what's the <TT>^M</TT>
- at the end of the line? Do we just type the character <TT>^</TT>
- followed by the letter <TT>M</TT>?
- <P>The answer is no. The <TT>^M</TT> you see in <B>Figure 3</B> is
- actually generated by typing a <TT><CTRL-v></TT> (holding down the 'Ctrl'
- key and the 'v' key at the same time) followed by a <TT><CTRL-m></TT>.
- <P>This key sequence embeds special characters into our file to tell vi
- we want to automatically generate an <TT><Enter></TT> key signal at
- this point in the file. If you're familiar with your ASCII codes,
- you know that hitting the <TT><Enter></TT> key is the same as typing
- a <TT><CTRL-m></TT>. Try typing a <TT><CTRL-m></TT> at the
- Unix command line if you like - it's the same as the <TT><Enter></TT>
- key. When you use the <TT><CTRL-v><CTRL-m></TT> key sequence,
- you actually generate only one character in your <TT>.exrc</TT> file, not
- two separate characters.
- <P>If we don't tell vi to put an <TT><Enter></TT> key keystroke at this
- point, vi will display the "<TT>:!more ~/.vi_help</TT>" command on the
- last line of your vi editor screen when you hit the <TT>F1</TT> function
- key, but it won't execute the command until you manually hit the <TT><Enter></TT>
- key, which isn't what we want. Think of the <TT><CTRL-v><CTRL-m></TT>
- key sequence as automatically hitting the <TT><Enter></TT> key for you.
- <P>The <TT>F2</TT> function key mapping will seem easy now. When
- the user hits the <TT>F2</TT> key, vi automatically issues the "<TT>:w</TT>"
- write command, and then generates an <TT><Enter></TT> key keystroke,
- saving your current file to disk.
- <P>Other useful macros are shown in <B>Figure 4</B>. <TT>F3</TT>
- is set to display line numbers on screen, <TT>F4</TT> is set to take the
- line numbers off screen, and <TT>F5</TT> configures automatic indentation
- of lines. All of these functions are very useful when writing programs.
- <BR>
- <BR>
- <TABLE BORDER=0 CELLPADDING=10 BGCOLOR="#CCCCCC" >
- <TR>
- <TD><TT>set showmode</TT>
- <BR><TT>set nomesg</TT>
- <BR><TT>:map #1 :!more ~/.vi_help^M </TT>
- <BR><TT>:map #2 :w^M</TT>
- <BR><TT>:map #3 :set number^M</TT>
- <BR><TT>:map #4 :set nonumber^M</TT>
- <BR><TT>:map #5 :set autoindent^M</TT>
- <BR> </TD>
- </TR>
- </TABLE>
-
- <TABLE CELLSPACING=0 CELLPADDING=0 >
- <TR>
- <TD ALIGN=LEFT VALIGN=TOP NOWRAP><B><FONT COLOR="#000099">Figure 4: </FONT></B></TD>
- <TD ALIGN=LEFT VALIGN=TOP>This <TT>.exrc</TT> file demonstrates useful
- macros for the <TT>F1</TT> through <TT>F5</TT> function keys. </TD>
- </TR>
- <TR>
- <TD></TD>
- <TD></TD>
- </TR>
- </TABLE>
-
- <BR>
- <P><B><FONT SIZE=+1>Conclusion</FONT></B>
- <P>You have now seen how to customize your own <TT>.exrc</TT> file to display
- your current working mode, keep other users messages off of your display,
- and create your own macros to make your life with the vi editor easier
- and more productive. You can now apply the techniques included in
- this article to customize vi to include your own working preferences.
- <BR>
- <!-- AD CODE STARTS -->
- <center>
- <!--#include virtual="/global/unix_ad1.shtml" -->
- </center>
- <!-- AD CODE STOPS -->
- <!--#include file="footer.html" -->
- </BODY>
- </HTML>
|