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

Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 4.5 Other Advanced Edits 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 , for they can save you a lot of editing time. Most of these commands can be used without ever leaving vi .

You already know how to think of files as a sequence of numbered lines. ex simply 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. 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 rarely 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.

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:


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 commas, with or without spaces in between). For example:


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:


With a screen editor you can 

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:


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:


Press [RETURN] .

Following the exercise, we will be discussing 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 practice file

ex practice

A message appears

"practice" 6 lines 320 characters

Go to and print (display) first line


Print (display) lines 1 through 3


Substitute screen for line on line 1


Invoke vi editor on file


Go to first line


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.5 Other Advanced Edits Learning the vi Editor Next: 5.2 Editing with ex
4.5 Other Advanced Edits Book Index 5.2 Editing with ex

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