|
CS 3723
Programming Languages |
vi Editor
|
The vi editor: This is an old-fashioned text-file editor in the Unix world.
It uses the full screen (or full window), but it does not use a mouse.
It is useful because it should always be available and should always work.
vi is enormously complicated -- whole books cover just this editor.
It is notoriously confusing for beginners, but you can get by very well with
just the 18 commands given in the next section.
For common editing tasks,
a mouse-based editor is often more appropriate. Very intricate editing
might better be done with a shell script or with a scripting language such as
Python or Ruby or even (ugh!) Perl.
The vi editor has been superseded by vim,
which is mostly downward
compatible with vi.
There's a lot on-line about these editors.
Unix commands below are in
bold green.
The basics:
Basics of vi:
18 commands, most of what you need |
To enter the vi editor, type
vi
on the command
line followed by a file name to create or open:
Produces a clear screen if it's a new file,
with ~ running down the left side.
You can get by with a very short list of commands (18 of them):
i,
I: -insert or insert at start of line,
and enter input mode
a,
A: -append or append at end of line,
and enter input mode
Esc: -leave input mode, enter command mode
x,
dd: -delete a char, delete a line (in command mode only)
h,
l,
j,
k: -move left, right, up, down (in command mode only)
ZZ: -save changes, exit vi (in command mode only)
:w,
:w filename: -write, write to "filename", no exit (in command mode)
:q,
:q!: -quit and don't save, quit and don't save even if changes (in command mode)
/sometext: -search for "sometext" (in command mode)
:23: -go to line 23 (in command mode)
If in doubt, press "Esc".
Then type ":q!" (to exit vi without
saving changes), and start over.
|
Remember, in input mode (after an
i,
I,
a, or
A command),
all key strokes except Esc become a
character in the file.
Carrying on:
Below are useful commands to make editing more convenient.
Try to relate these to the word processing techniques you use in the
gui world with a mouse.
"Command mode"
- cursor movements:
- To replace the arrow keys
h -left one character
l -right one character
j -down one line
k -up one line
- In windows you can use ctrl and the arrows to
move words by words, here it's
w -right one word
b -back one word
- Home key and End key are
$ -to the end of line
0 -to the beginning of the line
- Other useful keys
) -right one sentence
( -left one sentence
} -right one paragraph
{ -left one paragraph
- Page down and page up
<Ctrl-F> -forward one page
<Ctrl-B> -back one page
- deleting (or cutting to the clipboard):
d -
delete (then add one of the cursor movement symbols toshow
what should be deleted)
d$ -delete
to end of line
d0 -delete
to the beginning of the line
dw
-delete the current or next word
dd
-delete delete (delete the whole line)
x -delete
character cursor is on
- other basic commands:
r -replace
one character
ZZ -save
and exit (hold down shift and press "z" twice)
u -undo
last editing command (only once)
/sometext -search for "sometext", a very useful command
- Copy
y -yank (otherwise known as copy)
yy - copy the current line
yw -copy the current word and the 4 to the right
y0 -copy to the beginning of the line
Y -yank line cursor is on
- Paste
p -paste
below the current line if buffer contains a newline character, otherwise,
paste to left of current character
P -paste
above the current line if buffer contains a newline character, otherwise,
paste to right of current character
- any command can take numeric argument before the name of "object", i.e.
5dd -delete
5 lines beginning with cursor line (or)
d5d -
same as above
2dw -delete
two words to right
d2b - delete two words
to left
c3w> -change
3 words
3<Ctrl-B>
-move up three pages
- command line (sometimes called "ex mode"):
- in ex mode "set" command can be executed to
customize editing environment, i.e.:
:set all
-will show the state of all options
:set number
-will show on the screen numbers of all lines
:set autoindent
-holds previous line's indention
- in ex mode any ex command can be performed on
the range of lines, i.e.:
:18,24 del -delete
from line 18 to line 24
:23,48 copy 17
-block from line 23 to 48 copy to line 17
:2,17 move 92
-block from line 2 to 17 move to line 92
- file commands in ex mode:
:r somefile
-read in "somefile"
:x -save
and exit (if file is "Read Only", this command will
exit without saving)
:wq -write and quit (same
as above)
:w -write
(save)
:w somefile -save this
file as "somefile"
:q -quit
without saving
:q! -quit without saving
if changes were made
:23
-go to line 23, very useful when debugging
"Input Mode" - editing your code...
- text input commands (all require "Esc" to terminate):
i -insert
text before the character cursor is on
I -insert
text at the beginning of the line
a -append
(insert text after the character cursor is on)
A -append
text to the end of the line
c -change
(replace previous text with new one) example:
c3w -change
3 words to the right of cursor
R -start
overwriting text
o -start
entering text at the beginning of the new line below the cursor
O -start
entering text at the beginning of the new line above
the cursor
- if in doubt, press "Esc"
Regular Expressions and Substitution:
Regular expressions are not mentioned above, but they can be used
within vi with great effectiveness. especially in combination with the
s
(substitution) command, which is also not
mentioned above. However, it probably makes
more sense now to use a scripting language that supports
regular expressions.
( Revision date: 2014-05-21.
Please use ISO
8601, the International Standard.)
|