Search and replace with vi -- part 1

 Operating Systems

Global search and replace on any editor
should be called global search and destroy. Executing a badly formed
search and replace request will obliterate your text faster than
anything except rm *. Fortunately, under vi you may
undo the effect of the last change command, which includes the
effects of a search and replace. Undo action is executed by typing
u (for undo) in command mode. This is a crucial command
to know when you are editing, especially when you are doing global
search and destroy operations. It is also very handy when you are
practicing. You can try something out and then use u to
revert the file to its previous state.

The vi editor loads a text file into an editor buffer. When you
perform any edit including search and replace operations you are
actually editing the characters in the buffer not the actual file. I
have the words, line, text, and file throughout the article, but
keep in mind that you are actually editing text stored in a memory
buffer and not the file itself. The file isn't changed until the
editing that you have done is written back out to the file.

To get started with vi, it is helpful to think of vi's search and
replace command as a command in five separate parts:

  1. The command or symbol that signifies that the command to be executed is a search and replace command
  2. The range of lines in the file to be processed
  3. The text to be searched for
  4. The text to be used as a replacement
  5. Options for the search and or replace action

Each of these parts of the command has its own rules, and I will take them apart one by one.

First we will take a look at part one, the command to start a search
and replace. The vi editor is not itself an editor -- it is a
visual wrapper ("vi" for visual) around a single line editor called
"ex." No one uses single line editors anymore -- they are too
primitive and painful to use -- but they were the forerunners of the
full-screen and multiple screen editors on the market. The
vi interface creates a full-screen wrapper around the
ex single line editor. Many of vi's commands are
actually ex commands and are triggered by first entering
ex mode. Search and replace is one of these. To enter ex
mode, type the colon (:) character while in command mode. All of the
flavors and examples of search and replace in this article start
with the colon character. Although not covered in this article,
commands that start with the colon such as :q! to quit
are ex commands. The difference between ex
commands and the vi commands that were added into the
vi wrapper is the starting colon. You don't have to worry
about this, but historically it explains why so many vi
commands start that way.

Join us:






Spotlight on ...
Online Training

    Upgrade your skills and earn higher pay

    Readers to share their best tips for maximizing training dollars and getting the most out self-directed learning. Here’s what they said.


    Learn more

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

Ask a Question