Wednesday, March 26, 2008

vi Examples

s/pat/text/ substitute 1st match of pat with text

s/pat/text/g substitute every match of pat with text

s/pat/text/n substitute the nth occurrence of pat with text

:%!sort or :1,$!sort sort current file

:5,10s/foo/bar/2 change the second occurrence of foo with bar on lines 5-10

:map g 1G map g to really run 1G

3J Join next 2 lines to current one

3,9m$ move lines 3 through 9 to the end of the file

ab w/o without when w/o is typed change to without

:?foo?,/bar/d delete from the reverse match of foo until the next match of bar

:g/{/,/}/< shift all lines between, and including, a "{" and a "}" left

:$-4,$d delete last five lines of buffer

:%s/^\(.*\) \(.*\)$/\2 \1/ swap everything before and after the first space

d'' delete from current position to line of last jump

Perl matches Perl

^Perl matches Perl at beginning of line

Perl$ matches Perl at end of line

^Perl$ matches Perl as the only word on line

^$ matches the empty line

^..*$ matches a line with at least one character

.* matches any string of characters including none

^[ ^I]*$ as above but line can also contain spaces and/or tabs(^I)

[pP]erl matches perl or Perl

[aA][nN] matches an, aN, An, AN

p[aeiou]g second letter is a vowel

i[^aeiou]g second letter is not a vowel

p.g second letter is anything

^....$ matches a line with exactly four characters

^\. matches any line beginning with a dot

^\.[0-9a-z] same with a lowercase letter or digit following

^[^\.] matches any line that does not begin with a dot

;$ matches a line ending with a semicolon

figs* matches fig, figs, figss, figsss etc.

[a-z][a-z]* matches one or more lowercase letters

[a-zA-Z] matches any character

[^0-9a-zA-Z] matches any symbol (not a letter or number)

\&ltthe matches the, theater, then

the\> matches the, breathe

\&ltthe/> matches the

:%s/\<./\u&/g turn the first letter of all words to uppercase

:%s/\<[a-z][!-~]*\>/\u&/g as above

:%s/\<[a-z]/\u&/g as above

:%s/.*/\L&/ turn entire file to lowercase

:%s/<[^>]*>//g remove strings from file that start with a less than sign and end with a greater than sign (html tags)

:1,$s/1$/0/g Replace 1 to 0 from each line of the file, if last character of the line is 1

:1,$s/^1/0/g Replace 1 to 0 from each line of the file, if first character of the line is 1

No comments: