This document is to aid a new vi user into operating ex mode. This tutor is meant to further one's ability after they understand basic vi mode operations. If your an absolute beginner and have vim installed you can run the command “vimtutor†at your terminal. This document is meant to be read after the user is comfortable with the visual layer of vi.
Bill Joy’s vi was initially a five mode operational terminal editor. The current implementations only have four modes whereas the original fifth mode known as “open†mode was the same as the visual mode meant for terminals that had no screen cursor (i.e. paper based hardcopy terminals).
Both nvi and vim have the four modes of operation which are the visual presentation layer we know as vi, text input mode; accessible from the vi layer, the ex mode for executing commands as well as an input mode accessible from ex.
Though most users consider ex a mode of vi; the presentation layer is what they see when they first start the editor. Interestingly enough it’s the opposite. The vi layer is actually a mode built on top of ex; therefore is “visual on exâ€.
The ex editor is based on source derived from ed, the venerable UNIX line editor. The tutorial uses syntax from ed to help new users understand how to use ex. Information on how to get into ex from vi mode is at the end of the tutorial.
From the command line to enter ed is is as simple as this syntax:
This will give it a nice user friendly prompt.
Append
First we will add some text to the buffer to work with. This is as simple as using the append command (a) which will put the editor into text input mode. To return to the command mode type single dot (.) at the beginning of the line followed immediately by a carriage return as so:
Addressing
To address a line one could simply type the line number. for example to see the second line:
For adding text after a line address one would use the address combined with an append ‘a’ command as so:
Print
To view the change one could also use a range for addressing with the command (p) to print the text in that range. Address ranges are delimited by the comma.
This can be read as follows: print from line 2 to line 4.
Insert
To enter text before a line one can insert text with the insert (i) command which in this case is the analog to ‘a’
To view the entire buffer, which contains could be titled “The Evolution of Unix Text Utilitiesâ€, one might consider the ‘1,5p’. Though this method will work the editor can address the variable last line with the currency symbol ($) thus can be written as 1,$p. Shorthand for printing the first line to the end is the percent symbol (%) which was further shortened to simply comma (,) for making it accessible for touch typists. Therefore all of these constructs will provide the same results:
In ex the print (p) command is optional as it’s as it’s implied. Therefore the first two examples could be constructed as :1,$ and :% in ex. Finally the ed construct ‘,p’ is not interpreted in ex. For touch typists looking for a solution which would not require using shift key I have found that :1, works well.
The dot (.) will print the current line. Most commands leave the current line at the last which was affected. Here is an example usage of (.):
To address before the first line the user can use address 0 where it makes sense. For example 0a would be the equivalent to 1i in ed. Though 0i doesn’t work in ed; it is interpreted in ex. The simple fact that ex knows “what the user meant†shows where Joy wanted a more user friendly version of ed when creating ex.
Delete
Here is example usage which also shows how one would use delete (d) to remove one or more lines and finally to change (c) a line :
The last statement you’ll see that the file is intact. If a mistake was made the user could undo (u) the last operation with the ‘u’ command.
Join
The final command to take only a range is to join (j) two lines together. The operation will combine two or more lines into one. An example join line 4 to the end of line is (3,4j) or line 3 to the END of the document to line 2 would be (2,$j).
List
The join (j) command would be best to used in conjunct with the list (l) command which prints the text with a currency symbol ($) at the end of each line where a carriage return has taken place. The command (%l) will show where all the carriage returns are in the buffer.
Change
The change (c) command can take a range address as well such as (2,4c or .,$c). The behavior is the same. If the range is of three lines those three lines will be replaced If less than three lines are entered through input mode the rest of the text is truncated to the last entered line. It’s essentially a combination of delete the addressed line (1d) and insert at the addressed line (1i).
Input commands boil down to a (append), i (insert) or c (change). These are the only commands which will move the editor from command mode to input mode.
Address Numbering
To view the number of lines in in the buffer the (=) symbol will provide the integer. In combination with the current line number would be to combine the two as so ‘.=’ . Numbers can be printed at the beginning of the line with the number (n) command. Here is an example:
Commands may require zero, one, or two addresses. If no address is given the default address (.) is implied.
Move and Transfer: cut, copy and paste
The last two line based operations are similar to the gui paradigm of cut, copy, and paste. In vi mode to copy a line one would “yank†the text and to paste one would “put†the yanked text a line below where the cursor was placed. In ed and ex the operations are a bit simpler. The awkwardly named transfer (t) command is a copy then paste operation which syntax would look like (4,5t2) which copies lines 3 through 4; then pastes them to line 2. The move (m) command has the same structure; its operation is effectively the same as the cut and paste metaphor today. A simple move example would be (3,$m0) which is cut lines 3 through END and paste the lines to the beginning of the file.
Bill Joy’s vi was initially a five mode operational terminal editor. The current implementations only have four modes whereas the original fifth mode known as “open†mode was the same as the visual mode meant for terminals that had no screen cursor (i.e. paper based hardcopy terminals).
Both nvi and vim have the four modes of operation which are the visual presentation layer we know as vi, text input mode; accessible from the vi layer, the ex mode for executing commands as well as an input mode accessible from ex.
Though most users consider ex a mode of vi; the presentation layer is what they see when they first start the editor. Interestingly enough it’s the opposite. The vi layer is actually a mode built on top of ex; therefore is “visual on exâ€.
The ex editor is based on source derived from ed, the venerable UNIX line editor. The tutorial uses syntax from ed to help new users understand how to use ex. Information on how to get into ex from vi mode is at the end of the tutorial.
From the command line to enter ed is is as simple as this syntax:
% ed -p '(ed)~> '
This will give it a nice user friendly prompt.
Append
First we will add some text to the buffer to work with. This is as simple as using the append command (a) which will put the editor into text input mode. To return to the command mode type single dot (.) at the beginning of the line followed immediately by a carriage return as so:
Code:
(ed)~> [B]a[/B]
The ed utility is a line-oriented text editor
Vi is a a screen oriented text editor
Vim is a text editor that is upwards compatible to Vi
[B].[/B]
(ed)~>
Addressing
To address a line one could simply type the line number. for example to see the second line:
Code:
(ed)~> [B]2
[/B]Vi is a a screen oriented text editor
For adding text after a line address one would use the address combined with an append ‘a’ command as so:
Code:
(ed)~> [B]2a[/B]
Bostic’s nvi is a bug-for-bug compatible replacement of 4BSD vi
.
To view the change one could also use a range for addressing with the command (p) to print the text in that range. Address ranges are delimited by the comma.
Code:
(ed)~> [B]2,4p[/B]
Vi is a a screen oriented text editor
Bostic’s nvi is a bug-for-bug compatible replacement of 4BSD vi
Vim is a text editor that is upwards compatible to Vi
This can be read as follows: print from line 2 to line 4.
Insert
To enter text before a line one can insert text with the insert (i) command which in this case is the analog to ‘a’
Code:
(ed)~> [B]2i[/B]
The line editor ex, short for EXtended, is a fork of source derived from ed
.
To view the entire buffer, which contains could be titled “The Evolution of Unix Text Utilitiesâ€, one might consider the ‘1,5p’. Though this method will work the editor can address the variable last line with the currency symbol ($) thus can be written as 1,$p. Shorthand for printing the first line to the end is the percent symbol (%) which was further shortened to simply comma (,) for making it accessible for touch typists. Therefore all of these constructs will provide the same results:
Code:
(ed)~> [B]1,$p[/B]
(ed)~> [B]%p[/B]
(ed)~> [B],p[/B]
In ex the print (p) command is optional as it’s as it’s implied. Therefore the first two examples could be constructed as :1,$ and :% in ex. Finally the ed construct ‘,p’ is not interpreted in ex. For touch typists looking for a solution which would not require using shift key I have found that :1, works well.
The dot (.) will print the current line. Most commands leave the current line at the last which was affected. Here is an example usage of (.):
Code:
(ed)~> [B]3[/B]
Vi is a a screen oriented text editor
(ed)~> [B].[/B]
Vi is a a screen oriented text editor
(ed)~> [B]2[/B]
The line editor ex, short for EXtended, is a fork of source derived from ed
(ed)~> [B].,3p[/B]
The line editor ex, short for EXtended, is a fork of source derived from ed
Vi is a a screen oriented text editor
(ed)~> [B].,$[/B]
Vim is a text editor that is upwards compatible to Vi
To address before the first line the user can use address 0 where it makes sense. For example 0a would be the equivalent to 1i in ed. Though 0i doesn’t work in ed; it is interpreted in ex. The simple fact that ex knows “what the user meant†shows where Joy wanted a more user friendly version of ed when creating ex.
Delete
Here is example usage which also shows how one would use delete (d) to remove one or more lines and finally to change (c) a line :
Code:
(ed)~> [B]0i[/B]
?
(ed)~> [B]0a[/B]
top
.
(ed)~> [B]1,2p[/B]
top
The ed utility is a line-oriented text editor
(ed)~> [B]1c[/B]
This is the top
of the file
.
(ed)~> [B]1,4p[/B]
This is the top
of the file
The ed utility is a line-oriented text editor
The line editor ex, short for EXtended, is a fork of source derived from ed
(ed)~> [B]1,2d[/B]
(ed)~> [B]%p[/B]
The last statement you’ll see that the file is intact. If a mistake was made the user could undo (u) the last operation with the ‘u’ command.
Join
The final command to take only a range is to join (j) two lines together. The operation will combine two or more lines into one. An example join line 4 to the end of line is (3,4j) or line 3 to the END of the document to line 2 would be (2,$j).
List
The join (j) command would be best to used in conjunct with the list (l) command which prints the text with a currency symbol ($) at the end of each line where a carriage return has taken place. The command (%l) will show where all the carriage returns are in the buffer.
Change
The change (c) command can take a range address as well such as (2,4c or .,$c). The behavior is the same. If the range is of three lines those three lines will be replaced If less than three lines are entered through input mode the rest of the text is truncated to the last entered line. It’s essentially a combination of delete the addressed line (1d) and insert at the addressed line (1i).
Input commands boil down to a (append), i (insert) or c (change). These are the only commands which will move the editor from command mode to input mode.
Address Numbering
To view the number of lines in in the buffer the (=) symbol will provide the integer. In combination with the current line number would be to combine the two as so ‘.=’ . Numbers can be printed at the beginning of the line with the number (n) command. Here is an example:
Code:
(ed)~> [B]4[/B]
Bostic’s nvi is a bug-for-bug compatible replacement of 4BSD vi
(ed)~> [B].[/B]
Bostic’s nvi is a bug-for-bug compatible replacement of 4BSD vi
(ed)~>[B] .=[/B]
4
(ed)~> [B]1[/B]
The ed utility is a line-oriented text editor
(ed)~> [B].[/B]
The ed utility is a line-oriented text editor
(ed)~> [B].=[/B]
1
(ed)~> [B]=[/B]
5
Commands may require zero, one, or two addresses. If no address is given the default address (.) is implied.
Move and Transfer: cut, copy and paste
The last two line based operations are similar to the gui paradigm of cut, copy, and paste. In vi mode to copy a line one would “yank†the text and to paste one would “put†the yanked text a line below where the cursor was placed. In ed and ex the operations are a bit simpler. The awkwardly named transfer (t) command is a copy then paste operation which syntax would look like (4,5t2) which copies lines 3 through 4; then pastes them to line 2. The move (m) command has the same structure; its operation is effectively the same as the cut and paste metaphor today. A simple move example would be (3,$m0) which is cut lines 3 through END and paste the lines to the beginning of the file.