5.1 ex commands involving patterns
3. ex commands:
Note that a pattern is the same as a regular expression.
Items in [ ] imply they can be specified optionally.
Substituting (s):
(Within the scope of the start and end addresses and
in between, substitute the pattern found and replace with
the newpattern. Do this everywhere within the scope of
the search and confirm each change before making it.)
:{startaddress},{endaddress}s/pattern/newpattern/gc
Importing files into the edit session (r):
:r filename
Importing the unix command output into the edit session (r):
:r !unixcommand
Exporting part or all of a file by saving as a(nother) name (w):
:[{startaddress},{endaddress}]w[!] [>>] newfile
Global search for lines with patterns and display (g):
:g/pattern/p
Edit a new file from within a vi session
:e file
Display the vi arguments buffer for the files under edit
:args
Rewind the vi arguments buffer to point to the 1st one
:rew
Switch the context to the next file to be under edit
:n
Switch the context to the next file to be under edit
:set [all or list or number or showmode or ...]
Define a macro (command or input) for a key to trigger:
:map[!] lefthandside righthandside
:unm[ap][!] lefthandside
Define an abbreviation for a word or phrase:
:ab abbreviation realthing
:una abbreviation
Examples:
:1,10 d "Delete lines 1 through 10 inclusive"
:/the/,/end/ p "Print the lines starting with the one with 'the' in
it and continuing through the one with 'end' in it.
:/the/,$s/^\([A-Z]\)/ \1/gc "Within the scope of the line starting
with 'the' through the last line, substitute
where ever an uppercase letter is the
first letter, five spaces to precede it.
Do this everywhere within the scope and
confirm each change"
:r .profile "Import the .profile file's contents under the cursor
location"
:r !date "Import the result of the unix date command under the
cursor location"
:13,16w newfile "Output newfile to contain lines 13 through line 16
:.,.+6 w newfile "Output newfile to contain current line through next 6 lines.
:.-1,$ w newfile "Output newfile to contain previous line through the last line.
:map V dwelp # map the V key to interchange 2 adjacent words
:map q :%s/^/ > /g^V^M^M # map the q key to insert a " > " before all
lines in the file
:ab teh the # change all instances of teh to the (a misspelling)
:ab pcmcia "people can't memorize computer industry acronyms"
Questions? Robert Katz: arkay@arkaye.org
Last Update July 5, 2015