UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

8.4 Configuring vi Parameters

8.4.1 Description

8.4.1 Description

The vi editor has 60 reserved settings that you can configure. Although you probably will not use all of them, there are some that aid you in whatever task you are trying to achieve.

Example One: Setting Parameters Within vi

The following is a list of the available reserved settings that you can manipulate and that enable you to function more easily within vi. To view the configurations of the parameters in your current vi session, go into the command mode and issue the command set all .

:set all

Or

:setall

Don't forget to begin the command with a colon ( :) to execute the command set all . A list of current settings for all the available parameters is displayed:

noautoindent     flipcase=""      nonumber                nosync
autoprint        nohideformat     nooptimize              tabstop=8
autotab          noignorecase     paragraphs="PPppIPLPQP" taglength=0
noautowrite      noinputmode      prompt                  tags="tags"
nobeautify       keytime=2        noreadonly              tagstack
cc="cc -c"       keywordprg="ref" remap                   term="vt100"
nocharattr       lines=24         report=5                noterse
columns=80       nolist           noruler                 timeout
nodigraph        magic            nosafer                 warn
directory="/tmp" make="make"      scroll=11               window=0
noedcompatible   mesg             sections="NHSHSSSEse"   wrapmargin=0
equalprg="fmt"   nomodelines      shell="/bin/csh"        wrapscan
errorbells       more             noshowmatch             nowriteany
noexrc           nearscroll=24    noshowmode
exrefresh        newfile          shiftwidth=8
noflash          nonovice         sidescroll=8

Even though many parameters can be set within vi, you will use only a small number of them. Here are some of the more important parameters you should be aware of and possibly modify for your particular needs. To change any of the settings, again use the set command while in the command mode.

Syntax:

:set option
:set nooption
:set option=nn

There are six common parameters that require more modifications than any of the others. There are a couple places where you can hardcode the modifications so that the parameters will be set to your preference each time you go into vi.

Example Two: EXINIT Variable

Any changes you want to make to the vi parameters can be hardcoded into the variable EXINIT from inside the startup file for the shell that you are logging in to the system with. Declare the EXINIT variable by passing the settings of each parameter to the variable. Separate each command with a space or a vertical bar ( |), depending on the versions of vi you are using. If one doesn't work try the other.

Place the necessary parameters into the .profile in your home directory if you are using the Bourne or Korn shells:

EXINIT='autoindent wrapmargin=5 showmode'
export EXINIT

If you use the csh or tcsh shells then make the additions to your .cshrc or .login file.

setenv EXINIT 'set autoindent|wrapmargin=5|showmode'

You have to reference your startup files again for the EXINIT variable to take effect. When this is finished, verify that the variable was set correctly:

% set
TERM=vt100
SHELL=/bin/csh
MAIL=/var/mail/ugu
PWD=/local/home/user/ugu
EXINIT=set autoindent list wrapmargin=40

When the variable is confirmed and valid, go into the vi editor and execute :set from the command mode and you should see the changes you placed in your startup file.

:set
autoindent wrapmargin=40 showmode

Example Three: .exrc File

The vi editor also supports a configuration file called .exrc that is read when vi is executed. When vi is started, it looks for the .exrc file in the current directory from which vi was started. If there is no .exrc file in this directory, it checks for the existence of the .exrc file in the home directory of the account that is logged in.

Use this file to set any parameters that you want vi to have when it starts up. The .exrc file accepts multiple parameter settings on a single line or one parameter per line.

vi .exrc

" Indent lines and show modes
set autoindent showmode
"
" Wrap the text 10 characters from the end
set wrapmargin=10
"
" Number each of the line
set  number

Note - The double quote ( ") symbol is the only character that can be used for commenting lines. As you might have guessed by now, there is no standard to commenting lines. Configuration files use pound signs ( #), semicolons ( ;), or colons ( :); the .exrc is no different.


When the entries are made in the .exrc file, start vi back up on the file you want to edit. A :set command shows that the settings configured in .exrc were accepted.

% vi /tmp/foobar
:set
autoindent    showmode      wrapmargin=10
number

Reason

Everyone has the ability to configure settings within vi to provide the ease-of-use that people look for in applications, and, of course, you are always looking for ways to make your routine operations easier.

Real World Experience

The vi editor is a versatile editor from any point of view. When you use it to create a program and write source code, the editor can automatically indent and number each line and, in general, make source code creation much easier than it is with a plain text editor. Even if you decide to use it only for creating text files, documentation, or Web pages, vi can be configured to wrap text around to a new line and display its different modes when you toggle between the text. These features help speed up editing and modification times.

Other Resources

Man pages:

set, vi

World Wide Web:

Sven's VI Setup File-- http://www.math.fu-berlin.de/~guckes/vi/exrc

Vi settings options-- http://www.linuxbox.com/~taylor/4ltrwrd/VRoptions.html

UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

© Copyright Macmillan USA. All rights reserved.