TN101 - vi Editor
101.1 Summary
vi is an important editor to know in the UNIX environment because it is
always available. The following notes are reminders. For detailed
information please see the following books:
101.2 Other Resources
101.3 vi Quick Reference
Editor Commands
| [esc] | Return to Command mode |
| . | Repeat Previous Command |
| [ctrl]L | Redraw screen |
| :q! | Quit and abandon changes |
| ZZ | Quit and save changes |
| :w | Save buffer without quitting vi |
| :n | edit next file |
| :wfn | Write buffer to a new file |
| :w>>fn | Append buffer to a file |
| :n,nwfn | Write lines n to n to a new file |
| :rfn | Read filename and insert |
| vi -r | Recover file after a system crash |
|
Insert/Change Commands
| i | Before cursor |
| I | Before first nonblank char on line |
| a | After cursor |
| A | At end of line |
| o | Open a line next line down |
| O | Open a line next line up |
| rx | Replace single character with x |
| R | Replace characters |
| cn | Change characters |
| s | Delete char and continue typing |
| Sn | Delete line and continue typing |
|
Cursor movement commands
| h | Left one character |
| j | Down to same position in line below |
| k | Up to same position in line above |
| l | Right one character |
| w | Forward to first letter of next word |
| W | Same as w, but only whitespace is separator |
| b | Back to first letter of previous word |
| B | Same as b, but only whitespace is separator |
| fx | Move forward to character x in line, stop on char (repeat ;) |
| Fx | Move backward to character x in line, stop on char (repeat ;) |
|
| [space] | Right one character position |
| 0 | Beginning of current line |
| $ | End of current line |
| ^ | First non-blank char of line |
| [return] | Forward to beginning of next line |
| + | First character of next line |
| - | First character of previous line |
| ( | Back to beginning of current sentence |
| ) | Ahead to beginning of next sentence |
| { | Back to beginning of current ¶ |
| } | Ahead to beginning of next ¶ |
| tx | Move forward to character x in line, stop after char (repeat ;) |
| Tx | Move backward to character x in line, stop after char (repeat ;) |
|
File Position Commands
| H | Left end of top line on screen |
| nH | n lines from top |
| M | Left end of middle line on screen |
| L | Left end of lowest line on screen |
| nL | n lines from bottom |
| G | Last line in work buffer |
| nG | Move to line number n |
| [ctrl]D | Down half screen |
| [ctrl]U | Up half screen |
| [ctrl]F | Down almost a full screen |
|
| [ctrl]B | Up almost a full screen |
| [ctrl]E | Scroll down one line at a time |
| [ctrl]Y | Scroll up one line at a time |
| z Rtn | Move current line to top of screen |
| z | Move current line to center of screen |
| z- | Move current line to bottom of screen |
| /pat | Find pat (n repeats N reverses) |
| ?pat | Previous line matching pat |
|
Delete Commands
| x | Character at cursor |
| X | Character before cursor |
| dd | Delete current line |
| ndd | Delete n lines |
| dw | Delete to end of word |
| ndw | Delete n words |
| db | Delete to beginning of word |
| ndb | Delete n words |
| d0 | Delete to end of line |
| d$ | Delete to beginning of line |
| d) | Delete to end of sentence |
| d( | Delete to beginning of sentence |
| d} | Delete to end of paragraph |
| d{ | Delete to beginning of paragraph |
| dG | Delete to end of file |
| d1G | Delete to beginning of file |
|
Buffers and Marks
| Y | Yank to buffer |
| yy | Yank to buffer |
| mx | Mark position with letter x |
| `x | Goto mark x |
| 'x | Goto first of line with mark x |
| Y'x | Yank from current line to mark x |
| "xY | Yank to buffer x |
| P | Put from buffer |
| pp | Put from buffer |
| "xP | Put from buffer x |
| d'x | Delete to first of line with mark x |
|
Misc
| . | Repeat command |
| J | Join two lines |
| u | Undo a command |
| U | Undo an undo command |
| n>> | Shift n lines to right |
| n<< | Shift n lines to left |
| >'x | Shift current line to mark x to right |
| <'x | Shift current line to mark x to right |
| :l | Show control characters on line |
| :ve | Show vi version/td> |
|
Set Behavior
| :set ai | Turn on autoindent |
| :set noai | Turn off autoindent |
| :set nu | Turn on line numbers |
| :set nonu | Turn off line numbers |
| :set sw=n | Set shiftwidth = to n |
| :set | show settings |
|
101.4 ex Quick Reference
The vi editor is actually an interactive version of ex, and all of the ex
commands are available inside vi. Some ex commands helpful to apply global
changes or write macros.
| form of commands: |
line_address command parameters |
Line Address
| . | current line in file |
| 1 | first line in file |
| $ | last line in file |
| n | nth line in file |
| n,n | range of lines n to n |
| .-n | nth line before current line |
| .+n | nth line after current line |
| % | all lines in file (1,$) |
| 'x | line with marker x |
| .,'x | current line through line with marker x |
| /pat/ | first line in forward search matching pattern |
| ?pat? | first line in backward search matching pattern |
| .,/pat/ | current line through first line in forward search matching pattern |
|
Basic Commands
| . | Repeat Previous Command |
| as/pat/pat/ | substitute second pattern for first on lines addressed by a |
| %s/pat/pat/ | substitute second pattern for first in whole doc |
| %s/pat/pat/c | substitute with confirm |
| %s/pat/pat/g | substitute multiple times per line |
| g/pat/p | Print all lines with pattern on display |
| g/pat/d | Delete all lines with pattern |
| g!/pat/d | Delete all lines without pattern |
| g/pat/y x | Yank all lines with pattern to buffer x |
|
Executables
| %!exe | pipe all lines through executable exe |
| n,n!exe | pipe lines n-n through executable exe |
| %!expand | expand tabs to spaces |
| %!expand -a | convert spaces to tabs |
| r !date | Read output of date command line |
|
Misc
| :ab cc xxx | abbreviate cc to be expanded to xxx |
| :uab cc | turn abbreviation off for cc |
| :map | show mapped keys |
| :map c xxx | map char c to xxx |
| :unmap c | unmap char c |
| :map! c xxx | map char c to xxx (in insert mode) |
| :unmap! c | unmap char (in insert mode) |
| @x | exec named buffer x in a shell |
| :list | show control character on current line |
|
101.5 Sample .exrc
A file named .exrc located in the user's home directory will be executed
when vi starts. This example creates the macros described below:
|   |
map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
map % :?^$?+1,/^$/-1s/^#//^M/^$^M/.^M
map * :?^$?+1,/^$/-1!hang^M/^$^M/.^M
map @ :r !date^MkJ
map [ :?^$?+1,/^$/-1!hang -w^M/^$^M/.^M
map ] :?^$?+1,/^$/-1!hang -wn^M/^$^M/.^M
map { :?^$?+1,/^$/-1!hang -bw^M/^$^M/.^M
map } :?^$?+1,/^$/-1!hang -bwn^M/^$^M/.^M
|
| # | will prepend a # to all lines in current group and advance to cursor to next group |
| % | will remove a leading # from all lines in current group and advance to cursor to next group |
| * | will align all lines in current group with the first line and advance cursor to next group |
| @ | appends date to end of current line |
| [ | will wrap all lines in current group, indent same as first line, and advance cursor to next group |
| ] | same as above except we reduce right margin by same as left |
| { | will wrap all lines in current group, indent same as first line, (except first word of first line which will remain outside to left of paragraph) and advance cursor to next group |
| } | same as above except we reduce right margin by same as left |
Notes:
- A group is defined any number of lines separated by blank line above and below.
- The hanging indent functions require my perl executable hang
- Do not try to cut and paste the macro file, it has control characters that will be lost.
Download .exrc here.
- you can't see the file correctly if you "cat" it. The embedded ^M will overwrite part of the line
- You must imbed a real CR not a ^ and M.
- The macros fail if used on the last group in a file and there is not an empty line after the group.
How the map # works:
map # :?^$?+1,/^$/-1s/^/#/^M/^$^M/.^M
| # | will prepend a # to all lines in current group and advance to cursor to next group |
| map | map command |
| # | key to map |
| : | : key to get to ex |
| ?^$?+1 | 1st address search backwards for empty line go forward 1 line |
| , | address separator |
| /^$/-1 | 2nd address search forward for empty line go backward 1 line |
| s | substitute command |
| /^/ | find beginning of line |
| /#/ | add # |
| ^M | need CR to finish line |
| /^$^M/ | find next empty line |
| .^M | move forward one line |
|