I got a lot of help from O'reilly's UNIX PowerTools book.
If you've never really had a chance to learn vi before
see the exercises and facts in
[ vi.html ]
I have created a list of vi command
[ List of Vi Commands ]
You are a very good touch typist and so can type
6j5wyw
faster than you can grab the mouse highlight the 6th word on
line 7 and copy it.
You like the feeling of power at your finger tips.
You are trying to get pico to work and so can't use pico to
do the work.
Your home UNIX system has 'vi', 'ex', and 'ed'... and no other editors.
The window system is broken and you can't use Jot/TextEdit...
You've just moved to a new UNIX and to get access to the
nice X-windows editor you have to edit the .Xdefaults and .Xinitrc files.
You are at home and the line noise is bad at high speeds
so using PPP and your local Xterminal is not possible. But
you do have an old kermit hanging around. You login and try
pico
and 'ksh: pico not found'. Then
emacs
and 'ksh: emacs not found'.
You have a choice of 'ex' or 'vi'.
While I was working on this file the modems were rebooted....
so I logged in thru another connection and ran
vi -r 30.vi.mth
and recovered my edited file.
You are on a terminal that doesn't have any arrow keys!
Your fingers still hit the wrong keys sometimes.
You forget which mode you are in.
The !%@#%@^!& arrow keys don't work properly.
There is no help built into vi and no easy way to
provide it. It is not for amateurs. It is for
professionals and masochists.
Their are two main religions in UNIX editing: vi
and EMACS. We won't get into the controversy. You need to be
skilled in at least one of them. I can teach you vi. EMACS
is in chapter 33. You have my permission to use it.... but you
are on your own.
We are stuck with vi. Bit in return it is powerful and designed
to minimize the hand movement needed to change a text
file. As a result it uses modes. This means that every key
has several meanings depending on the internal state vi. In
the command mode each key is a command or part of a command. I've
listed some of these under
[ Syntax ]
below. Some keys in command mode shift you into other modes. For
instance "i" gets you into insert mode, and you stay there until you
tap the <Esc> key (or CTRL/[ if you don't have an Esc Key!).
The other main mode is line mode. When you type ':' you enter
line mode and can type a single ex command. However a single
command in ex can globally scan the file for all lines containing
a given pattern and on these lines find all occurrences of a string
or pattern and change it, if you want it to!
Tapping 'Q' (Shifted Q) gets you into permanent line edit mode or
'ex'. In 'ex' you can get into visual mode with the command
vis
Don't forget 'vi' can edit many files at one time and you can
move data between them. Learn these:
:w Write this file
:n Next file
:e<file> Edit file
:e# Edit the other file
:e!# Edit the other file and forget my changes to this file
I do not :set autowrite..... This is something you decide for
yourself and do not change. It is when you turn it off and on
that you may make a mistake and (1) not save something that should
have been saved, or (2) save something you wanted to discard. (IMHO)
(clipboard): On the Mac you can cut, copy and paste items from
any file, to and from a system wide clipboard. In 'vi'
on UNIX you have 26 clipboards named "a" thru "z". They belong
to the running vi not the system however. They become empty
when you quit that vi. I use 'c' for clipboard:
cut "cd<something>
copy "cy<something>
paste "cp pastes after cursor
paste "cP pastes before cursor
See 30.05.
'vi' reads
.exrc
(at HOME or in the current working directory)
before starting up.... What is in your .exrc?
ex the extended editor that vi is an interface to
rc read commands
See 30.06...
Notice the powerful searches in 30.09 and 30.10. Very useful.
(1) programmers must change identifiers consistently... do it globally.
(2) administrators need to take data generated by a command - say
a list of hacking attempts - and summarize or clarify them...
30.15 has some interesting examples.... think about them! I have
got into the habit of using many of these almost automatically.
Notice the ways to save data from a file: 30.11,30.12,30.13.
I don't use the material in 30.18, 30.19, 30.20, 30.21: too dangerous.
30.22: Some incredibly powerful techniques here.... I do a lot of
!}br word wrap paragraph
!Gsort|uniq -c|sort -nr list and count words in file
!4!spell<enter>u spell check 4 lines
!'a stats work out statistics...
30.23 describes a bug that bit me yesterday on our FTP server which
runs a different version of Sun UNIX to Orion and the workstations.
30.24 and 30.25 will save you when everything else breaks down.
30.26: Shell escapes.... very handy and they work in vi,view, more,
less, lynx, mailx(but no colon), hack, ....
You can run a shell command in background in 'vi' as well:
:!generate_index_file >index.html &
Shell escapes are a handy "boss panic key"
30.27: A new one...
30.28: Tags - a neat idea for programmers and other authors.
30.31..30.33: Abbreviations - powerful but they have caused me problems. Use
carefully. Find out what you have already:
:ab<enter>
You may have more than you thought.
30.34: line or character?
30.35 and 30.36. Learn to use vi under emergency conditions.
I've edited a file in a window with garbage continuously scrolling
up the screen.
30.37: I don't like fmt and wrote my own br. You can
download or browse it from:
http://www.csci.csusb.edu/dick/tools
[ tools ]
It has the source code(in old and ANSI C),
design documentation, and manual page.
Sometimes you can type too fast for some 'vi's in Input mode and it seems
to lock up! Tap CTRL/C to fix this.
- c::=character.
- ctrl(k)::=tapping the k key with the Control shift key held down.
- dot::= .
- period::= .
- eoln::=tap the Return or Enter key
- eoln= End-of line.
- esc::=the Esc character, ASCII code #27.
- letter::="a".."z" | "A".."Z".
- n::=decimal_number.
- text::=any series of characters with no Esc in it unless prefixed by a ctrl V.
- SP::=space.
- BS::=Back_space.
- big_word::=sequence of non-white-space characters -- includes punctuation and math symbols.
- word::=either a sequence of letters and digits, or a sequence of non-whitespace and non alphanumeric symbols.
- vi_repeat_command_n_times::= n vi_command,
- vi_append_after::= a.
- vi_add_after_end_of_line::=A.
- vi_move_back_a_word::=b.
- vi_move_back_a_big_word::=B.
- vi_change_something::= c movement replacement_text esc
- vi_change_rest_of_line::= C replacement_text esc
- vi_change_one_line::= cc replacement_text esc.
- vi_change_n_lines::= c n c replacement_text esc.
- replacement_text::=text.
- vi_cut_command::= double_quote buffer_id vi_delete_something,
- vi_delete_something::= d movement.
For example: dw - delete word, d3l - delete next 3 chars,...
Also
[ vi_delete_character ]
- vi_delete_rest_of_line::=D.
- vi_delete_whole_line::= dd.
- vi_delete_n_lines::= d n d.
- vi_move_to_end_of_word::=e.
- vi_move_to_end_of_big_word::=E, includes puctuation but not whitespace.
- vi_move_to_find_char_in_line::= f c.
- vi_move_to_find_previous_char_in_line::= F c.
- vi_go_forward_a_screen::= ctrl/F,
- vi_move_forward::=ctrl/F,
- vi_go_to_line_n::= n G
- vi_move_to_line::= n G
- vi_go_to_last_line::= G,
- vi_move_to_last_line::= G,
- vi_give_me_information_on_this_file::=ctrl/G | :f eoln
- vi_move_back_a_space::= h | BS.
- vi_insert_text::= i inserted_text esc.
- inserted_text::=text.
- vi_insert_a_start_of_line::= I inserted_text esc.
- vi_move_down::=j
- vi_move_to_start_of_text_on_next_line::=eoln| + .
- vi_join_lines::=J.
- vi_move_up::=k.
- vi_move_up_to_start_of_text_on_previous_line::= -.
- vi_move_forward::= l | SP.
- vi_mark_this_point::= m letter.
- vi_move_to_next_string::=n.
- vi_move_to_next_string_backwards::=N.
- vi_open_line_below::=o inserted_text esc.
- vi_open_line_above::=Oinserted_text esc.
- vi_paste_last_delete_after::=p.
- vi_paste_last_delete_before::=P.
- vi_paste_buffer::=double_quote buffer_id (p | P).
- vi_paste_last_but_n_delete::=double_quote n (p|P).
- vi_quit_visual::=Q.
- vi_replace_a_char::= r c
- vi_replace_string::= R text esc.
- vi_substitutes::=s something
- vi_move_to_char::=t c.
- vi_move_to_char_backwards::=T c.
- vi_undo::=u.
- vi_Big_undo::=U.
- vi_undo_everything::= :e! eoln.
- vi_redo::= dot .
- vi_move_over_word::=w.
- vi_move_over_big_word::=W.
- vi_delete_char::=vi_cross_out|vi_cross_out_previous.
- vi_cross_out::=x.
- vi_cross_out_previous::=X
- yank::=copY.
- copY_into_buffer::= double_quote buffer_id vi_copy_command,
- buffer_id::= letter
- vi_copY_something::= y movement.
- vi_copY_line::=yy.
- vi_go_to_sleep::=ZZ
- vi_line_commands::= : ex_command.
- vi_quit_and_no_save::= q!
- vi_search_forward::= / vi_regular_expression,
- vi_search_backward::= ? vi_regular_expression,
[ 26.patterns.html ]
[ 26.patterns.html ]
- vi_shift_line_left::= <<,
- vi_shift_line_right::= >>,
- vi_shift_n_lines_left::= <n<,
- vi_shift_n_lines_right::= >n>,
- vi_shift_something_left::= <movement,
- vi_shift_something_right::= >movement,
- vi_shell_escape::= vi_pipe_thru | ex_shell_escape,
- vi_pipe_thru::= vi_pipe_line | vi_pipe_n_lines | vi_pipe_something,
- ex_shell_escape::= :! shell_command,
- vi_pipe_line::= !!,
- vi_pipe_n_lines::=!n!,
- vi_pipe_something::= ! movement.
- vi_go_to_column_n::= n vertical_bar,
- vi_go_to_first_column::=0.
- vi_go_to_first_nonwhitespace::= ^
- vi_go_to_last_char_in_line::= $.
- vi_move_to_column_n::= n vertical_bar,
- vi_move_to_end_of_paragraph::= }.
- vi_move_to_first_column::=0.
- vi_move_to_first_non_whitespace::= ^
- vi_move_to_last_column::=$.
- vi_move_to_marked_character::=`
- vi_move_to_marked_line::='
- vi_move_to_matching_parenthesis::=%.
- vi_move_to_start_of_paragraph::= {.
- vi_word_wrap_the_whole_file::= 1G!Gbr eoln.
- movement::= any number followed by any vi command with "move" in above list,
- movement::=n single_movement.
- single_movement::= h | j |k | l | + | - | Fc|fc|tc|Tc G | SP | BS | { | } |....
- c::=any character
- vi_command::=many including the following
- A:: vi_command, vi_add_after_end_of_line.
- a:: vi_command, vi_append_after.
- B:: vi_command, vi_move_back_a_big_word.
- b:: vi_command, vi_move_back_a_word.
- C:: vi_command, vi_change_rest_of_line.
- c:: vi_command, vi_change_something.
- D:: vi_command, vi_delete_rest_of_line.
- d:: vi_command, vi_delete_something.
- E:: vi_command, vi_move_to_end_of_big_word.
- e:: vi_command, vi_move_to_end_of_word.
- f:: vi_command, vi_move_to_find_char_in_line.
- F:: vi_command, vi_move_to_find_previous_char_in_line.
- G:: vi_command, vi_go_to_line_n.
- h:: vi_command, vi_move_back_a_space.
- I:: vi_command, vi_insert_a_start_of_line.
- i:: vi_command, vi_insert_text.
- J:: vi_command, vi_join_lines.
- j:: vi_command, vi_move_down.
- k:: vi_command, vi_move_up.
- l:: vi_command, vi_move_forward.
- m:: vi_command, vi_mark_this_point.
- n:: vi_command, vi_move_to_next_string.
- N:: vi_command, vi_move_to_next_string_backwards.
- O:: vi_command, vi_open_line_above.
- o:: vi_command, vi_open_line_below.
- p:: vi_command, vi_paste_last_delete_after.
- P:: vi_command, vi_paste_last_delete_before.
- Q:: vi_command, vi_quit_visual.
- r:: vi_command, vi_replace_a_char.
- R:: vi_command, vi_replace_string.
- s:: vi_command, vi_substitutes.
- t:: vi_command, vi_move_to_char.
- T:: vi_command, vi_move_to_char_backwards.
- U:: vi_command, vi_Big_undo.
- u:: vi_command, vi_undo.
- W:: vi_command, vi_move_over_big_word.
- w:: vi_command, vi_move_over_word.
- x:: vi_command, vi_cross_out.
- X:: vi_command, vi_cross_out_previous.
- y:: vi_command, vi_copY_something.
- ZZ:: vi_command, vi_go_to_sleep.
- ex_command::= address action,
- ex_actions::=following
- a::ex_command, append upto line with a single . on it
- d::ex_command, delete lines
- e::ex_command, edit file
- f::ex_command, which file name?
- i::ex_command, insert before the line
- j::ex_command, join lines
- l::ex_command, list (show tabs and end_of_lines)
- m::ex_command, move lines
- q::ex_command, quit
- r::ex_command, read file in
- s::ex_command, Substitute: s/pattern/replacement/flags
- u::ex_command, Undo
- vis::ex_command, Visual mode
- w::ex_command, Write file, see append_to_file, write_new_file, overwrite_file
For more commands see
[ 33.ed+ex.html ]
[ 33.ed+ex.html ]
- address::= range | line | global | all_lines,
- all_lines::=%.
- this_file::= %.
- range::= line,line,
- global::= g search, vsearch,
- line::=search | n | place ( + | - ) n | search (+|-) n,
- place::= here | end_of_file,
- here::= dot
- end_of_file::= $
- search::= forwards|backwards
- forwards::= / pattern /,
- backwards::= ? pattern ?,
- ex_substitute_command::=addresss/pattern/replacement/flags,
- pattern::=
[ 26.patterns.html ]
[ 26.patterns.html ]
- replacement::=string with & and \1,\2,\3,... replaced
- flags::= any_number_of( c | g | p ), -- confirm, global, print.
- append_to_file::= address w >> filename.
- write_to_new_file::= address w filename.
- overwrite_file::= address w! filename.
. . . . . . . . . ( end of section Syntax) <<Contents | End>>
We have a file of introductory material:
//www.csci.csusb.edu/doc/vi.txt
and the Frequently Asked Questions on Usenet
//www.csci.csusb.edu/doc/vi.FAQ.txt
and also some pointers to the internet archives
//www.csci.csusb.edu/doc/vi.archives.txt
Lynda Lamb has written an incredible book on vi that
is one of O'Reilly's first publications.
The UNIX in a NutShell books list vi commands.