Editing Text Files


Text editors are application programs which allow users to create new text files and/or modify the contents of existing text files. Many of us have used simple text editors, perhaps a popular one such as Microsoft's Notepad text editor.

Unix has many text editing applications available for use. Two of the more popular of these are vi (whose name comes from VIsual editor) and emacs (whose name is derived from Editor MACroS). The vi editor is typically a non-GUI based tool, whereas emacs is available in both GUI and non-GUI modes (however, most users prefer the GUI mode of emacs). Both of these text editing applications are included in most, if not all Unix installations. Since this e-text is focused upon command line (i.e. text) based applications, the discussion of text based editor programs will focus upon the vi editor. If additional information regarding the emacs editor is desired, it can be found at GNUs site here.

The vi editor is a text based editor with numerous, powerful features. This power sometimes overwhelms less experienced users and results in users shying away from vi. The key to using vi is to understand what it is you wish to do and how to accomplish doing it. While some of the commands may seem awkward at first, the key to becoming proficient with this or any other application is practice.

Commands to accomplish several necessary and useful tasks are listed below. Note these commands are a small subset of all vi commands, although these are most of the commands the author regularly uses.

Opening a File

To open a new or existing file using vi, you simply enter the command:

	$ vi file_spec [Enter]
where file_spec can be any existing text file specfication (for which you have read access) or the specification of a new file. Once in the vi editor, you will see a tilde character (~) on each line below the existing text. Some vi editor versions will also display the name of the file, the number of lines, characters, etc. for added user friendliness. Note you can also simply type vi [Enter] without a file_spec, which will result in your editing of an unnamed document.

Exiting vi

There are several ways to exit the vi editor, based upon your wish to save any changes or not. To exit vi and save (write) your changes, you enter the command:
	:wq      (or ZZ)
This command will write your changes and quit the editing session. If you do not wish to save any changes (since your last write), you enter:
	:q
if you have made no changes since the last write or
	:q!
if you have made changes since your last write. Note that if you try to quit and get a message like
	No write since last change (use ! to override)
this indicates that you have made changes since last write and must quit with the :q! command.

Modes in vi

The vi editor has 2 modes, command mode and insert mode. When the editor starts initially, it starts in command mode. In command mode, everything the user types is interpreted as a command. Some commands are single character commands, some are multiple character commands, and some commands require a carriage return for activation. Insert mode is entered with either the i, a, or o commands (see below). In this mode, every character the user types appears within the text document. Once in insert mode, you can return to command mode by using the escape [Esc] key. Note the [Esc] key is not a toggle, it only switches from insert mode to command mode.

Inserting Text

Inserting text (and entering insert mode) can be accomplished a couple of ways, based upon where you wish the inserted text to appear. Specifically,
	i - inserts text to left of cursor
   	a - append, inserts to right of cursor
   	o - open a new line, inserts on the line below the cursor (at the beginning of the line)
Recall, if you wish to leave insert mode and return to command mode, this is accomplished by pressing the [Esc] key.

Cursor Movement

Moving the cursor is done using various alpha-numeric keystrokes. Specifically,
	h - move cursor left 
	j - move cursor down
	k - move cursor up
	l - (ell) move cursor right (or you can use the spacebar)
	0 - (zero) move cursor to beginning of line
	$ - move cursor to end of line
	nG - move to line number n (e.g. 3G moves to line 3)
	G - move to bottom of document
Note that the arrow keys may work on some terminals, but this is not guaranteed and it is suggested that the h, j, k, and l keys be used. Some question the use of the h, j, k, and l keys for movement. This is not as un-natural as it may seem since these keys are near the normal typing position of your right hand (i.e. r-hand home row position). With practice, you will find this very natural and movement very easy.

Deleting Text

As with inserting text, deleting text can be done many ways depending where and how much text you wish to delete. Specifically,
	x - delete a single character 
	dw - delete a single word 
	dd - delete a single line 
	nx, ndw, ndd deletes n chars, words or lines respectively 
Keep in mind that if text is deleted by mistake, the delete can be undone and redone correctly (see below).

Moving Text

Moving text can be done either with cutting the text or copying the text, moving to the desired position in the file and putting the cut or copied text. Specifically,
	ndd - cut (delete) n lines and place in buffer
	nyy - copy (yank) n lines and place in buffer
	p - paste buffer where cursor is positioned

Miscellaneous

Several miscellaneous commands are available that make ones' life with vi easier.

To search for a text string within a file, you use the command /string followed by [Enter], where string is the string you wish to search for. You can also find subsequent occurrences of string with the command /[Enter].

To repeat a command in vi, simply use the . (dot) command. For instance, if you wish to delete 5 lines, you enter 5dd. If you then wish to delete 5 more, you can simply use the . command.

To undo the action of a previous command, simply use the u command. For instance, if you have deleted 5 lines by mistake, simply press the u key and the delete will be undone. In some vi implementations, you can even undo multiple actions.

vi In Summary

Opening a file $ vi file_spec [Enter]    (for new or existing files)
Exiting a file with save :wq (or ZZ)
Exiting a file without save :q (if no changes were made)
:q! (if the file was changed)
Two basic modes command mode (entered with Esc key)
insert mode
Entering insert mode i (to insert to left of cursor)
a (append, insert to right of cursor)
o (open a new line)
Cursor movement h - move cursor left
j - move cursor down
k - move cursor up
l - (ell) move cursor right (or use spacebar)
0 - (zero) move cursor to beginning of line
$ - move cursor to end of line
nG - move to line number n
G - move to bottom of document
Deleting text x - delete a single character
dw - delete a single word
dd - delete a single line
nx, ndw, ndd deletes n chars, words or lines respectively
Cut/copy & paste ndd - cut n lines and place in buffer
nyy - copy n lines and place in buffer
p - paste buffer where cursor is positioned
Miscellaneous /string [Enter] - searches and moves to first occurrence of string
/[Enter] - searches and moves to next occurrence of string
. - redo last command
u - undo last command
r - replace (overwrite) a single character



©2015, Mark A. Thomas. All Rights Reserved.