topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Grasping more of the vi editor

on this topic
October 2, 2001, 04:22 PM —  ITworld.com — 

The response to part 1 of our introduction to
vi (the first Unix101 column) was terrific. I'm glad that so many of
you liked the article. There were a lot of great suggestions -- some of
which are included in this article, and some of which I will
save up for when we revisit vi in a future column.

This article covers manipulating vi's default behavior and
cutting and pasting within as well as across files.
Customizing vi's behavior and learning to manage its buffers
can greatly improve its performance.

Vi's search function is a prime example of this. Searching is done
from command mode by typing a slash followed by the search text as in:


/Encyclopedia

This will search for the string Encyclopedia. Once an "Encyclopedia" is
found, typing an "n" will search for the next "Encyclopedia." Having
grown accustomed to PC editors that tend to default to a
case-independent search, vi annoyed me with the fact that /Encyclopedia
would find "Encyclopedia" but not "encyclopedia" or "ENCYCLOPEDIA." Vi
has a setting that will cause the search to ignore case. This is an ex
mode command, so from command mode you type it as a colon followed by
ic as in:


:set ic      (and press RETURN)

Ic is shorthand for ignorecase. If you need to turn this off
for a case-dependent search, type:


:set noic    (and press RETURN)

Another useful set option is showmode. If you tend to type
and then break for something (research or a fresh coffee), you might
walk away from the computer leaving vi in insert or edit mode.


:set showmode

will place a message in the last line of the screen on the
right indicating that you are in INSERT MODE, APPEND MODE, or
whatever.

This can be turned off with


:set noshowmode

To turn line numbering on and off use:


:set number

and


:set nonumber

Numbers are displayed at the beginning of
each line. These numbers do not appear in the document, simply on the
screen. They can be helpful if you are trying to compose something that
must fit in 55 lines (for a printed page).

Wrapping text

Setting the wrap margin will cause the text to auto wrap while you
are typing, but the command is quirky in a vi-ish sort of way. The
command is


:set wrapmargin=nn (where nn is a number)

The odd thing about this command is that it sets the amount of space to
the right that will not contain text. It would seem natural to set
wrapmargin=60 and assume that you would have 60 characters worth of
space to type in, but wrapmargin=60 sets a margin of 60, and you only
have 20 characters in which to write.

To set margins determine the width of text you want on the
screen and use the formula


:set wrapmargin=(80 - text width)

You can see all of the :set options that are available by
typing


:set all

A list of set options will be displayed that allow you to set auto
indenting, auto saving, word wrapping margins, how many lines to scroll
for CTRL-D and CTRL-U commands, and to set up the editor to use
defaults for certain programming languages.

You

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources