Visual interface editor clone (UNIX)
elvis [options]... [+command] file...
Neutrino
The elvis utility is an interactive fullscreen editor that is compatible with the Unix/POSIX vi editor. The vi utility in QNX Neutrino is a link to elvis.
In elvis, changes are buffered and are written to the file only upon request. Since a temporary file is used for storage, elvis can edit files larger than the amount of memory available on the machine it's run on.
There are two major command modes:
You can switch between modes. You'll probably use the visual command mode most of the time. This is the mode elvis normally starts up in.
In visual mode, the entire screen is filled with lines of text from your file (except the last screen line, which is reserved for status). You can view text and move around the file. Each keystroke is interpreted as part of a visual command. If you start typing text, it isn't inserted; instead, it's treated as part of a command. To insert text, you must first give an “insert text” command (see “Inserting text”).
The ex mode is quite different. In this mode, elvis displays a “:” character on the bottom line of the screen as a prompt. You are then expected to type in a command and press Enter. The set of commands recognized in ex mode differs from those in visual mode, and are known as ex commands. A summary of these commands is found at the end of this description. (A line-oriented editor, ex is a predecessor of vi, and the two share common functionality.)
The capabilities of elvis are described throughout the following sections:
Most visual mode commands are one keystroke long. The following sections list the operation performed by each keystroke, and any necessary options or operands.
Most commands may be preceded by a decimal number. Usually, this number specifies how many times the command is to be repeated. Without this number, the command in most cases executes just once.
You can't enter text into your file directly from visual command mode. Instead, you must first give a command that puts you into input mode. The commands to do this are:
A, C, I, O, R, S, a, c, i, o, s
Visual command mode looks a lot like text input mode. If you forget which mode you're in, just press Esc. If elvis beeps, you're in visual command mode. If elvis doesn't beep, you were in input mode — by pressing Esc you switch to visual command mode. One way or another, after you press Esc, elvis is ready for a command. Note that if the showmode option is set (this option described in the “Options” section below), the mode is displayed in the lower right-hand corner as either Command or Input. Also note that each mode uses a different cursor shape. |
Among its various commands, elvis has seven basic but powerful operators that let you change, delete, cut, paste, shift, or filter text regions. Although they're described below in the appropriate sections, you should note that, for the most part, they all share a common form:
op<object>
Where op can be:
One operator doesn't take an object:
The object operand specifies the range of text that the op operator acts upon. The object can be any movement command or pattern search. (See “Movement commands.”) In this way, you can see the power of the operators. The elvis utility can act on words, sentences, regions up to a specified pattern — whatever range the object operand specifies.
When you give a movement command as an operand, you can specify an optional count [n] to multiply the effect of the operand (e.g. d2w to delete next two words). When a count isn't provided, it typically defaults to 1.
Operators are frequently used with lines. So, for simplicity, a few special forms of these commands operate only with line objects. The following list shows the syntax of these special forms. Note the count is optional, and precedes the operator (this differs from the above form):
When you yank, delete, change, shift, or filter text, elvis saves the affected region in a single unnamed cut buffer, which you can recall by the p (put) command. However, elvis also has 26 named buffers, a to z, that you can use to save blocks of text during an editing session. These buffers are often used when editing multiple files to move text around.
You can prefix the previously defined operator forms by "a through "z (represented below by "<a-z>) to indicate which cut buffer to use to store the modified text region:
When you use the uppercase letters to denote the named buffers, the objects are appended to the buffer:
The following movement commands provide a convenient means to position the cursor throughout the file being edited. You can precede most by an optional count to repeat the action. More importantly, you can use these movement commands as operands to the change, yank, delete, put commands to specify the range of action to be performed (see “Inserting text” and “Deleting, yanking, putting” sections).
The movement commands operate on the following text objects:
. ! ?
To move the cursor, you can use the keypad arrow keys; you can also use the H, J, K, and L keys.
Tags can't be included as <object> operands for commands such as change, yank, put, delete, shift, and filter. |
The following commands enter input mode, where the text you enter (until you press Esc) is put into the file:
If the range is within the current line, a $ is displayed at the end of the <object> to indicate end-of-range. Otherwise, the text in the range is deleted and you're placed in input mode. When within a line, the specified range of text isn't deleted until you type Enter.
In input mode, all keystrokes are inserted into the text at the cursor's position, except for the following:
As mentioned in the introduction, elvis supports very powerful operator constructs. In this section we describe the delete, yank, and put operators.
When you have a sequence of text you want to copy or move to a different location, you first use the yank or delete operators to copy or delete the data into a buffer; you then use the put command to place the data at its new location.
If you're using named buffers, you can switch to another file before putting the text back. Thus you can copy from one file to another.
To copy the next four lines, yank them with a command such as 4yy or y4j, move to a new location and put the text by typing the p command.
To delete the next four lines, type 4dd or d4j.
To move the next four lines, use a command such as 4ddor d4j, then move to a new location and put the text by typing p or :pu.
With the filter command, you can select regions of text and run them through any command and insert the output into the file. The text in the range specified from the current line to the delimited <object> is filtered through the command and replaces the region specified. The text that was in the region before being replaced by the output of the command is saved in the unnamed buffer, or in a named buffer if one was specified. If uppercase letters are used, elvis appends text to the named buffer.
The shift operators, < and >, shift all the lines delimited by the current line and the <object> operand. Text is shifted by the value of the shiftwidth option (see below). The forms of the shift command are:
Named buffer prefixes don't work with the shift operator. |
A pattern used for searching and substituting is called a regular expression. While most characters match themselves in a search request, some have special meaning, as described in the table below. To be used in a search expression, these special characters must be preceded by a backslash (\).
Character | Meaning |
---|---|
^ | Match “beginning of line”. |
$ | Match “end of line”. |
. | Match any single character except the newline. |
\< | Match the beginning of a word. |
\> | Match the end of a word. |
[string] | Match any single character in string. |
[^string] | Match any single character not in string. |
[x-y] | Match any character between x and y (range). |
[^x-y] | Match any character not between x and y (range). |
* | Match any repeated characters. |
\ | Escape special characters. |
The commands for searching are as follows:
For example, to match fred1, fred2, or fred3:
/fred[1-3]
To match QNX Neutrino at the beginning of a line:
/^QNX Neutrino
To match QNX Neutrino at the end of a line:
/QNX Neutrino$
The global and substitute commands can operate over a range of lines. You can specify a range wherever [x,y] is indicated. The first element, x, indicates the first line of the range, and the second element, y, indicates the last line. You can use line numbers or any of the following special characters as range elements:
A range element may also be a pattern specification:
/pattern/
or a marked location:
'<a-z>
For example, to print the lines from the next line containing steve until the first subsequent blank line:
/steve,/^$/!lp
The substitute command substitutes text matching a pattern with replacement text:
:[x,y]s/pattern/replacement_text/[c][g][p]
If none of the modifiers c, g, or p is specified, this command replaces the first occurrence of the given pattern. You can modify this behavior by specifying any combination of the three modifiers:
This command is very powerful when used in conjunction with the global command (see below).
The global command searches through the lines of the specified range — or through the whole file if no range ([x,y]) is specified — for lines that contain the pattern. The command is performed on each matching line. The global command is of the following form:
You can combine the substitute and global commands, using the following syntax:
[x,y]g/pattern/s//replacement_text/
This command runs the substitute command on every occurrence of a matched pattern within the given range. The null pattern specification (i.e. //) indicates to the substitute command that it's to use the currently matched global pattern as the text it's to replace.
The following variations may also be used:
[x,y]g!/pattern/s//replacement_text/ [x,y]v/pattern/s//replacement_text/
For example, substitute the word fred with the word barney in lines 1 to 10:
1,10g/fred/s//barney
Match every mary that's at the beginning of a line and prompt the user to confirm the substitution:
1,$g/^mary/s//dave/c
On occasion, you need to undo the effects of a command:
The editor saves the last nine deleted blocks of text in save buffers. You can retrieve a buffer with the following commands:
If you accidentally select the wrong buffer, you can use the undo command to clear it and then try specifying a different buffer.
You can also use named buffers (see “Deleting, yanking, putting”):
The scrolling commands are as follows:
To position the current line at different positions on the screen by scrolling forward/backward:
The status commands are as follows:
The following commands write to a file. You can precede all w commands by an [x,y] range. For example, specify 1,6w to write the first 6 lines.
See also the :ta (tags) movement command.
To execute a single command from within elvis, you can use the following command:
To execute more than one command, you can create a shell:
Macros let you bind a single key to an arbitrary set of editing commands.
Defining a macro is simple: you define an lhs (left-hand side), which is the single character you want translated, followed by an rhs (right hand side), which is the sequence it is mapped into.
For example, :map q :wqCtrl-VEnterEnter maps q into :wqEnter. The Ctrl-V is needed to quote (escape) the first Enter, while the second Enter ends the map definition.
Word abbreviations are similar to macros, but they expand a short word into a longer word or words. If you type the abbreviation as part of a longer word, it's left alone. Abbreviations are used in input mode primarily to save typing.
Substitution isn't performed until you type a nonalphanumeric character to mark the end of the word. If you type Ctrl-V before the nonalphanumeric character, elvis doesn't perform the substitution.
For example:
:ab woof mary had a little ram
This inserts the text mary had a little ram whenever the word woof is typed.
You can set or examine options via the colon command set. The values of options affects the operation of subsequent commands.
There are three option types:
To print all the option settings, type:
:set all
For convenience, options have both a long descriptive name and a short name (shown in parentheses below) that's easier to type. You may use either interchangeably.
In input mode, if autoindent is on, each added line begins with the same amount of leading white space as the line above it. Without autoindent, added lines begin at column zero.
This option affects only ex mode. If this option is on, and either the cursor has moved to a different line or the previous command modified the file, elvis prints the current line.
If you've made modifications to the current file then try switching to another file, (e.g. :tag, or :next...), elvis normally prints an error message and refuses to switch. If this option is on, elvis writes the modified version of the current file and switches to the new file.
elvis stores text in temporary files. This option lets you control which directory those temporary files appear in. The default is /tmp.
You can set this option only in a .exrc file; once elvis is started, the directory can't be changed.
If your /tmp directory is on ramdisk, recovery isn't possible if the system is rebooted. |
Remember :s// options.
The elvis utility normally rings a bell when you do something wrong. This option lets you disable the bell.
Normally, when elvis searches for text, it distinguishes between uppercase and lowercase letters. When this option is on, uppercase and lowercase are seen as equivalent.
In nolist mode (the default), elvis displays text in a “normal” manner — with tabs expanded to an appropriate number of spaces, etc. However, sometimes it's useful to have tab characters displayed differently. In list mode, each tab is displayed as ^I, and a $ is displayed at the end of each line.
The search mechanism in elvis can accept regular expressions — strings in which certain characters have special meaning. The magic option is normally on, which causes these characters to be treated specially. If you turn the magic option off (:set noma), then all characters except ^ and $ are treated literally. Both ^ and $ retain their special meanings regardless of the setting of magic.
The { and } commands move the cursor forward or backward in increments of one paragraph. Paragraphs may be separated by blank lines or by a leading “dot” command of a text formatter. Different text formatters use different dot commands. This option lets you configure elvis to work with your text formatter.
It's assumed your formatter uses commands that start with a “.” character at the front of a line, followed by a one- or two-character command name.
The value of the paragraphs option is a string in which each pair of characters is one possible form of your text formatter's paragraph command. For example, lines starting with .IP, .LP, or .PP are considered new paragraphs.
Prevent overwriting of original file. Normally, elvis lets you write back any file for which you have write permission. If you don't have write permission, all you can do is write the changed version of the file to a different file.
If you set the readonly option, elvis pretends you don't have write permission to any file you edit. This is useful when you mean to use elvis only to look at a file, not to change it. This way, you can't change the file accidentally.
This option is normally off, unless you use the view alias of elvis, which is like elvis except the readonly option is on.
Changes, deletes, and yanks may affect many lines. For commands affecting a lot of lines, elvis outputs a message saying what was done and how many lines were affected. This option lets you define what “a lot of lines” means. The default is 5, so a message is shown on the status line for any command affecting 5 or more lines.
Scroll amount for Ctrl-U and Ctrl-D. These commands normally scroll backward or forward by half a screenfull, but you can adjust this. The value of this option says how many lines those keys should scroll. You can also set this with [n]Ctrl-D and [n]Ctrl-U.
The [[ and ]] commands move the cursor backward or forward in increments of one section. Sections may be delimited by a { character in column 1 (which is useful for C source code) or by means of a text formatter's “dot” commands.
This option lets you configure elvis to work with your text formatter's section command, in exactly the same way that the paragraphs option makes it work with the formatter's paragraphs command. For example, lines starting with .SH or .NH are considered new sections.
When elvis forks a shell (perhaps for the :! or :sh commands), this is the program it uses as a shell. The normal default is /bin/sh. However, if you have set the SHELL environment variable, the default value is copied from the environment.
The < and > commands shift text left or right by a uniform number of columns. The shiftwidth option defines that uniform number. The default is 8 spaces.
With showmatch set, every time you type ), }, or ] in input mode, elvis momentarily moves the cursor to the matching opening parenthesis or bracket.
In visual mode, it's easy to forget whether you're in the visual command mode, or input/replace mode. Normally, the showmode option is off, and you aren't informed as to which mode you're in. If you turn the showmode option on, a message appears in the lower right corner of your screen, telling you which mode you're in.
The width of tab characters.
This read-only option shows the name of the terminal entry that elvis is using for your terminal.
If you've modified a file, but not yet written it back to disk, elvis normally prints a warning before executing a :!cmd" command. However, in nowarn mode, this warning isn't given.
Normally, elvis also prints a message after a successful search that wrapped at EOF. The [no]warn option also disables this warning.
Wrap long lines in input mode. Normally (with wrapmargin=0), elvis lets you type in extremely long lines.
However, with wrapmargin set to something other than 0 (wrapmargin=70 is nice), long lines are automatically “wrapped” on a word break for lines longer than wrapmargin's setting.
Normally, when you search for something, elvis finds it no matter where it is in the file. elvis starts at the cursor position, and searches forward. If elvis reaches EOF without finding what you're looking for, it wraps around to continue searching from line 1, up to the current line.
If you turn off the wrapscan option (:se nows), and elvis subsequently reaches EOF during a search, it stops and says so.
You can configure the option settings with the EXINIT
environment variable:
EXINIT="set ai aw" export EXINIT |
The following list of ex commands is intended for your convenience only. It's beyond the scope of this document to describe the operations of these commands in detail. Most of these commands have been indicated as alternatives to the visual mode commands described in the above sections.
The general form of these commands is:
:[x,y] command parameter
[Prefix] | Command | Short form |
---|---|---|
abbrev abbr text | ab | |
[line] | append | a |
args | ar | |
[x[,y]] | change | c |
[x[,y]] | copy line | co |
[x[,y]] | delete ["named_buffer] | d |
edit[!] file | e | |
file | f | |
[x[,y]] | global /pattern/command/ | g |
[line] | insert | i |
[x[,y]] | join | j |
[x[,y]] | list | l |
map lhs rhs | map | |
[x[,y]] | move line | m |
next[!] | n | |
[x[,y]] | number | nu |
[x[,y]] | p | |
[line] | put ["named_buffer] | pu |
quit[!] | q | |
[line] | read file | !command | r |
rewind[!] | rew | |
set [option] | se | |
shell | sh | |
source file | so | |
[x[,y]] | substitute /pattern/text/[c][g][p] | s |
tag | ta | |
unabbrev abbr | una | |
undo | u | |
unmap lhs | unm | |
version | ve | |
visual | vi | |
[x[,y]] | write[!][[>>]file] | w |
xit[!] | x | |
[x[,y]] | yank ["named_buffer] | ya |
The following ex commands have no long version:
Command | Form |
---|---|
escape | !command |
print next | CR |
lshift | < |
rshift | > |
The elvis utility requires that the TERM
environment variable be set to indicate your terminal type. For example,
if elvis is running on a console, TERM should be
set as follows:
export TERM=qansi or export TERM=qnx depending on the mode your console is running in (QNX vs ANSI). |
Steve Kirkendall
When displayed, long lines scroll horizontally. On some implementations, these wrap onto multiple rows on the screen.
Under QNX Neutrino, you must have a /tmp directory for temporary files. To use a RAM disk as /tmp, try this:
ln -sP /dev/shmem /tmp
Using Editors in the Neutrino User's Guide
Linda Lamb, Learning the vi Editor, O'Reilly and Associates, 1990