USING TEXT EDITORS ON BARNEY (PICO, VI, EMACS)

 

Text editors are programs used to edit text files. Text files are limited to letters and numbers only; they do not include graphics, fonts, or special formatting information found in documents created by word processors. Text files include mail messages, source code, batch files, and configuration files. Text files are the most common type of file you will use on Barney. For this reason, there are several options available to you when you are working with text files.

TEXT EDITORS AVAILABLE ON BARNEY

There are three text editors that are available on Barney. These are pico, vi, and emacs. Each can be run by typing their name at the "[username]$" prompt. For extensive help on each of these editors, type "man editor" at the "[username]$" prompt. In addition, both pico and emacs have help systems available while they are running.

Each of these text editors is significantly different from the other. Pico is a simple editor with basic functions such as cut and paste, searching, and spell checking. Pico was written by the same group that wrote the e-mail software, pine, so there are a great deal of similarities between the editor in pine and pico. Within pico, commands are listed at the bottom of the screen. Because of its ease of use and similarity to pine, pico is the default editor on Barney. Programs such as tin (Threaded Internet News), use pico whenever an editor is needed. Pico is recommended for those users that need some simple text editing without the complexity of vi or emacs.

Vi is a more sophisticated and complicated editor than pico. It has more powerful searching capabilities, can replace blocks of text, jump to line numbers, and a host of other features. Its power comes primarily from its ability to move to precise locations within a file using very few keystrokes. This makes it an excellent editor for configuration files and source code. In addition, vi comes preinstalled on most UNIX systems. This makes moving from one UNIX system to another easier.

Emacs is the most powerful of the three editors. It has customized environments for writing source code in various languages, allows split screens, custom commands, and more. For those users that are going to do a lot of programming or working with large text documents in UNIX, emacs can save a lot of time once the initial learning curve is completed. Its sophistication is matched by its complexity. Those interested in learning emacs may find the book Learning GNU Emacs from O'Reilly and Associates beneficial.

The remainder of this document will focus on the vi editor. Users interested in pico or emacs should refer to their respective man pages, their online help, or other resources.

RUNNING VI

Vi is a screen-oriented editor. That means that the entire screen is used while editing a file. In order for vi to work correctly, you must be using terminal emulation that supports moving around the screen. VT100 and VT220 are the most common emulations that support screen editing.

Vi is run by either typing "vi" or "vi filename" at the "[username]$" prompt. If no filename is specified, vi opens a new document. The "~" characters in an empty are used to indicate empty lines in the file.

COMMAND AND INPUT MODES

Vi operates in two different modes -- command and input. The input mode is used while inserting text into the file. Some commands are available in input mode, but most are only available in command mode. Command mode is used for moving around the file, deleting or inserting blocks of text, and saving the file. In addition, command mode can be used to execute shell commands such as ls and more.

The ESC (escape) character is used to enter command mode from input mode. ESC can also be used within command mode to cancel a command that has been partially entered. Input mode is entered by (i)nserting or (a)ppending text.

When entering a command in command mode, entering a number before the command will cause the command to repeat that number of times. For example, entering "^B" scrolls the file back one screen and entering "5" then "^B" would scroll the screen back five screens. Note: the "^" indicates that the CTRL (control) is held down while pressing the letter.

MOVING AROUND WITHIN A FILE

As stated previously, one of the greatest strengths of vi is its ability to move around within a text file using a minimum of keystrokes. You may move by character, word, sentence, paragraph, and page. Following are some commands that can be used to move around within a file. These commands only apply while in command mode.

Scrolling

Line/Character Movement

^B

Scroll backward one screen

h

Move backward one character

^F

Scroll forward one screen

j

Move down one line in same column

^U

Scroll backward a half screen

k

Move up one line in the same column

^D

Scroll forward a half screen

spc

Move one right one character

^Y

Scroll backward one line

-

Move to first real character in previous line

^E

Scroll forward one line

^

Move to first real character in current line

+

Move to first real character in the next line

Context Movement

$

Move to the end of the current line

W

Move forward to beginning of a word

0

Move to the first character of the current line

B

Move backward to beginning of a word

G

