Chapter 15. Editing and
Viewing Files and Strings
The vi editor is a full-screen file editor that was
developed from the ex editor. It is also common to all versions of UNIX. The
vedit command starts a version of the vi editor intended for
beginners. In the vedit editor, the report option is set to 1, the showmode
option is set, and the novice option is set, making it a line editor.
The following list provides the maximum limits of the
vi editor. These counts assume single-byte characters.
- {LINE_MAX} characters per line
- 256 characters per global command list
- 128 characters in the previously inserted and deleted text
- 128 characters in a shell escape command
- 128 characters in a string-valued option
- 30 characters in a tag name
- 128 map macros with 2048 characters total
- 1,048,560 lines of {LINE_MAX} characters per line silently enforced
|
---|
Running the vi editor on a file larger than 64 MB may cause the following
error message to display:
0602-103 file too large to place in /tmp
|
The vi editor operates in the following modes:
-
command mode
- When you start the vi editor, it is in command mode.
You can enter any subcommand except those designated for use only in the text
input mode. The vi editor returns to command mode when subcommands and other
modes end. Press the Esc key to cancel a subcommand.
-
text-input mode
- You use the vi editor in this mode to add text. Enter
text input mode with any of the following subcommands: a, A,
i, I, o, O, cx (where the
x represents the scope of the subcommand), C, s,
S, and R . After entering one of these subcommands, you can
enter text into the editing buffer. To return to command mode, press the
Esc key for normal exit or press Interrupt (the Ctrl-C key sequence) to
end abnormally.
-
last-line mode
- Subcommands with the prefix : (colon),
/ (slash), ? (question mark), ! (exclamation point),
or !! (two exclamation points) read input on a line displayed at the
bottom of the screen. When you enter the initial character, the vi editor
places the cursor at the bottom of the screen, where you enter the remaining
characters of the command. Press the Enter key to run the subcommand, or
press Interrupt (the Ctrl-C key sequence) to cancel it. When the !!
prefix is used, the cursor moves only after both exclamation points are
entered. When you use the : prefix to enter the last-line mode, the vi
editor gives special meaning to the following characters when they are used
before commands that specify counts:
-
%
- All lines regardless of cursor position
-
$
- Last line
-
.
- Current line
Table 23 shows the flags that can
be used with the vi command.

