Sie sind auf Seite 1von 5

The vi Editor

A short reference to commonly used commands.

Environment
Specify the following in your bash .profile file: export EDITOR="vi" export VISUAL="vi" set o vi The EDITOR and VISUAL environment variables are often read by applications to determine which editor to invoke. The set o vi command will result in vi-type behavior at the command line. To enter vi-mode, press ESC. You can use vi commands to go back in history, edit a command, and re-execute. Consider the following commands in your .exrc file: set set set set set set set set set noerrorbells showmatch matchtime=8 tabstop=8 shiftwidth=4 nowrapscan ignorecase autoindent cindent

Set noerrorbells so ringing bells won't make you embarrassed. If showmatch is enabled, vi will match parenthesis and braces while you type. Matchtime specifies the match time in tenths of a second (default=5). For this class, use 8-byte tabs, and indent by 4 bytes. The tabstop and shiftwidth settings automate this setting. Specify nowrapscan to stop searches from wrapping to the beginning. Enable ignorecase and search/replace will ignore the case when searching for pattern matches. You can temporarily override this behavior by specifying set noic, and re-enable with set ic. Set autoindent and vi will indent to the same level as the previous line. Specify cindent and vi will automatically indent C programs. This includes while and if statements. Parameter autoindent is supported by all versions of vi, while cindent is only supported by vim (vi improved).

General
Many commands allow a repetition factor. For example, j moves the cursor down one line, while 5j moves the cursor down 5 lines. You can place up to 26 markers (a-z) in the document. For example, ma marks the current position with mark a. You can specify a range for many commands: :w file :1,$w file :3,5w file :'a,.w file write current document to file write current document to file write lines 3 to 5 to file write lines from mark a to current line to file

Note that "$" represents the last line, and "." represents the current line.

Cursor Movement
h l k j b w 0 $ ctrl-u ctrl-d ctrl-f ctrl-b 5G G ctrl-g ma 'a '' % left one character right one character up one line down one line left one word right one word beginning of a line (zero) end of line scroll up 1/2 screen scroll down 1/2 screen scroll forward one screen scroll backward one screen go to line 5 (1G goes to beginning of file) go to end of file display line number mark current position and call it a. Legal marks are a-z. return to mark a return to previous context find matching ( or [

Changing Text
i I a A O o r R x X dw dd D J >> >'a << <'a Tab Ctrl-d
1

insert before cursor1 insert at beginning of line1 append after cursor1 append at end of line1 open above. Insert a new line above the current line1 open below. Insert a new line below the current line1 replace character replace1 delete character delete character backward delete word delete line delete from current position to end of line join current line with next line shift right shift right all lines in range shift left shift left all lines in range shift right (insert mode) shift left (insert mode)

Press ESC to exit insert mode.

Block Operations
Block operations, that work on a block of text, consist of a command followed by a range specifier. Command c y d > <

change (cc = change line) yank, yy = yank line delete, dd = delete line, D = delete remainder of line (d$) shift right, >> = shift line right shift left, << = shift line left

Range Specifier w next word $ end of line G end of file 'a line marked "a" Examples dw d$ dG d'a y'a >'a

delete next word delete characters from current position to end of line delete lines from current line to end of file delete lines from 'a to current line yank lines from 'a to current line shift lines from 'a to current line

Copy/Cut/Paste
yy y'a dd d'a p P copy or yank current line and place in buffer copy or yank lines from 'a to current line and place in buffer cut or delete current line and place in buffer cut or delete lines from 'a to current line and place in buffer paste buffer after current line paste buffer before current line

Searching
/pat/ ?pat? n N search foward for "pat", the last "/" may be omitted search backward for "pat", the last "?" may be omitted repeat search repeat search in opposite direction

Replace
:%s/old/new/ :%s/old/new/g :%s/old/new/cg :3,10s/old/new/cg :'a,.s/old/new/cg :'a,.s%old%new%cg in all lines, substitute first occurrence of "old" with "new" in all lines, substitute all occurrences of "old" with new" substitute with confirmation substitute from line 3 to 10 substitute from line with mark "a" to current line any non-alphabetic character can be used as a delimiter

Regular Expressions
Regular expressions may be used in both search and substitute commands. Glob characters: ^ beginning of line $ end of line . single character * 0 or more characters [] a class, specifying one character within square brackets: ^ if first character, all characters NOT in the class are found - if not first or last character, a range is implied \ escape to consider character, not command: (^ $ * . \ [ ~) Examples: /^proc /\^proc /ab.d /a*d /[fb]oo /[0-9] /[0-9][0-9] /[^0-9] /[0-]

search for 'proc' at beginning of line search for '^proc' search for 'abad', 'abbd', 'abcd', 'abdd', ... search for 'ad', 'a.d', 'a..d', ... search for 'foo' or 'boo' search for numbers 0..9 search for 2-digit numbers search for non-numeric characters search for 0 or -

File Commands
:e file :e! file :e! :r foo :f file :q :q! :w :w file :w! file :w>> file :wq :'a,.w file :'a,.w! file :'a,.w>> file :e# :n :N :ar :rew edit file edit file, forcing a re-read from disk edit file, forcing a re-read from disk read file foo and insert after current line rename file referenced by buffer quit force quit write buffer into current file write buffer into new file write buffer into new file, overwriting file if exists append buffer into new file write buffer to file and quit write block to file write block to file, overwriting file if it exists append block to file edit previously edited file edit next file edit previous file lists all files being edited rewind to first file

Set Commands
:set :set :set :set :set ic noic ts=8 sw=4 display set parameters ignore case don't ignore case set tabsize to 8 set shift width to 4

Shell Commands
:sh :!ls fork to a subshell, exit to resume editing execute shell ls command, enter to resume editing

Last But Not Least


u undo the last change (history = 1)

Das könnte Ihnen auch gefallen