Goto line number (if # entered) or end of file

(

Move to the beginning of a sentence*

)

Move to the end of a sentence*

Movement by Search

{

Move to beginning of prev paragraph

/

Search forward for text following '/'

}

Move to beginning of next paragraph

?

Search backward for text following '?'

*A sentence is defined as a period, !, or ? followed by two spaces or a new line.

Remember, you can cause the above commands to repeat multiple times simply by entering a number before you type the command. This is especially useful with the "G" command, as it will allow you to jump to a specific line anywhere in the file (e.g., "100G" jumps to line 100).

ENTERING AND EDITING TEXT

Vi offers several ways to insert and change text. You can insert text beginning at the current character, after the current character, at beginning or end of the current line, or insert a new line above or below the current line. You can also replace existing characters, blocks of characters, and lines. Commands are entered in command mode unless stated otherwise. Remember, many of these commands can be preceded with a number to indicate the number of times (or count) the command should be performed.

Inserting Text*

Changing Text

i

Insert before current character

r char

Replace current character with character

a

Insert after current character

R text ESC

Replace current character(s) with text

I

Insert at beginning of current line

s text ESC

Substitute current character with text

A

Append to end of current line

S text ESC

Substitute entire line with text

O

Insert new line above current line

cw text ESC

Change current word to text

o

Insert new line below current

C text ESC

Change rest of current line to text

ESC

Return to command mode

J

Join next line(s) to end of current line

Deleting Text

Copying and Placing Text

x

Delete current character(s)

nyy

Yank (cut) n lines and place in buffer

X

Delete previous character(s)

p

Put yanked text after cursor

dw

Delete current word(s)

P

Put yanked text before cursor

db

Delete previous word(s)

dd

Delete line(s)

Undo

:n,md

Delete lines n through m

u

Undo last command

D

Delete from cursor to end of line

U

Restore current line to original state

*Entering a command to insert or append text will leave you in insert mode until you press the ESC key.

You can also search and replace text strings using a command in the form, ":n,ms/str1/str2/opt." This command will search from lines n to m for the string str1. If it finds str1, it will replace it with str2 depending upon the option used. Options include 'g' for global, 'c' for confirm, and 'p' for print changes. For example, if I wanted to search lines 100 to 200 for the string "tony" and replace it with "toni" with a confirmation of the change for each instance, I would use the command ":100,200s/tony/toni/c."

SAVING AND QUITTING VI

When quitting vi, there are several options for saving or throwing away changes to the document.

:wq

Write to current file and quit

:q

Quit (must save first)

:w

Write to current file without quitting

:q!

Quit without saving changes

:w file

Write to new file without quitting

:e!

Re-edit current file, discarding unsaved changes

:w!

Write to file, ignoring read-only status

WARNING: UNIX does not maintain multiple versions of a file like VAX systems do. Unless you specifically write to a different file, you are modifying the original file. Once the changes have been written to the file, there is no way to recover the previous contents of the file.

 

VI COMMAND QUICK REFERENCE

Running vi

Getting More Help

vi Runs vi and starts a new file

man vi Displays the man page for vi editor

vi file Runs vi and opens file

man ex Displays the man page for ex (line oriented editor)

Scrolling

Line/Character Movement

^B Scroll backward one screen

h Move backward one character

^F Scroll forward one screen

j Move down one line in same column

^U Scroll backward a half screen

k Move up one line in the same column

^D Scroll forward a half screen

spc Move one right one character

^Y Scroll backward one line

- Move to first real character in previous line

^E Scroll forward one line

^ Move to first real character in current line

+ Move to first real character in the next line

Context Movement

$ Move to the end of the current line

W Move forward to beginning of a word

0 Move to the first character of the current line

B Move backward to beginning of a word

G Goto line number (if # entered) or end of file

( Move to the beginning of a sentence

) Move to the end of a sentence

Movement by Search

{ Move to beginning of prev paragraph

/ Search forward for text following '/'

} Move to beginning of next paragraph

? Search backward for text following '?'

Inserting Text*

Changing Text

i Insert before current character

r char Replace current character with character

a Insert after current character

R text ESC Replace current character(s) with text

I Insert at beginning of current line

s text ESC Substitute current character with text

A Append to end of current line

S text ESC Substitute entire line with text

O Insert new line above current line

cw text ESC Change current word to text

o Insert new line below current

C text ESC Change rest of current line to text

ESC Return to command mode

J Join next line(s) to end of current line

Deleting Text

Copying and Placing Text

x Delete current character(s)

nyy Yank (cut) n lines and place in buffer

X Delete previous character(s)

p Put yanked text after cursor

dw Delete current word(s)

P Put yanked text before cursor

db Delete previous word(s)

dd Delete line(s)

Undo

:n,md Delete lines n through m

u Undo last command

D Delete from cursor to end of line

U Restore current line to original state

Writing Files

Quitting and Resetting vi

:wq Write to current file and quit

:q Quit (must save first)

:w Write to current file without quitting

:q! Quit without saving changes

:w file Write to new file without quitting

:e! Re-edit current file, discarding unsaved changes

:w! Write to file, ignoring read-only status

*Inserting commands will remain in insert mode until the ESC key is pressed.