Table 23: The vi Editor Flags
The list of subcommands is very extensive, so you are
only going to learn some of the more common subcommands used. You would use the
subcommands to perform these kinds of actions:
- Moving the cursor
- Editing text
- Manipulating files
- Other actions
Enter the following subcommands in command mode. You can cancel an
incomplete command by pressing the Esc key.
Use subcommands to move the cursor within a file in these ways:
- Moving within a line
-
Left Arrow or h or Ctrl-H
- Moves the cursor one character to the left.
-
Down Arrow or j or Ctrl-J or Ctrl-N
- Moves the cursor down one line (it remains in the same column).
-
Up Arrow or k or Ctrl-P
- Moves the cursor up one line (it remains in the same column).
-
Right Arrow or l
- Moves the cursor one character to the right.
- Moving within a line by character position
-
^
- Moves the cursor to the first nonblank character.
-
0
- Moves the cursor to the beginning of the line.
-
$
- Moves the cursor to the end of the line.
-
fx
- Moves the cursor to the next x character.
-
Fx
- Moves the cursor to the last x character.
-
tx
- Moves the cursor to one column before the next x character.
-
Tx
- Moves the cursor to one column after the last x character.
-
;
- Repeats the last f, F, t, or T subcommand.
-
,
- Repeats the last f, F, t, or T subcommand in the opposite direction.
- Moving to words
-
w
- Moves the cursor to the next small word.
-
b
- Moves the cursor to the previous small word.
-
e
- Moves the cursor to the next end of a small word.
-
W
- Moves the cursor to the next big word.
-
B
- Moves the cursor to the previous big word.
-
E
- Moves the cursor to the next end of a big word.
- Moving by line position
-
H
- Moves the cursor to the top line on the screen.
-
L
- Moves the cursor to the last line on the screen.
-
M
- Moves the cursor to the middle line on the screen.
-
+
- Moves the cursor to the next line at its first nonblank character.
-
-
- Moves the cursor to the previous line at its first nonblank character.
-
Enter
- Moves the cursor to the next line at its first nonblank character.
- Moving to sentences, paragraphs, or sections
-
(
- Places the cursor at the beginning of the previous sentence.
-
)
- Places the cursor at the beginning of the next sentence.
-
{
- Places the cursor at the beginning of the previous paragraph.
-
}
- Places the cursor at the beginning of the next paragraph, or at the next
section if you are in C mode.
-
]]
- Places the cursor at the next section.
-
[[
- Places the cursor at the previous section.
The subcommands for editing enable you to perform the following tasks:
- Marking a specific location in a file and returning
-
"
- Moves the cursor to the previous location of the current line.
-
"
- Moves the cursor to the beginning of the line containing the previous
location of the current line.
-
mx
- Marks the current position with the letter specified by the x parameter.
-
`x
- Moves the cursor to the mark specified by the x parameter.
-
'x
- Moves the cursor to the beginning of the line containing the mark specified
by the x parameter.
- Adding text to a file
-
aText
- Inserts text specified by the Text parameter after the cursor. End
text input mode by pressing the Esc key.
-
AText
- Adds text specified by the Text parameter to the end of the line.
End text input mode by pressing the Esc key.
-
iText
- Inserts text specified by the Text parameter before the cursor. End
text input mode by pressing the Esc key.
-
IText
- Inserts text specified by the Text parameter before the first
non-blank character in the line. End text input mode by pressing the Esc
key.
-
o
- Adds an empty line below the current line. End text input mode by pressing
the Esc key.
-
O
- Adds an empty line above the current line. End text input mode by pressing
the Esc key.
- Changing text while in input mode
-
Ctrl-D
- Goes back to previous autoindent stop.
-
^ Ctrl-D
- Ends autoindent for this line only.
-
0Ctrl-D
- Moves cursor back to left margin.
-
Esc
- Ends insertion and returns to command state.
-
Ctrl-H
- Erases the last character.
-
Ctrl-Q
- Enters any character if xon is disabled.
-
Ctrl-V
- Enters any character.
-
Ctrl-W
- Erases the last small word.
-
\
- Quotes the erase and kill characters.
-
Ctrl-?
- Interrupts and ends insert or the Ctrl-D key sequence.
- Changing text from command mode
-
C
- Changes the rest of the line (same as c$).
-
cc
- Changes a line.
-
cw
- Changes a word.
-
cwText
- Changes a word to the text specified by the Text parameter.
-
D
- Deletes the rest of the line (same as d$).
-
dd
- Deletes a line.
-
dw
- Deletes a word.
-
J
- Joins lines.
-
rx
- Replaces the current character with the character specified by x.
-
RText
- Overwrites characters with the text specified by the Text parameter.
-
s
- Substitutes characters (same as cl).
-
S
- Substitutes lines (same as cc).
-
u
- Undoes the previous change.
-
x
- Deletes a character at the cursor.
-
X
- Deletes a character before the cursor (same as dh).
-
<<
- Shifts one line to the left.
-
<L
- Shifts all lines from the cursor to the end of the screen to the left.
-
>>
- Shifts one line to the right.
-
>L
- Shifts all lines from the cursor to the end of the screen to the right.
-
~
- Changes letter at the cursor to the opposite case.
- Copying and moving text
-
p
- Puts back text from the undo buffer after the cursor.
-
P
- Puts back text from the undo buffer before the cursor.
-
"xp
- Puts back text from the x buffer.
-
"xd
- Deletes text into the x buffer.
-
y
- Places the object that follows (for example, w for word) into the undo
buffer.
-
"xy
- Places the object that follows into the x buffer, where x is any letter.
-
Y
- Places the line in the undo buffer.
- Restoring and repeating changes
-
u
- Undoes the last change.
Note |
---|
-
After an undo, the cursor moves to the first non-blank character on the updated
current line.
|
-
U
- Restores the current line if the cursor has not left the line since the
last change.
-
.
- Repeats the last change or increments the "np command.
|
---|
-
1.This subcommand will repeat the last change, including an undo. Therefore,
after an undo, repeat performs an undo rather than repeat the last change.
-
2.This subcommand is not meant for use with a macro. Enter @@ (two at
signs) to repeat a macro.
|
-
"np
- Retrieves the nth last delete of a complete line or block of lines.
The subcommands for manipulating files allow you to do the tasks outlined in
the following sections:
- Saving changes to a file
-
:w
- Writes the edit buffer contents to the original file.
-
:w File
- Writes the edit buffer contents to the file specified by the File
parameter.
-
:w! File
- Overwrites the file specified by the File parameter with the edit buffer
contents.
- Editing a second file
-
:e File
- Edits the specified file.
-
:e!
- Re-edits the current file and discards all changes.
-
:e + File
- Edits the specified file starting at the end.
-
:e + Number File
- Edits the specified file starting at the specified line number.
-
:e #
- Edits the alternate file. The alternate file is usually the previous file
name before accessing another file with a :e command. However, if
changes are pending on the current file when a new file is called, the new file
becomes the alternate file. This subcommand is the same as the Ctrl-A
subcommand.
-
:r File
- Reads the file into the editing buffer by adding new lines below the
current line.
-
:r !Command
- Runs the specified AIX command and places its output into the file by
adding new lines below the current cursor position.
-
:ta Tag
- Edits a file containing the Tag tag starting at the location of the tag. To
use this subcommand, you must first create a database of function names and
their locations using the ctags command.
-
Ctrl-A
- Edits the alternate file. The alternate file is usually the previous
current file name. However, if changes are pending on the current file when a
new file is called, the new file becomes the alternate file. This subcommand is
the same as the :e # subcommand.
- Editing a list of files
-
:n
- Edits the next file in the list entered on the command line.
-
:n Files
- Specifies a new list of files to edit.
- Finding file information
-
Ctrl-G
- Shows the current file name, current line number, number of lines in the
file, and percentage of the way through the file where the cursor is located.
The vi editor provides the subcommands described in the following sections:
- Adjusting the screen
-
Ctrl-L
- Clears and redraws the screen.
-
Ctrl-R
- Redraws the screen and eliminates blank lines marked with @ (at sign).
-
zNumber
- Makes the window the specified number of lines long.
- Entering shell commands
-
:sh
- Enters the shell to allow you to run more than one AIX command. You can
return to the vi editor by pressing the Ctrl-D key sequence.
-
:!Command
- Runs the specified AIX command and then returns to the vi editor.
Note |
---|
The # (alternate file), % (current file), and !
(previous command) special characters are expanded when following a :!
subcommand. To prevent any of these characters from being expanded, use the
\ (backslash).
|
-
:!!
- Repeats the last :!Command subcommand.
-
Number!!Command
- Runs the specified AIX command and replaces the lines specified by Number
with the output of the command. If a number is not specified, the default value
is 1. If the command expects standard input, the specified lines are used as
input.
-
!Object Command
- Runs the specified AIX command and replaces the object specified by the
Object parameter with the output of the command. If the command expects
standard input, the specified object is used as input.
- Interrupting and ending the vi editor
-
Q
- Enters the ex editor in command mode
-
ZZ
- Exits the vi editor, saving changes.
-
:q
- Quits the vi editor. If you have changed the contents of the editing
buffer, the vi editor displays a warning message and does not quit.
-
:q!
- Quits the vi editor, discarding the editing buffer.
-
Esc
- Ends text input or ends an incomplete subcommand.
-
Ctrl-?
- Interrupts a subcommand.
To set the vi editor up as your default editor, use the EDITOR
command. The EDITOR variable can also be put into the user .profile or
the /etc/profile file.
# EDITOR=vi
Note |
---|
The EDITOR command sets a variable and must not be confused with
the set -o vi command that sets up command line editing.
|
15.2 Editing a File Using
the vi Editor