Marking Text & EX Commands
The following commands are performed in command mode.
To enter command mode, press the Esc key.
How to mark a location in text.
- At the first line of text to you want to mark, type ma, where
a is the letter from a - z that will be the reference
to this spot.
- Move to the last line of text to you want to mark, type mb, where
b is the letter from a - z that will be the reference to this spot.
NOTE: a and b are used as examples, you can use
any letters you
like from a to z.
How to refer to the marked lines.
Moves the cursor to the first position of the line marked by the a.
'a
Moves to the position in the line where cursor was, when you marked the line.
`a
Cool EX commands that manipulate marked blocks of
text.
Frist mark the text you want, as explained above. I am using a and b as
examples.
To write the block of text to the file myfile:
:'a,'bw!myfile
To move the block of text to the end of the current file:
:'a,'b m $
To copy the block of text to the end of the current file:
:'a,'b co $
To delete the block of text:
:'a,'b d
Another way to reference a block of text is with line numbers.
There are several ways to find line numbers:
Tells you what line you are currently on.
:nu
Tells you what line you are currently on, as well as total lines and filename.
Ctrl G
Turns line numbering on. (They are just for editing use and will not be saved in you file.)
:set nu
Turns line numbering off.
:set nonu
How to use line numbers with EX commands
To delete from line 100 to 200:
:100,200 d
To move lines 100 to 200 to line 300:
:100,200 m 300
To copy lines 100 to 200 to line 300:
:100,200 co 300
To copy the current line and 4 lines below to a myfile:
:.,+4w!myfile
To copy the current line and 4 lines above to a myfile:
:-4,.w!myfile
To append lines 100 - 200 to the file myfile:
:100,200w! >>myfile
To delete from you current position to the beginning of the file:
:1,. d
To delete from your current line to end of file:
:.,$ d
How to import another document into your current document
To import myfile into the current file.
:r myfile
To leave the current vi session and escape to the shell prompt without exiting you vi session.
Note: Any of the available shells on your system could be substituted for sh.
:!sh
To return to the vi session from the shell prompt type.
exit