home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 4.6 Review of vi Buffer and Marking Commands Chapter 5 Next: 5.2 Editing with ex
 

5. Introducing the ex Editor

If this is a handbook on vi , why would we include a chapter on another editor? ex is not really another editor. vi is the visual mode of the more general, underlying line editor, ex . Some ex commands can be useful to you while you are working in vi , since they can save you a lot of editing time. Most of these commands can be used without ever leaving vi .[1 ]

[1] vile is different from the other clones. Many of the more advanced ex commands simply don't work. Instead of noting each one, more details are provided in Chapter 12, vile -- vi Like Emacs .

You already know how to think of files as a sequence of numbered lines. ex gives you editing commands with greater mobility and scope. With ex you can move easily between files and transfer text from one file to another in a variety of ways. You can quickly edit blocks of text larger than a single screen. And with global replacement you can make substitutions throughout a file for a given pattern.

This chapter introduces ex and its commands. You will learn how to:

  • Move around a file by using line numbers

  • Use ex commands to copy, move, and delete blocks of text

  • Save files and parts of files

  • Work with multiple files (reading in text or commands, traveling between files)

5.1 ex Commands

Long before vi or any other screen editor was invented, people communicated with computers on printing terminals, rather than on today's CRTs (or bitmapped screens with pointing devices and terminal emulation programs). Line numbers were a way to quickly identify a part of a file to be worked on, and line editors evolved to edit those files. A programmer or other computer user would typically print out a line (or lines) on the printing terminal, give the editing commands to change just that line, then reprint to check the edited line.

People don't edit files on printing terminals any more, but some ex line editor commands are still useful to users of the more sophisticated visual editor built on top of ex . Although it is simpler to make most edits with vi , the line orientation of ex gives it an advantage when you want to make large-scale changes to more than one part of a file.

NOTE: Many of the commands we'll see in this chapter have filename arguments. Although it's possible, it is usually a very bad idea to have spaces in your files' names. ex will be confused to no end, and you will go to more trouble than it's worth trying to get the filenames to be accepted. Use underscores, dashes, or periods to separate the components of your file names, and you'll be much happier.

Before you start off simply memorizing ex commands (or worse, ignoring them), let's first take some of the mystery out of line editors. Seeing how ex works when it is invoked directly will help make sense of the sometimes obscure command syntax.

Open a file that is familiar to you and try a few ex commands. Just as you can invoke the vi editor on a file, you can invoke the ex line editor on a file. If you invoke ex , you will see a message about the total number of lines in the file, and a colon command prompt.

For example:

$ ex practice


"practice" 6 lines, 320 characters
:

You won't see any lines in the file unless you give an ex command that causes one or more lines to be displayed.

ex commands consist of a line address (which can simply be a line number) plus a command; they are finished with a carriage return. One of the most basic commands is p for print (to the screen). So, for example, if you type 1p at the prompt, you will see the first line of the file:

  :1p


  With a screen editor you can
  :

In fact, you can leave off the p , because a line number by itself is equivalent to a print command for that line. To print more than one line, you can specify a range of line numbers (for example, 1,3  -- two numbers separated by a comma, with or without spaces in between). For example:

  :1,3


  With a screen editor you can
  scroll the page, move the cursor,
  delete lines, insert characters, and more,

A command without a line number is assumed to affect the current line. So, for example, the substitute command (s ), which allows you to substitute one word for another, could be entered like this:

  :1


  With a screen editor you can
  :s/screen/line/


  With a line editor you can

Notice that the changed line is reprinted after the command is issued. You could also make the same change like this:

  :1s/screen/line/


  With a line editor you can

Even though you will be invoking ex commands from vi and will not be using them directly, it is worthwhile to spend a few minutes in ex itself. You will get a feel for how you need to tell the editor which line (or lines) to work on, as well as which command to execute.

After you have given a few ex commands on your practice file, you should invoke vi on that same file, so that you can see it in the more familiar visual mode. The command :vi will get you from ex to vi .

To invoke an ex command from vi , you must type the special bottom line character : (colon). Then type the command and press [RETURN] to execute it. So, for example, in the ex editor you move to a line simply by typing the number of the line at the colon prompt. To move to line 6 of a file using this command from within vi , enter:

:6

Press [RETURN] .

Following the exercise, we will discuss ex commands only as they are executed from vi .

5.1.1 Exercise: The ex Editor

At the UNIX prompt, invoke ex editor on a file called practice :

ex practice

A message appears:

"practice" 6 lines, 320 characters

Go to and print (display) first line:

:1

Print (display) lines 1 through 3:

:1,3

Substitute screen for line on line 1:

:1s/screen/line

Invoke vi editor on file:

:vi

Go to first line:

:1

5.1.2 Problem Checklist

  • While editing in vi, you accidentally end up in the ex editor.

    A Q in the command mode of vi invokes ex . Any time you are in ex , the command vi returns you to the vi editor.


Previous: 4.6 Review of vi Buffer and Marking Commands Learning the vi Editor Next: 5.2 Editing with ex
4.6 Review of vi Buffer and Marking Commands Book Index 5.2 Editing with ex

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System