| Linux in a nutshell, By Ellen Siever, Stephen Spainhour, Stephen Figgins and Jessica P. Hekman source ref: linuxinanutshell.html |
|
Review of vi Operations
vi Command-Line Options
ex Command-Line Options
Movement Commands
Edit Commands
Saving and Exiting
Accessing Multiple Files
Interacting with the Shell
Macros
Miscellaneous Commands
Alphabetical List of Keys in Command Mode
Syntax of ex Commands
Alphabetical Summary of ex Commands
vi Configuration
vi is the classic screen-editing program for Unix. A number of enhanced versions exist, including nvi, vim, vile, and elvis. On Linux, the vi command is usually a link to one of these programs.
vi is based on an older line editor called ex. Powerful editing capabilities can be invoked within vi by pressing the colon (:), entering an ex command, and pressing the Return key. Furthermore, you can place ex commands in a startup file called ~/.exrc, which vi reads at the beginning of your editing session. Because ex commands are still an important part of vi, they also are described in this chapter. On Linux, ex is sometimes called hex.
This chapter, which essentially covers standard vi but reflects nvi extensions, presents the following topics:
For more information, see the O'Reilly book Learning the vi Editor by Linda Lamb and Arnold Robbins.
This section provides a review of the following:
Once the file is opened, you are in command mode. From command mode, you can:
In insert mode, you can enter new text in the file. Press the Esc or Ctrl-[ keys to exit insert mode and return to command mode. The following commands invoke insert mode:
In vi, commands have the following general form:
[n] operator [m] object
The basic editing operators are:
If the current line is the object of the operation, then the operator is the same as the object: cc, dd, yy. Otherwise, the editing operators act on objects specified by cursor-movement commands or pattern-matching commands. n and m are the number of times the operation is performed or the number of objects the operation is performed on. If both n and m are specified, the effect is n × m.
An object can represent any of the following text blocks:
Most commands are not echoed on the screen as you input them. However, the status line at the bottom of the screen is used to echo input for the following commands:
Commands that are input on the status line must be entered by pressing the Return key. In addition, error messages and output from the Ctrl-G command are displayed on the status line.
The three most common ways of starting a vi session are:
vi file vi + n file vi +/ pattern file
You can open file for editing, optionally at line n or at the first line matching pattern. If no file is specified, vi opens with an empty buffer. The command-line options that can be used with vi are:
While most people know ex commands only by their use within vi, the editor exists also as a separate program and can be invoked from the shell (for instance, to edit files as part of a script). Within ex, you can enter the vi or visual command to start vi. Similarly, within vi, you can enter Q to quit the vi editor and enter ex.
If you invoke ex as a standalone editor, you can include the following options:
You can exit ex in several ways:
A number preceding a command repeats the movement. Movement commands are also objects for change, delete, and yank operations.
| Command | Action |
|---|---|
| h, j, k, l | Left, down, up, right ( , , , ) |
| Spacebar | Right |
| Backspace | Left |
| Ctrl-H | Left |
| Command | Action |
|---|---|
| w, b | Forward, backward by word (treating punctuation marks as words). |
| W, B | Forward, backward by word (recognizing only whitespace, not punctuation, as separators). |
| e | End of word (treating a punctuation mark as the end of a word). |
| E | End of word (recognizing only whitespace as the end of a word). |
| ), ( | Beginning of next, current sentence. |
| }, { | Beginning of next, current paragraph. |
| ]], [[ | Beginning of next, current section. |
| Ctrl-D | Move to previous tab setting. |
| Ctrl-T | Move to next tab setting. |
| Ctrl-W | Move back one word. |
| Command | Action |
|---|---|
| 0, $ | First, last position of current line. |
| ^, _ | First nonblank character of current line. |
| +, - | First character of next, previous line. |
| Return | First nonblank character of next line. |
| n| | Column n of current line. |
| H | Top line of screen. |
| M | Middle line of screen. |
| L | Last line of screen. |
| nH | n lines after top line. |
| nL | n lines before last line. |
| Ctrl-J | Move down one line. |
| Ctrl-M | Move to first nonblank character of next line. |
| Command | Action |
|---|---|
| Ctrl-F, Ctrl-B | Scroll forward, backward one screen. |
| Ctrl-D, Ctrl-U | Scroll down, up one-half screen. |
| Ctrl-E, Ctrl-Y | Show one more line at bottom, top of window. |
| z Return | Reposition line with cursor to top of screen. |
| z. | Reposition line with cursor to middle of screen. |
| z- | Reposition line with cursor to bottom of screen. |
| Ctrl-L, Ctrl-R | Redraw screen (without scrolling). |
| Command | Action |
|---|---|
| /pattern | Search forward for pattern. |
| / | Repeat previous search forward. |
| /pattern/+n | Go to line n after pattern. |
| ?pattern | Search backward for pattern. |
| ? | Repeat previous search backward. |
| ?pattern?-n | Go to line n before pattern. |
| n | Repeat previous search. |
| N | Repeat previous search in opposite direction. |
| % | Find match of current parenthesis, brace, or bracket. |
| fx | Move forward to x on current line. |
| Fx | Move backward to x on current line. |
| tx | Move forward to just before x in current line. |
| Tx | Move back to just after x in current line. |
| , | Reverse search direction of last f, F, t, or T. |
| ; | Repeat last character search (f, F, t, or T). |
| Command | Action |
|---|---|
| Ctrl-G | Display current filename and line number. |
| nG | Move to line number n. |
| G | Move to last line in file. |
| :n | Move to line number n. |
| Command | Action |
|---|---|
| mx | Mark current position with character x. |
| `x | (backquote) Move cursor to mark x. |
| 'x | (apostrophe) Move to start of line containing x. |
| `` | (backquotes) Return to previous mark (or location prior to search). |
| '' | (apostrophes) Like preceding, but return to start of line. |
Recall that c, d, and y are the basic editing operators.
| Command | Action |
|---|---|
| a | Append after cursor. |
| A | Append to end of line. |
| i | Insert before cursor. |
| I | Insert at beginning of line. |
| o | Open a line below cursor. |
| O | Open a line above cursor. |
| Esc | Terminate insert mode. |
| Tab | Insert a tab. |
| Backspace | Delete previous character (in insert mode). |
| Ctrl-I | Insert a tab. |
| Ctrl-U | Delete current line. |
| Ctrl-V | Insert next character verbatim. |
| Ctrl-[ | Terminate insert mode. |
Some of the control characters listed in the previous table are set by stty. Your terminal settings may differ.
The following table is not exhaustive but illustrates the most common operations.
| Command | Action |
|---|---|
| cw | Change through end of current word. |
| cc | Change line. |
| c$ | Change text from current position to end-of-line. |
| C | Same as c$. |
| dd | Delete current line. |
| d$ | Delete remainder of line. |
| D | Same as d$. |
| ndd | Delete n lines. |
| dw | Delete a word. |
| d} | Delete up to next paragraph. |
| d^ | Delete back to beginning of line. |
| d/pattern | Delete up to first occurrence of pattern. |
| dn | Delete up to next occurrence of pattern. |
| dfa | Delete up to and including a on current line. |
| dta | Delete up to (not including) a on current line. |
| dL | Delete up to last line on screen. |
| dG | Delete to end-of-file. |
| p | Insert last deleted text after cursor. |
| P | Insert last deleted text before cursor. |
| rx | Replace character with x. |
| Rtext | Replace text beginning at cursor. |
| s | Substitute character. |
| ns | Substitute n characters. |
| S | Substitute entire line. |
| u | Undo last change. |
| U | Restore current line. |
| x | Delete current character. |
| X | Delete back one character. |
| nX | Delete previous n characters. |
| . | Repeat last change. |
| ~ | Reverse case. |
| & | Repeat last substitution. |
| Y | Copy (yank) current line to temporary buffer. |
| yy | Same as Y. |
| "xyy | Copy current line to buffer x. |
| ye | Copy text to end of word into temporary buffer. |
| yw | Same as ye. |
| y$ | Copy rest of line into temporary buffer. |
| "xdd | Delete current line into buffer x. |
| "Xdd | Delete current line and append to buffer x. |
| "xp | Put contents of buffer x. |
| J | Join previous line to current line. |
| :j! | Same as J. |
Writing a file means saving the edits and updating the file's modification time.
| Command | Action |
|---|---|
| ZZ | Quit vi, writing the file only if changes were made. |
| :x | Same as ZZ. |
| :wq | Write and quit file. |
| :w | Write file. |
| :w file | Save copy to file. |
| :n1,n2w file | Write lines n1 to n2 to new file. |
| :n1,n2w >> file | Append lines n1 to n2 to existing file. |
| :w! | Write file (overriding protection). |
| :w! file | Overwrite file with current buffer. |
| :w %.new | Write current buffer named file as file.new. |
| :q | Quit file. |
| :q! | Quit file (discarding edits). |
| Q | Quit vi and invoke ex. |
| :vi | Return to vi after Q command. |
| % | Current filename. |
| # | Alternate filename. |
| Command | Action |
|---|---|
| :e file | Edit another file; current file becomes alternate. |
| :e! | Restore last saved version of current file. |
| :e+ file | Begin editing at end of new file. |
| :e+ n file | Open new file at line n. |
| :e# | Open to previous position in alternate file. |
| :ta tag | Edit file containing tag at the location of the tag. |
| :n | Edit next file. |
| :n! | Force next file into buffer (don't save changes to current file). |
| :n files | Specify new list of files. |
| :args | Display multiple files to be edited. |
| :rew | Rewind list of multiple files to top. |
| Command | Action |
|---|---|
| :r file | Read in contents of file after cursor. |
| :r !command | Read in output from command after current line. |
| :nr !command | Like preceding, but place after line n (0 for top of file). |
| :!command | Run command, then return. |
| !object command | Send object, indicated by a movement command, as input to shell command command; replace object with command output. |
| :n1,n2! command | Send lines n1 through n2 to command; replace with output. |
| n!!command | Send n lines to command; replace with output. |
| !! | Repeat last system command. |
| !!command | Replace current line with output of command. |
| :sh | Create subshell; return to file with EOF. |
| Ctrl-Z | Suspend editor, resume with fg. |
| :so file | Read and execute ex commands from file. |
| Command | Action |
|---|---|
| :ab in out | Use in as abbreviation for out. |
| :unab in | Remove abbreviation for in. |
| :ab | List abbreviations. |
| :map c sequence | Map character c as sequence of commands. |
| :unmap c | Disable map for character c. |
| :map | List characters that are mapped. |
| :map! c sequence | Map character c to input mode sequence. |
| :unmap! c | Disable input mode map (you may need to quote the character with Ctrl-V). |
| :map! | List characters that are mapped to input mode. |
The following characters are unused in command mode and can be mapped as user-defined commands:
NOTE
The = is used by vi if LISP mode is set. Different versions of vi may use some of these characters, so test them before using them.
| Command | Action |
|---|---|
| < | Shift line left to position indicated by following movement command. |
| > | Shift line right to position indicated by following movement command. |
| << | Shift line left one shift width (default is 8 spaces). |
| >> | Shift line right one shift width (default is 8 spaces). |
| >} | Shift right to end of paragraph. |
| <% | Shift left until matching parenthesis, brace, bracket, etc. (Cursor must be on the matching symbol.) |
| ^[ | Abort command or end input mode. |
| ^] | Perform a tag look-up on the text under the cursor. |
| ^\ | Enter ex line-editing mode. |
| ^^ | (Caret key with Ctrl key pressed) Return to previously edited file. |
For brevity, control characters are marked by ^.
| Command | Action |
|---|---|
| a | Append text after cursor. |
| A | Append text at end-of-line. |
| ^A | Search for next occurrence of word under cursor. |
| b | Back up to beginning of word in current line. |
| B | Back up one word, treating punctuation marks as words. |
| ^B | Scroll backward one window. |
| c | Change text up to target of next movement command. |
| C | Change to end of current line. |
| ^C | End insert mode; interrupts a long operation. |
| d | Delete up to target of next movement command. |
| D | Delete to end of current line. |
| ^D | Scroll down half-window; in insert mode, unindent to shiftwidth if autoindent is set. |
| e | Move to end of word. |
| E | Move to end of word, treating punctuation as part of word. |
| ^E | Show one more line at bottom of window. |
| f | Find next character typed forward on current line. |
| F | Find next character typed backward on current line. |
| ^F | Scroll forward one window. |
| g | Unused. |
| G | Go to specified line or end-of-file. |
| ^G | Print information about file on status line. |
| h | Left arrow cursor key. |
| H | Move cursor to home position. |
| ^H | Left arrow cursor key; Backspace key in insert mode. |
| i | Insert text before cursor. |
| I | Insert text before first nonblank character on line. |
| ^I | Unused in command mode; in insert mode, same as Tab key. |
| j | Down arrow cursor key. |
| J | Join previous line to current line. |
| ^J | Down arrow cursor key; in insert mode, move down a line. |
| k | Up arrow cursor key. |
| K | Unused. |
| ^K | Unused. |
| l | Right arrow cursor key. |
| L | Move cursor to last position in window. |
| ^L | Redraw screen. |
| m | Mark the current cursor position in register (a-z). |
| M | Move cursor to middle position in window. |
| ^M | Move to beginning of next line. |
| n | Repeat the last search command. |
| N | Repeat the last search command in reverse direction. |
| ^N | Down arrow cursor key. |
| o | Open line below current line. |
| O | Open line above current line. |
| ^O | Unused. |
| p | Put yanked or deleted text after or below cursor. |
| P | Put yanked or deleted text before or above cursor. |
| ^P | Up arrow cursor key. |
| q | Unused. |
| Q | Quit vi and enter ex line-editing mode. |
| ^Q | Unused. (On some terminals, resume data flow.) |
| r | Replace character at cursor with the next character you type. |
| R | Replace characters. |
| ^R | Redraw the screen. |
| s | Change the character under the cursor to typed characters. |
| S | Change entire line. |
| ^S | Unused. (On some terminals, stop data flow.) |
| t | Find next character typed forward on current line and position cursor before it. |
| T | Find next character typed backward on current line and position cursor after it. |
| ^T | Unused in command mode; in insert mode, move to next tab setting. |
| u | Undo the last change made. |
| U | Restore current line, discarding changes. |
| ^U | Scroll the screen upward a half-window. |
| v | Unused. |
| V | Unused. |
| ^V | Unused in command mode; in insert mode, insert next character verbatim. |
| w | Move to beginning of next word. |
| W | Move to beginning of next word, treating punctuation marks as words. |
| ^W | Unused in command mode; in insert mode, back up to beginning of word. |
| x | Delete character under cursor. |
| X | Delete character before cursor. |
| ^X | Unused. |
| y | Yank or copy text up to target of following movement command into temporary buffer. |
| Y | Make copy of current line. |
| ^Y | Show one more line at top of window. |
| z | Reposition line containing cursor. z must be followed by Return (reposition line to top of screen), . (reposition line to middle of screen), or - (reposition line to bottom of screen). |
| ZZ | Exit the editor, saving changes. |
To enter an ex command from vi, type:
:[address] command [options]
An initial : indicates an ex command. As you type the command, it is echoed on the status line. Enter the command by pressing Return. address is the line number or range of lines that are the object of command. options and addresses are described in the following sections. ex commands are described in the alphabetical summary.
If no address is given, the current line is the object of the command. If the address specifies a range of lines, the format is:
x,y
where x and y are the first and last addressed lines (x must precede y in the buffer). x and y may be line numbers or symbols. Using ; instead of , sets the current line to x before interpreting y. The notation 1,$ addresses all lines in the file, as does %.
| Symbol | Meaning |
|---|---|
| 1,$ | All lines in the file |
| % | All lines; same as 1,$ |
| x,y | Lines x through y |
| x;y | Lines x through y, with current line reset to x |
| 0 | Top of file |
| . | Current line |
| n | Absolute line number n |
| $ | Last line |
| x-n | n lines before x |
| x+n | n lines after x |
| -[n] | One or n lines previous |
| +[n] | One or n lines ahead |
| 'x | Line marked with x |
| '' | Previous mark |
| /pattern/ | Forward to line matching pattern |
| ?pattern? | Backward to line matching pattern |
See Chapter 9, "Pattern Matching", for more information on using patterns.
ex commands can be entered by specifying any unique abbreviation. In this listing, the full name appears in the margin, and the shortest possible abbreviation is used in the syntax line. Examples are assumed to be typed from vi, so they include the : prompt.
| abbrev | ab [string text] Define string when typed to be translated into text. If string and text are not specified, list all current abbreviations.
ExamplesNote: ^M appears when you type Ctrl-V followed by Return. :ab ora O'Reilly & Associates, Inc. :ab id Name:^MRank:^MPhone: |
| append | [address] a[!] text . Append text at specified address, or at present address if none is specified. Add a ! to switch the autoindent setting that will be used during input (e.g., if autoindent was enabled, ! disables it). Terminate input by entering a line consisting of just a period. |
| args | ar Print filename arguments (the list of files to edit). The current argument is shown in brackets ([]). |
| cd | cd dir chdir dir Change current directory within the editor to dir. |
| change | [address] c[!] text . Replace the specified lines with text. Add a ! to switch the autoindent setting during input of text. Terminate input by entering a line consisting of just a period. |
| copy | [address] co destination Copy the lines included in address to the specified destination address. The command t is the same as copy. Example:1,10 co 50 Copy first 10 lines to just after line 50 |
| delete | [address] d [buffer] Delete the lines included in address. If buffer is specified, save or append the text to the named buffer.
Examples:/Part I/,/Part II/-1d Delete to line above "Part II" :/main/+d Delete line below "main" :.,$d Delete from this line to last line |
| edit | e[!] [+n] [file] Begin editing file. Add a ! to discard any changes to the current file. If no file is given, edit another copy of the current file. With the +n argument, begin editing on linen. Examples:e file :e# Return to editing the previous file :e! Discard edits since last save |
| exusage | exu [command] Print a brief usage message describing command, or a list of available commands if command is omitted. |
| file | f [filename] Change the name of the current file to filename, which is considered "not edited." If no filename is specified, print the current status of the file. Example:f %.new |
| global | [address] g[!]/pattern<//[commands] Execute commands on all lines that contain pattern or, if address is specified, on all lines within that range. If commands are not specified, print all such lines. If ! is used, execute commands on all lines that don't contain pattern. See v. Examples
|
| help | h
Print a brief help message. Information on particular commands can be obtained through the exusage and viusage commands. |
| insert | address i[!] text . Insert text at line before the specified address, or at present address if none is specified. Add a ! to switch the autoindent setting during input of text. Terminate input by entering a line consisting of just a period. |
| join | [address] j[!] [count] Place the text in the specified address on one line, with whitespace adjusted to provide two blank characters after a period (.), no blank characters after a ), and one blank character otherwise. Add a ! to prevent whitespace adjustment. Example:1,5j! Join first five lines, preserving whitespace |
| k | [address] k char Mark the given address with char. Return later to the line with 'char. |
| list | [address] l [count] Print the specified lines so that tabs display as ^I, and the ends of lines display as $. l is a temporary version of :set list. |
| map | map[!] [char
commands] Define a keyboard macro named char as the specified sequence of commands. char is usually a single character, or the sequence #n, representing a function key on the keyboard. Use a ! to create a macro for input mode. With no arguments, list the currently defined macros.
Examples:map K dwwP Transpose two words :map q :w^M:n^M Write current file; go to next :map! + ^[bi(^[ea) Enclose previous word in parentheses |
| mark | [address] ma char Mark the specified line with char, a single lowercase letter. Return later to the line with 'char. Same as k. |
| mkexrc | mk[!] file Create an .exrc file containing a set command for every ex option, set to defaults. |
| move | [address] m destination Move the lines specified by address to the destination address. Example:.,/Note/m /END/ Move text block after line containing "END" |
| next | n[!] [[+command] filelist] Edit the next file from the command-line argument list. Use args to list these files. If filelist is provided, replace the current argument list with filelist and begin editing on the first file; if command is given (containing no spaces), execute command after editing the first such file. Add a ! to discard any changes to the current file. Example:n chap* Start editing all "chapter" files |
| number | [address] nu [count] Print each line specified by address, preceded by its buffer line number. Use # as an alternate abbreviation for number. count specifies the number of lines to show, starting with address. |
| open | [address] o [/pattern/]
Enter vi's open mode at the lines specified by address or at the lines matching pattern. Enter and exit open mode with Q. Open mode lets you use the regular vi commands, but only one line at a time. May be useful on slow dial-up lines. |
| preserve | pre Save the current editor buffer as though the system had crashed. |
| previous | prev[!] Edit the previous file from the command-line argument list. |
| [address] p [count] [address] P [count] Print the lines specified by address. count specifies the number of lines to print, starting with address. Add a ! to discard any changes to the current file. Example:100;+5p Show line 100 and the next 5 lines |
|
| put | [address] pu [char] Restore the lines that were previously deleted or yanked from named buffer char, and put them after the line specified by address. If char is not specified, restore the last deleted or yanked text. |
| quit | q[!] Terminate current editing session. Use ! to discard changes made since the last save. If the editing session includes additional files in the argument list that were never accessed, quit by typing q! or by typing q twice. |
| read | [address] r file Copy in the text from file on the line below the specified address. If file is not specified, the current filename is used. Example:0r $HOME/data Read file in at top of current file |
| read | [address] r !command Read the output of Linux command into the text after the line specified by address. Example:$r !cal Place a calendar at end-of-file |
| recover | rec [file] Recover file from system save area. |
| rewind | rew[!] Rewind argument list and begin editing the first file in the list. The ! flag rewinds, discarding any changes to the current file that haven't been saved. |
| script | sc[!] [file] Create a new shell in a buffer that can be saved, optionally specifying file where the buffer can be saved. Can be used only in vi. |
| set | se parameter1 parameter2 ... Set a value to an option with each parameter, or if no parameter is supplied, print all options that have been changed from their defaults. For Boolean-valued options, each parameter can be phrased as option or nooption; other options can be assigned with the syntax option=value. Specify all to list current settings. Examples:set nows wm=10 :set all |
| shell | sh Create a new shell. Resume editing when the shell is terminated. |
| source | so file
Read and execute ex commands from file. Example:so $HOME/.exrc |
| stop | st Suspend the editing session. Same as Ctrl-Z. Use fg to resume session. |
| substitute | [address] s [/pattern/replacement/] [options]
[count] Replace each instance of pattern on the specified lines with replacement. If pattern and replacement are omitted, repeat last substitution. count specifies the number of lines on which to substitute, starting with address. When preceded by the global (g) or v command, this command can be specified with a blank pattern, in which case the pattern from the g or v command is then used. For more examples, see Section 9.4.1, "Examples of Searching and Replacing" in Chapter 9, "Pattern Matching". Options
Examples
|
| suspend | su Suspend the editing session. Same as Ctrl-Z. Use fg to resume session. |
| t | [address] t destination
Copy the lines included in address to the specified destination address. t is an alias for copy. Example:%t$ Copy the file and add it to the end |
| tag | [address] ta[!] tag Switch the editing session to the file containing tag. ExampleRun ctags, then switch to the file containing myfunction: :!ctags *.c :tag myfunction |
| tagnext | tagn[!] Find the next occurrence of the current tag. |
| tagpop | tagp[!] Forget the current tag and return to the last position of the previous tag found. |
| tagprev | tagpr[!] Return to the previous occurrence of the current tag. |
| tagtop | tagt[!] Return to the first tag searched for and forget about all tags. |
| unabbreviate | una word Remove word from the list of abbreviations. |
| undo | u Reverse the changes made by the last editing command. |
| unmap | unm[!] char
Remove char from the list of keyboard macros. Use ! to remove a macro for input mode. |
| v | [address] v/pattern/[commands] Execute commands on all lines not containing pattern. If commands are not specified, print all such lines. v is equivalent to g!. See global. Example:v/#include/d Delete all lines except "#include" lines |
| version | ve Print the editor's current version number. |
| vi | vi [+n]
file Begin editing file, optionally at line n. Can be used only in vi. |
| visual | [address] vi [type] [count] Enter visual mode (vi) at the line specified by address. Exit with Q. type can be one of -, ^, or . (See the z command.) count specifies an initial window size. |
| viusage | viu [key] Print a brief usage message describing the operation of key, or a list of defined keys if key is omitted. |
| wq | wq[!] Write and quit the file in one command. The ! flag forces the editor to write over any current contents of file. |
| write | [address] w[!] [[>>] file
Write lines specified by address to file, or write full contents of buffer if address is not specified. If file also is omitted, save the contents of the buffer to the current filename. If >>file is used, write contents to the end of an existing file. The ! flag forces the editor to write over any current contents of file. |
| write | [address] w !command Write lines specified by address to command. Examples:1,10w name_list Copy first 10 lines to name_list :50w >> name_list Now append line 50 |
| xit | x Write the file if it was changed since the last write, then quit. |
| yank | [address] ya [char] [count] Place lines specified by address in named buffer char. If no char is given, place lines in general buffer. count specifies the number of lines to yank, starting with address. Example:101,200 ya a |
| z | [address] z [type] [count] Print a window of text, with the line specified by address at the top. count specifies the number of lines to be displayed. Type
|
| ! | [address] !command Execute Linux command in a shell. If address is specified, apply the lines contained in address as standard input to command, and replace the lines with the output. Examples:!ls List files in the current directory :11,20!sort -f Sort lines 11-20 of current file |
| = | [address] = Print the line number of the next line matching address. If no address is given, print the number of the last line. |
| < > | [address]<[count] [address]>[count] Shift lines specified by address either left (<) or right (>). Only blanks and tabs are removed in a left shift. count specifies the number of lines to shift, starting with address. |
| address | address Print the line specified in address. |
| Return | Return Print the next line in the file. |
| & | & [options] [count] Repeat the previous substitution (s) command. count specifies the number of lines on which to substitute, starting with address. Examples:s/Overdue/Paid/ Substitute once on current line :g/Status/& Redo substitution on all "Status" lines |
| ~ | [address] ~ [count]
Replace the previous regular expression with the previous replacement pattern from a substitute (s) command. |
| ^D | ^D Scroll through the file. |
| ^Z | ^Z Suspend the editing session. Use fg to resume session. |
This section describes the following:
The :set command lets you specify options that change characteristics of your editing environment. Options may be put in the ~/.exrc file or set during a vi session.
The colon should not be typed if the command is put in ~/.exrc.
| Command | Action |
|---|---|
| :set x | Enable option x. |
| :set nox | Disable option x. |
| :set x=val | Give value to option x. |
| :set | Show changed options. |
| :set all | Show all options. |
| :set x? | Show value of option x. |
The following table describes the options to :set. The first column includes the optional abbreviation, if there is one, and uses an equals sign to show that the option takes a value. The second column gives the default, and the third column describes the behavior of the enabled option.
| Option | Default | Description |
|---|---|---|
| autoindent (ai) |
noai | In insert mode, indent each line to the same level as the line above or below. Use with shiftwidth option. |
| autoprint (ap) |
ap | Display changes after each editor command. (For global replacement, display last replacement.) |
| autowrite (aw) |
noaw | Automatically write (save) file if changed, before opening another file with :n or before giving Linux command with :!. |
| beautify (bf) |
nobf | Ignore all control characters during input (except tab, newline, or formfeed). |
| directory= (dir) |
/tmp | Name the directory in which ex stores buffer files. (Directory must be writable.) |
| edcompatible | noed-compatible | Use ed-like features on substitute commands. |
| errorbells (eb) |
errorbells | Sound bell when an error occurs. |
| exrc (ex) |
noexrc | Allow the execution of ~/.exrc files that reside outside the user's home directory. |
| hardtabs= (ht) |
8 | Define boundaries for terminal hardware tabs. |
| ignorecase (ic) |
noic | Disregard case during a search. |
| lisp | nolisp | Insert indents in appropriate LISP format. (), { }, [[, and ]] are modified to have meaning for LISP. |
| list | nolist | Print tabs as ^I; mark ends of lines with $. (Use list to tell if end character is a tab or a space.) |
| magic | magic | Wildcard characters . (dot), * (asterisk), and [ ] (brackets) have special meaning in patterns. |
| mesg | mesg | Permit system messages to display on terminal while editing in vi. |
| number (nu) |
nonu | Display line numbers on left of screen during editing session. |
| redraw (re) |
noredraw | Terminal redraws screen whenever edits are made (in other words, insert mode pushes over existing characters, and deleted lines immediately close up). Default depends on line speed and terminal type. noredraw is useful at slow speeds on a dumb terminal: deleted lines show up as @, and inserted text appears to overwrite existing text until you press Esc. |
| remap | remap | Allow nested map sequences. |
| report= | 5 | Display a message on the prompt line whenever you make an edit that affects at least a certain number of lines. For example, 6dd reports the message "6 lines deleted." |
| scroll= | <1/2 window> | Amount of screen to scroll. |
| sections= (sect) |
SHNHH HU | Define section delimiters for [[ ]] movement. The pairs of characters in the value are the names of nroff/troff macros that begin sections. |
| shell= (sh) |
/bin/sh | Pathname of shell used for shell escape (:!) and shell command (:sh). Default value is derived from SHELL variable. |
| shiftwidth= (sw) |
8 | Define number of spaces used by the indent commands (^T, ^D, >>, and <<). |
| showmatch (sm) |
nosm | In vi, when ) or } is entered, cursor moves briefly to matching ( or {. (If the match is not on the screen, rings the error message bell.) Very useful for programming. |
| showmode | noshowmode | In insert mode, displays a message on the prompt line indicating the type of insert you are making, such as "Open Mode" or "Append Mode." |
| slowopen (slow) |
Hold off display during insert. Default depends on line speed and terminal type. | |
| tabstop= (ts) |
8 | Define number of spaces that a tab indents during editing session. (Printer still uses system tab of 8.) |
| taglength= (tl) |
0 | Define number of characters that are significant for tags. Default (0) means that all characters are significant. |
| tags= | tags /usr/lib/tags | Define pathname of files containing tags (see the ctags command in Chapter 3, "Linux Commands"). By default, the system looks for files tags (in the current directory) and /usr/lib/tags. |
| term= | Set terminal type. | |
| terse | noterse | Display shorter error messages. |
| timeout (to) |
timeout | Keyboard maps timeout after 1second. |
| ttytype= | Set terminal type. Default is inherited from TERM environment variable. | |
| warn | warn | Display the message, "No write since last change." |
| window= (w) |
Show a certain number of lines of the file on the screen. Default depends on line speed and terminal type. | |
| wrapmargin= (wm) |
0 | Define right margin. If greater than 0, automatically insert carriage returns to break lines. |
| wrapscan (ws) |
ws | Searches wrap around either end of file. |
| writeany (wa) |
nowa | Allow saving to any file. |
The following lines of code are an example of a customized .exrc file:
set nowrapscan wrapmargin=7 set sections=SeAhBhChDh nomesg map q :w^M:n^M map v dwElp ab ORA O'Reilly & Associates, Inc.
|
|
|