This page is on the Computer Science and Engineering School's WWW Server at CalState, San Bernardino, California, USA in Dick Botting's site . Please Email comments to


    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.html ]

      I have created a list of vi command [ List of Vi Commands ]

      Why vi

      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.

      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

      and 'ksh: pico not found'. Then
      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
      and recovered my edited file.

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

      Why you still hate vi

      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


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

      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:
      [ 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.



      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.
      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.
      44. vi_insert_a_start_of_line::= I inserted_text esc.
      45. vi_move_down::=j
      46. vi_move_to_start_of_text_on_next_line::=eoln| + .
      47. vi_join_lines::=J.
      48. vi_move_up::=k.
      49. vi_move_up_to_start_of_text_on_previous_line::= -.
      50. vi_move_forward::= 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, [ 26.patterns.html ] [ 26.patterns.html ]

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

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

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

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

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

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

        The Vi Alphabet

      115. vi_command::=many including the following

        Some vi Line mode/ex commands

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

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

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

      130. ex_substitute_command::=addresss/pattern/replacement/flags,
      131. pattern::= [ 26.patterns.html ] [ 26.patterns.html ]

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

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

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

      See also

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

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