Cool vi Tips

vi is a venerable text editor that is great for making quick fixes to files when you are in the command line on the server.

If you have ssh access enabled on your web hosting server (ask support if you want it turned on), you can use the vi editor to make quick changes to files.

For example, if you get a PHP error that says something like "error on line 42", you can use vi to go directly to that line.

To make vi start at a that line in a file, add +linenumber to the command you use to start vi. Replace linenumber with the actual line number, for example:

vi +42 index.php

If you're already opened up the file in vi, use the goto command. To use goto, press Esc, type the line number, and then press Shift-g . If you press Esc and then Shift-g without entering a line number, it will take you to the last line in the file.

The ex command line can be used to go to a line. For example, if you wanted to go to line 108, you could press Esc and then enter:

:108

To search forward for some text, use the / (forward slash) command. Press Esc and then enter /pattern , replacing pattern with the text for which you want to search forward. For example, to find every instance of the word "namaste", enter:

/namaste

To look for the next occurrence after the first, either press n or press / again and then press Enter. To go back to a previous occurrence, press Shift-n . To search backwards in a file, use the ? command.

To see what line you're on at any time, press Ctrl-Shift-g . To number all lines, press Esc and enter:

:set number

To go to the end of a file use, G (capital G)

To delete to the end of a line, use D (capital D)

To delete a whole line, use dd (two d)

To repeat the last command, use . (period)

Category:

Author: ivanoats

Created on: June 19th, 2009

leave a comment

Back to article list