Notes on using VI

Information from http://ecn.www.ecn.purdue.edu/ECN/Documents/VI/
and http://www.devshed.com/Server_Side/Administration/Vi101/

See also: http://www.fprintf.net/vimCheatSheet.html
and http://www.math.fu-berlin.de/~guckes/vim/source/html.vim

and 
http://www.networkcomputing.com/unixworld/tutorial/009/009.html
	an advanced tutorial with hefty neat stuff

and http://www.tldp.org/HOWTO/Vim-HOWTO-12.html
 http://www.thomer.com/vi/vi.html
 http://people.msoe.edu/~taylor/4ltrwrd/index.html

Command line: vi or vi 

: is used to start many commands.
:w writes to the current file
:w  writes to a different file
:q quits
:quit! quits without saving

VI starts in command mode, giving these options, some with (shortcuts):
append (a, A)
insert (i, I)
open (o, O)
change (c, C)
replace (R)

These all go to text input mode, from which you return with Esc.

Other command mode options:
join (J) -- like pico's ^J command
delete
undo (u, U)
move cursor
yank (y, Y) and put (p, P)
replace (r)
named buffers

Moving around

Ctrl-D moves _downward 1/2 screen Ctrl-U moves _upward 1/2 screen Ctrl-B moves _backward 1 screen Ctrl-F moves _forward 1 screen note that all of these can be preceded with a number of times to do it. Goto: G (capitalized) or G . So G or 250G . 250G moves to the beginning of line 250 if it exists, otherwise nothing. G moves to the end of the file. Ctrl-g (lowercase) prints status info on the file and your position. Search: / -- like the first part of a Perl regexp. Search down. ? -- same, but searches upward. n finds the _next occurrence N finds the _Next occurrence in the other direction Note that these do allow regexps. Positioning on screen: H - moves to the _Highest line on screen M - moves to the _Middle line on screen L - moves to the _Lowest line on screen 3H moves to the 3rd line from the highest 4L moves to the 4th line from the lowest Arrow keys work for character movement, as do h - left j - down k - up l - right return moves down a line and goes to the beginning Movement scope 0 moves to the beginning of the line $ moves to the end of line w, W move to the beginning of the next word on the right b, B move to the beginning of the next word on the left e, E move to the end of the word on the right or the current word all these can be preceded with numbers of times to do it the lower-case variations include punctuation as words; upper-case do not.

Editing text

