[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Real Editors

Contents Previous Topic Next Topic

If you intend to use Unix with any regular basis, you should seriously consider learning to use either gvim or emacs. As this author is familiar with gvim, I will attempt to give some of the basics, however, to be fair, I will provide some links for emacs:

emacs

Two emacs tutorials are provided by:

Alternatively, you can launch emacs and hit Ctrl-h and the t for tutorial. At this point, you can use the arrow keys.

vi, vim, or gvim

The original unix page editor was vi. With the modifications introduced by gvim, it is still one of the most powerful editor available. Throughout this document, we will simply refer to vi, and this will specify either vi, vim, or gvim.

The distinguishing characteristic of vi is that you are always in one of two modes: insert mode and command mode.

Insert Mode

When you are in insert mode, any (normal) key you type results in that character being placed a the current location of the cursor.

Command Mode

As you will quickly realize, most of the time editing a document is not spent entering characters; it is spent performing other operations, such as moving though the text, making changes, etc. Thus, we will begin with the most common operations: moving through the document.

The relationship between these two modes is shown in Figure 1.

Figure 1. Representation of the modes for vi.

Exiting

To exit, type :wq (write and quit). If this does not exit the document, this means you are in Insert Mode. Transfer to Command Mode by pressing Ctrl-[ and then type :wq.

If you don't want to save your changes your changes, type :q!, i.e., quit, really.

Simple Movement

At the Unix prompt, type:

{ecelinux:1} dos2unix Single_list.h Single_list.h
{ecelinux:2} gvim Single_list.h

(You *did* down-load this file from a previous tutorial, right?)

When vi came out, terminals had no arrow keys, however, this was not a problem with vi: the four keys to move around were h, j, k, and l. Don't memories these keys, simply take a look at Figure 2 and start to move around in SingleList.h. You will note that j is positioned immediately under the index finger, and moving down in a document will be one of the most common movements you will make.

Figure 2. Movement keys in vi.

Editing SingleList.h, move through the document. With a little practice, you will simply get used to pressing the key below your index finger to go down, etc.

Arrow Keys

While you are typing, you will be tempted to simply reach over and use the four arrow keys, ←, ↑ ↓ and →. Resist this temptation. If you are already this far, you will appreciate it very shortly.

Repetition

We will now look at the first pattern used in vi: the ability to make a fixed number of repetitions of a command by first typing a number.

In the middle of the document, in Command Mode, type 5j. You will note that you moved five lines down, as if you typed jjjjj. Similarly, you will be able jump, for example, 12 spaces to the right by typing 12l.

Transfers to Insert Mode

We will introduce the four most common means of transferring from Command Mode to Insert Mode. This will also introduce a second pattern.

In Figure 3, you will note that the cursor (while in Command Mode) is on the letter N. If you want to begin inserting characters after current character, press a for append, while to insert before the current character, press i for insert.

To insert at the start of the line (regardless of where the cursor is actually located on the line), press I while to append at the end of the line, press A.

All four of these commands are summarized in Figure 3 and to return to command mode, press Ctrl-[.

Figure 3. Insertions using I, i, a, and A.

Deleting Characters and Rows

To delete the character which is currently located under the cursor, press x. To delete, for example, 7 characters (starting at the current location), use 7x. To delete the previous character, use X.

To delete an entire line, press dd, while to delete, for example, 5 lines, use the Command 5dd.

Searching

The vi editor allows you to search your document for text strings. For example, if you were searching for the word int, you would, in Command Mode, type /int and hit Enter.

The cursor will move to the i of the next text matching int. Once you have performed a search, you can repeat the same search by pressing n (for next). Pressing N will find the next instance in the opposite direction.

Once again, the relationship between n and N is quite obvious: they do similar things, though the more common task is assigned to n while the less common variant is assigned to N.

To search for int in the backwards direction, type ?int and press enter (? is Shift-/).

Link with the Firefox Browser

Next time you're in Firefox, type /int and hit Enter.

Regular Expressions

For Computer Engineering students: you will be learning about Regular Expressions in ECE 251, so you might as well learn (and use to your advantage) regular expressions in ECE 250.

Moving to Line NN

If you want to move to line 43 in a file (for example, you compiled your Project, and gcc told you there was an error on line 43), in Command Mode, type :43 and press Enter.

Matching Parentheses, Brackets, and Braces

The second-most important command you will learn in this tutorial (after searching) is the ability to find matching parentheses. Suppose you have the following document, where you have very carefully indented the braces so that they line up:

int f( int N ) {
	int sum;

	for ( int i = 0; i < N; ++i ) {
		for ( int j = i; j < N; ++j ) {
			for ( int k = j; k < N; ++k ) {
				sum += i + j + k;
		}
	}

	return sum;
}

You may note in this simple example that one of the braces is missing, however, it may be difficult to determine which is the missing brace. (You may not even know that this function has the missing brace.) To find the matching brace of, for example, the brace in red, place the cursor on that character and press %. You will see that it jumps to the brace after the fourth line. Hitting % again jumps back to the last brace. At this point, you will be able able to note that the opening brace of this function does not have a matching closing brace.

Terminology for ()[]<>{}

For your information, () are called parentheses (one parenthesis), [] are called brackets, <> are called angled brackets, while {} are called braces. On occasion, it is useful to be redundant by saying curly braces, square brackets, and round parentheses. Angled brackets are always described using the adjective angled.

From Here On?

You now have enough tools to perform basic editing, and you can now begin editing Project 1. We could extend this tutorial to include more instructions, however, at this point, with all the information above, you probably overload your mind. it is more important that you actually try editing a document. Once you have worked on the project, you will notice that there are many annoying things you have to do, and this provide justification for all the myriad of vi commands.

If you're really keen, or you already know vi, you can down-load this one-page summary of vi commands.

Contents Previous Topic Next Topic

Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]