VI Tricks

VI is a text editor that comes on pretty much every *nix box on the planet.  It can be your friend or it can be a difficult beast to wrestle with.  The interface on it is *not* intuitive at all but it is extremely powerful.  You can do things in VI that can only be done with some sort of regex otherwise. 

For example, if you want to remove all the blank lines from you code before you start to compress it…

  1. Open your document in vi
  2. Get your edit prompt by hitting ESC
  3. Type this in:  %s/^$//g
  4. Hit enter & you’re done

If you don’t have dos2unix on your box, and you need get rid of the ^M’s Windows puts in everything…..

  1. Open your document in vi
  2. Get your edit prompt by hitting ESC
  3. Type this in:%s/<CTRLV><CTRLM>//g
  4. Hit enter & you’re done

If an error message is giving you a specific line of code, you set line numbers to show……

  1. Open your document in vi
  2. Get your edit prompt by hitting ESC
  3. Type this in: set nu
  4. Hit enter and line numbers will magically appear

Want to jump to the line that you’re looking for instead of scrolling…..

  1. Open your document in vi
  2. Get your edit prompt by hitting ESC
  3. Type this in:#G where # is the line number
  4. Hit enter and hit your destination
  5. You can get to the top by using 0G (that’s a zero) and to the end by using G by itself

Need to delete an entire block of text, no problem……

  1. Open your document in vi
  2. Get your edit prompt by hitting ESC
  3. Type this in:dd# where # is the number of lines of text you want gone
  4. Hit enter and it will magically disappear
  5. If you want to move it, navigate through the document until you get to where you want it and hit “p”
Advertisements

5 thoughts on “VI Tricks

  1. Huh. I’ve been reading for a while now and I must admit that this is probably the last place I expected to see vi tips. Just a couple of minor corrections though:
    <li>The substitute commands (“%s/”) need to be entered in command mode, which you use “:” to get into, so the complete command would be (without quotes) “:%s/^$//g”.
    <li>The proper key sequence for enabling line numbering is “:set nu” (short for the “number” option), and “:set nonu” disables them.<li>It’s “#dd” to delete a number of lines. And “p” will insert them below the current line, and “P” above.
    And a rather large correction. All these tips work fine on vim, which is a derivative of vi, but they may not all work on the original vi as found in larger Unices.

  2. @ignacio –  The setnu was a typo.  As for the others, they work fine on the Solaris, Gentoo, and Ubuntu servers we run here and we’re not running VIM.  We strip our boxes to the bone.  We only run vi.  Not even pico or emacs. 

  3. @morrighu – Gentoo and Ubuntu (and probably even modern versions of Solaris) don’t actually come with vi, even if the command you use to invoke it is “vi”. What they have is vim running in compatibility mode. There’s no need for all the goodies that come with vim if all you want is a basic text editor. But it is vim nonetheless.

Comments are closed.