[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [Samples] / vi.notes
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Thu Aug 25 21:06:51 PDT 2011

Opening the PDF files on this page may require you to download Adobe Reader or an equivalent viewer (GhostScript).


    Vi Tips and techniques


      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 [ vi.txt ]

      I have created a list of vi command [ 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!) [ vi_charts.pdf ] , Printable (PostScript) version [ vi_charts.ps ] , Quick list of command [ vi.commands ] , Some Frequently Asked vi Questions [ vi.FAQ ] , [ vi.html ]

      Why vi

      1. You are a very good touch typist and so can type
        faster than you can grab the mouse highlight the 6th word on line 7 and copy it.
      2. 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.
      3. You like the feeling of power at your finger tips.
      4. You are trying to get pico to work and so can't use pico to do the work.
      5. Your home UNIX system has 'vi', 'ex', and 'ed'... and no other editors.
      6. The window system is broken and you can't use Jot/TextEdit...
      7. 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.
      8. You are at home and using SSH. You login and try
        and 'ksh: pico not found'. Then
        and 'ksh: emacs not found'. You have a choice of 'ex' or 'vi'.

      9. 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.

      10. You are on a terminal that doesn't have any arrow keys!

      Why you still hate vi

      1. Your fingers still hit the wrong keys sometimes.
      2. You forget which mode you are in.


      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 [ 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!

      Key assignments in vi

      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


      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

      (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

      (1) programmers must change identifiers consistently... do it globally. Use a command like

      (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:

       	!}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...

      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"



      1. c::=character.
      2. ctrl(k)::=tapping the k key with the Control shift key held down.
      3. dot::= "."
      4. period::= "."
      5. eoln::=tap the Return or Enter key
      6. eoln= End-of-line.

      7. esc::=the Esc character, ASCII code #27 tap the top left hand key.
      8. letter::="a".."z" | "A".."Z".
      9. n::=decimal_number.
      10. text::=any series of characters with no Esc in it unless prefixed by a ctrl V.
      11. SP::=space.
      12. BS::=Back_space.

      13. big_word::=sequence of non-white-space characters -- includes punctuation and math symbols.
      14. word::=either a sequence of letters and digits, or a sequence of non-whitespace and non alphanumeric symbols.

        List of Vi Commands

      15. vi_repeat_command_n_times::= n vi_command,
      16. vi_append_after::= a.
      17. vi_add_after_end_of_line::=A.
      18. vi_move_back_a_word::=b.
      19. vi_move_back_a_big_word::=B.
      20. vi_change_something::= c movement replacement_text esc
      21. vi_change_rest_of_line::= C replacement_text esc
      22. vi_change_one_line::= cc replacement_text esc.
      23. vi_change_n_lines::= c n c replacement_text esc.
      24. replacement_text::=text.
      25. vi_cut_command::= double_quote buffer_id vi_delete_something,
      26. vi_delete_something::= d movement. For example: dw - delete word, d3l - delete next 3 chars,... Also [ vi_delete_character ]
      27. vi_delete_rest_of_line::=D.
      28. vi_delete_whole_line::= dd.
      29. vi_delete_n_lines::= d n d.
      30. vi_move_to_end_of_word::=e.
      31. vi_move_to_end_of_big_word::=E, includes puctuation but not whitespace.
      32. vi_move_to_find_char_in_line::= f c.
      33. vi_move_to_find_previous_char_in_line::= F c.
      34. vi_go_forward_a_screen::= ctrl/F,
      35. vi_move_forward::=ctrl/F,
      36. vi_go_to_line_n::= n G
      37. vi_move_to_line::= n G
      38. vi_go_to_last_line::= G,
      39. vi_move_to_last_line::= G,
      40. vi_give_me_information_on_this_file::=ctrl/G | :f eoln
      41. vi_move_back_a_space::= h | BS.
      42. vi_insert_text::= i inserted_text esc.
      43. inserted_text::=text_without_an_Esc_character.
      44. vi_insert_a_start_of_line::= I inserted_text esc.
      45. vi_move_down_one_line::=j
      46. vi_move_to_start_of_text_on_next_line::=eoln| + .
      47. vi_join_lines::=J.
      48. vi_move_up_one_line::=k.
      49. vi_move_up_to_start_of_text_on_previous_line::= -.
      50. vi_move_forward_one_space::= l | SP.
      51. vi_mark_this_point::= m letter.
      52. vi_move_to_next_string::=n.
      53. vi_move_to_next_string_backwards::=N.
      54. vi_open_line_below::=o inserted_text esc.
      55. vi_open_line_above::=Oinserted_text esc.
      56. vi_paste_last_delete_after::=p.
      57. vi_paste_last_delete_before::=P.
      58. vi_paste_buffer::=double_quote buffer_id (p | P).
      59. vi_paste_last_but_n_delete::=double_quote n (p|P).
      60. vi_quit_visual::=Q.
      61. vi_replace_a_char::= r c
      62. vi_replace_string::= R text esc.
      63. vi_substitutes::=s something
      64. vi_move_to_char::=t c.
      65. vi_move_to_char_backwards::=T c.
      66. vi_undo::=u.
      67. vi_Big_undo::=U.
      68. vi_undo_everything::= :e! eoln.
      69. vi_redo::= dot .
      70. vi_move_over_word::=w.
      71. vi_move_over_big_word::=W.
      72. vi_delete_char::=vi_cross_out|vi_cross_out_previous.
      73. vi_cross_out::=x.
      74. vi_cross_out_previous::=X
      75. yank::=copY.
      76. copY_into_buffer::= double_quote buffer_id vi_copy_command,
      77. buffer_id::= letter
      78. vi_copY_something::= y movement.
      79. vi_copY_line::=yy.
      80. vi_go_to_sleep::=ZZ
      81. vi_line_commands::= : ex_command.
      82. vi_quit_and_no_save::= q!
      83. vi_search_forward::= / vi_regular_expression,
      84. vi_search_backward::= ? vi_regular_expression,
      85. vi_regular_expression::= See http://cse.csusb.edu/dick/samples/regular_expressions.html. [ 26.patterns.html ]

      86. vi_shift_line_left::= <<,
      87. vi_shift_line_right::= >>,
      88. vi_shift_n_lines_left::= <n<,
      89. vi_shift_n_lines_right::= >n>,
      90. vi_shift_something_left::= <movement,
      91. vi_shift_something_right::= >movement,

      92. vi_shell_escape::= vi_pipe_thru | ex_shell_escape,
      93. vi_pipe_thru::= vi_pipe_line | vi_pipe_n_lines | vi_pipe_something,
      94. ex_shell_escape::= :! shell_command,
      95. vi_pipe_line::= !!,
      96. vi_pipe_n_lines::=!n!,
      97. vi_pipe_something::= ! movement.

      98. vi_go_to_column_n::= n vertical_bar,
      99. vi_go_to_first_column::=0.
      100. vi_go_to_first_nonwhitespace::= ^
      101. vi_go_to_last_char_in_line::= $.

      102. vi_move_to_column_n::= n vertical_bar,
      103. vi_move_to_end_of_paragraph::= }.
      104. vi_move_to_first_column::=0.
      105. vi_move_to_first_non_whitespace::= ^
      106. vi_move_to_last_column::=$.
      107. vi_move_to_marked_character::=`
      108. vi_move_to_marked_line::='
      109. vi_move_to_matching_parenthesis::=%.
      110. vi_move_to_start_of_paragraph::= {.

      111. vi_word_wrap_the_whole_file::= 1G!Gbr eoln.

      112. movement::= any number followed by any vi command with "move" in above list,
      113. movement::=n single_movement.
      114. single_movement::= h | j |k | l | + | - | Fc|fc|tc|Tc G | SP | BS | { | } |....
      115. c::=any character

        The Vi Alphabet

      116. vi_command::=many including the following

        Some vi Line mode/ex commands

      117. ex_command::= address action,
      118. 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

      119. address::= range | line | global | all_lines,

      120. all_lines::=%.
      121. this_file::= %.
      122. range::= line,line,
      123. global::= g search, vsearch,
      124. line::=search | n | place ( + | - ) n | search (+|-) n,
      125. place::= here | end_of_file,
      126. here::= dot
      127. end_of_file::= $
      128. search::= forwards|backwards
      129. forwards::= / pattern /,
      130. backwards::= ? pattern ?,

      131. ex_substitute_command::=addresss/pattern/replacement/flags,
      132. pattern::= See http://cse.csusb.edu/dick/samples/regular_expressions.html

      133. replacement::=string with & and \1,\2,\3,... replaced
      134. flags::= any_number_of( c | g | p ), -- confirm, global, print.

      135. append_to_file::= address w >> filename.
      136. write_to_new_file::= address w filename.
      137. overwrite_file::= address w! filename.

      . . . . . . . . . ( end of section Syntax) <<Contents | End>>

      See also

      We have a file of introductory material: [ vi.txt ] and the Frequently Asked Questions on Usenet [ vi.FAQ.txt ] and also some pointers to the internet archives [ 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.

    . . . . . . . . . ( end of section Vi Tips and techniques) <<Contents | End>>