.Open Vi Tips and techniques . Acknowledgement I got a lot of help from O'Reilly's UNIX PowerTools book. . Beginners start here If you've never really had a chance to learn `vi` before see the exercises and facts in .See ./vi.txt I have created a list of `vi` command .See List of Vi Commands . Other resources in my Web site Pictures of the key mappings: what each key does as a command , Dangerous PDF (OK on Linux, crashes Adobe!) .See ./vi_charts.pdf , Printable (PostScript) version .See ./vi_charts.ps , Quick list of command .See ./vi.commands , Some Frequently Asked vi Questions .See ./vi.FAQ , .See ./vi.html . Why vi .List You are a very good touch typist and so can type .As_is 6j5wyw faster than you can grab the mouse highlight the 6th word on line 7 and copy it. To fix a broken workstation you use SSH to log into it from a functional machine and can't recall how to export windows to the working machine from the broken one. 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 using SSH. You login and try .As_is pico and 'ksh: pico not found'. Then .As_is 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 .As_is vi -r 30.vi.mth and recovered my edited file. You are on a terminal that doesn't have any arrow keys! .Close.List . Why you still hate vi .List Your fingers still hit the wrong keys sometimes. You forget which mode you are in. .Close.List . Facts 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. We have EMACS but I can teach you `vi`. We are stuck with `vi`. But 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 .See 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 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! .Image vi1.gif Key assignments in vi .Image ./vi2.gif Key assignments in vi Tapping 'Q' (Shifted Q) gets you into permanent line edit mode or 'ex'. In 'ex' you can get into visual mode with the command .As_is vis Don't forget 'vi' can edit many files at one time and you can move data between them. Learn these: .As_is :w Write this file .As_is :n Next file .As_is :e Edit file .As_is :e# Edit the other file .As_is :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: .As_is cut "cd .As_is copy "cy .As_is paste "cp pastes after cursor .As_is paste "cP pastes before cursor See 30.05. 'vi' reads .As_is .exrc (at $HOME or in the current working directory) before starting up.... What is in your .exrc? .As_is ex the extended editor that vi is an interface to .As_is rc read commands (1) programmers must change identifiers consistently... do it globally. Use a command like .As_is :g/wodget/s//widget/g (2) administrators need to take data generated by a command - say a list of hacking attempts - and summarize or clarify them... Because `vi` will pass your line/file/paragraph through ANY UNIX command we have some powerful options: .As_is !}br word wrap paragraph .As_is !Gsort|uniq -c|sort -nr list and count words in file .As_is !4!spellu spell check 4 lines .As_is !'a stats work out statistics... 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: .As_is :!generate_index_file >index.html & Shell escapes are a handy "boss panic key" .Open Syntax . Notation 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 tap the top left hand key. 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. . List of Vi Commands 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 .See 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_without_an_Esc_character`. vi_insert_a_start_of_line::= I `$inserted_text` `$esc`. vi_move_down_one_line::=j vi_move_to_start_of_text_on_next_line::=`$eoln`| + . vi_join_lines::=J. vi_move_up_one_line::=k. vi_move_up_to_start_of_text_on_previous_line::= -. vi_move_forward_one_space::= 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::=O`$inserted_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`, vi_regular_expression::=http://www/dick/samples/regular_expressions.html. .See http://www/dick/cs360/notes/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 | + | - | F`$c`|f`$c`|t`$c`|T`$c` G | `$SP` | `$BS` | { | } |.... c::=`any character` . The Vi Alphabet vi_command::=many including the following .Set 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. .Close.Set . Some vi Line mode/ex commands ex_command::= `$address` `$action`, ex_actions::=following .Set 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 .Close.Set address::= `$range` | `$line` | `$global` | `$all_lines`, all_lines::=%. this_file::= %. range::= `$line`,`$line`, global::= g `$search`, v`$search`, 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::=`$address`s/`$pattern`/`$replacement`/`$flags`, pattern::=http://www/dick/samples/regular_expressions.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. .Close Syntax . See also We have a file of introductory material: .See http://www.csci.csusb.edu/dick/doc/vi and the Frequently Asked Questions on Usenet .See http://www.csci.csusb.edu/dick/doc/vi.FAQ and also some pointers to the internet archives .See http://www.csci.csusb.edu/dick/doc/vi.archives 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. .Close Vi Tips and techniques