If you forget which mode you're in, press Esc. If command, terminal beep. If input, move to command. append mode comes in two forms: a and A. a moves one character to the right and enters input mode A moves to the end of the line and enters input mode note that the screen isn't necessarily redrawn in append mode as you type insert mode comes in two forms: i and I. i inserts text to the left of the cursor. I moves to the line beginning and inserts text. open mode comes in two forms: o and O. o opens a blank line below the current line O opens a blank line above the current line :r reads another file into the current buffer directly below the current line. deleting text is in two forms: D as a command, and d as an operator D removes the text on the current line from the cursor to the end of line dw deletes a word (forward) db deletes a word _backward d$ deletes from cursor to eol (like D) d0 deletes from cursor to beginning of line dL deletes from current line to end of screen ("delete Lines") dG deletes from current line to end of file ("delete Global") d) deletes complete sentence forward d( deletes complete sentence backward dd deletes a complete line ("delete delete") Note that all these can be preceded with numbers, as they are operator-scope pairs. When deleting, an @ symbol replaces deleted lines to save processor redraws. Ctrl-R or Ctrl-L redraws the screen. Change text mode comes in two forms: C (command) and c (operator) cw changes a word (forward) cb changes a word _backward c$ changes from cursor to eol (same as C) c0 changes from cursor to beginning of line cL changes from current line to end of screen ("change Lines") cG changes from current line to end of file ("change Global") c) changes from cursor to sentence start c( changes from cursor to sentence end cc changes complete lines These basically act as overwrite filters to keep you from changing more than you want to change. Replace comes in two forms: r and R r replaces a single character you key in, staying in command mode R replaces characters as you key them, going in text input mode Note that R only replaces until the end of the current line, but continues inputting after that, so you can add any number of new lines Erase ("gobble") comes in two forms: x and X X erases the character to the left of the cursor x erases the character the cursor is sitting on. Be very careful of using erase to get rid of lots of text, as the redraws don't happen as frequently as the erase commands. xp transposes pairs of characters Undo comes in two flavors: u and U u undoes the most recent command U undoes all the changes to the current line

Cutting and Pasting

Yanking (copying) text comes in two forms: y (operator) and Y (command) Y copies a whole line y behaves like erase and change and move Putting (pasting) has two forms: p and P p pastes after the cursor P pastes before it Thus xp is really just erase a character and then paste it to the right. Cut and paste is done via delete and then paste. Note that if you delete more after the original deletion, the old buffer does not get restored.

Named buffers:

26 buffers [a-z] can be used to hold temporary text access them by "acommand lowercase name overwrites the buffer; uppercase appends "a7dd delete seven lines and overwrite them to buffer a "G2yy yank two lines and append them to buffer g Beware of the fact that there isn't an easy way to access the contents of a named buffer without going and just pasting and undoing their contents. paste from a named buffer like so "gp or "gP - paste g below or above the current line the Ex editor Line editor rather than screen editor. Use commands by prefacing them with : in command mode i.e. :wq get into Ex by typing :Q and then typing vi at the ex prompt when you want to come back.

Repeating commands

Repeat them by typing . in command mode. Do search and replace with, for example, /HAE cw Hollis Easter n . n . etc.. To use a shell program: :w -- write your buffer, for good measure. :! -- get to a shell prompt Open multiple files with :e for Edit :e!\ newfile -- tosses buffer, doesn't update, gets newfile :e\ #

Marks (with cutting and pasting)

More useful cutting and pasting ma -- _mark this point with name a move to a new place further on y'a -- yank from mark a to here -- can be used with other operators, too 'a -- move to mark a and put the cursor there. `a does the same thing.

Other interesting discoveries

Page Up and Page Down capitalize and lowercase the character the cursor is sitting on. @ buffer -- executes a named buffer. [count] ~ motion -- reverses case on the next count characters. ^ -- moves to the first non-blank character of the current line. Ctrl-X[0-9A-Fa-f]+ -- inserts a hexadecimal character into the text. [count] > motion -- shifts lines right -- motion affects which direction [count] < motion -- shifts lines left -- the count lines are grabbed from [count] | -- moves to the specified column position. Ooh! Big one! Infinite undo. Hit u for undo, then . for extra undos. Autocommands: Similar to mutt's folder hooks. Creates a series of commands that are executed whenever a given event occurs, be it file creation or whatever. :autocmd :au -- create a new autocommand :au FileReadPost *.txt set ruler -- turn on the ruler when reading text :autocmd -- display all autocmds in memory :autocmd! -- delete all autocmds in memory For substitution using regexps, :1,$s/find/replace/g finds find and replaces it with replace every time it occurs between the first and last lines. Ctags: First, create a tags file: $ ctags *.c This shell command creates a tags file in your working dir. Then, in vi, over a fn call, type ^] to view the fn definition, and ^T to go back. Crash recovery: vi -r -- recover from .swp file. Delete that file afterwards! Vimrc stuff: You can add all sorts of things to .vimrc to be run before opening vim. The rule of thumb: if the command starts with a colon, omit said colon in the vimrc file. :mkvimrc -- generate a .vimrc file with the editor's current settings Fun with Substitution! :substitute /thing/bar/ substitute first match thing w/bar :substitute /thing/bar/g substitute all thing with bar :%substitute /thing/bar/g substite all thing on each line with bar :%substitute /thing/bar/c _confirm changes In vim, you can edit multiple files, either at the command line or inside the program. :next -- gives you the next file in the queue :rewind -- goes to the first file in the filelist :last -- goes to the last file in the filelist :qa! -- quit all without saving :wqa -- quit all with saving :e filename -- open filename for editing :split -- split the windows so you can view multiple parts of a file :new filename -- open windows with different files. ^WW -- switch between windows v -- goes into visual mode, which lets you do block selections V -- goes into visual line mode, selecting lines ^V -- goes into visual block mode, selecting columns note that in visual modes, all the usual things apply: cut, paste, indent (angle brackets), ~, etc. o -- moves b/w upper left and lower right corners of selection gv -- repeats previous selection :abbreviate -- gives you text abbrevs :ab -- does the same :unabbreviate -- removes it :unab :abclear -- clear all abbreviations ^K -- put in digraphs like copyright ^V -- same :digraphs -- show the digraph tables :map -- generate key map in normal and visual mode :map! -- same, for command and insert modes :map -- show the current mappings :map! -- same :unmap -- remove a mapping :unmap! -- same qa -- begin recording a macro into register a q -- finish recording the macro. These are in command mode only. @a -- play back the macro in register a. This can also be 76@a, etc. !!bash -- interpret the word under the cursor as a bash command and replace the word with bash's output of the command :! -- run a shell command and display its output :shell -- spawn a new shell ^D -- _destroy, _delete, or _ditch the new shell. = -- indent code as best vim can :syntax [on|off] -- syntax highlighting

Various :set options

list -- displays lines with characters for eol, etc. number -- displays line numbers. readonly -- marks the session as readonly ruler -- displays row and column data on the status line shiftwidth, sw -- set the autoindent and shift column width showmode -- display current editor mode and a * modified flag tabstop, ts -- sets tab stops for the editor display term -- sets the terminal type verbose -- displays error msgs for every error comment -- skip leading shell, C, and C++ comments leftright -- scroll long lines left/right rather than wrapping nodg -- VIM, turns off diacritical glomming autoindent -- automatic indentation cindent -- C-style indentation showmatch -- parenthesis matching list -- show all characters in the file, including specials