View Full Version : vi .exrc tips and tricks


MrHnau
10-14-2005, 09:07 AM
I've found it useful to share with others some of the stuff I've stuck in my .exrc file, and to learn from others. Everyone has different stuff it seems :smirk:

I've found some of the following to be extremely useful.

map g ^[:e#^M
^[ is the code for escape, ^M is the code for enter. To enter a control character, you need to enter a control-V followed by the character you want in your file. For instance, ^[ would be control-V ESC.
I use this one all the time. It allows you to toggle between two files. Useful when having to cut and paste or compare two files. Do this when not in edit mode

map ^A ^[:%s/^[ ]*//g^M
In this case ^A is control A, do it when not in edit mode. I've found cutting and pasting between windows and a Vi editor can really screw up your formatting. This removes all preceeding spaces and tabs. The
[ ] actually contains a space and a tab. order is irrelevant.

ab teh the
one of my common typos when I'm typing fast. Stick in your favorite to cut back on edit times! Also can be used for abbreviations, eg
ab Jan January
Do this in edit mode.

map = ^[i#^[56a-^M^[
Enter a long comment line when not in edit mode. Modify it for your appropriate programming language/shell. If you want, you can program in a complete header if its normally uniform.

Anything anyone else would like to share?

MrH

MrHnau
10-14-2005, 10:32 AM
Another useful thing I've played with in Vi is the use of buffers. You can use up to 36 different buffers. 10 are dedicated to "normal" yanks and deletes. 26 are user defined.

Normal buffers:
Vi keeps a history of your yanks and deletes. Can be quite useful, especially if you regret something you deleted!
dd (stores your line in buffer 1)
dd (moves contents of buffer 1 into buffer 2, stores your next line in buffer 1)
"2p (pastes contents of buffer 2, your first line)

User defined:
You also have 26 user defined buffers, a-z. Can use them in the same fashion as the default buffers, you just need to identify which buffer you want to use!
"ayy (Store your line in buffer a)
"b20yy (store 20 lines in buffer b)
"bp (pastes contents of buffer b)

You can also work with tags! Sometimes you don't know exactly how many lines to cut, so you can set a mark for vi to remember your location. You can cut from your current location to the location of your mark. For instance, you have a file with the following contents:

1
2
3
4
5

from the top line, do the following
jmm (go down one line, set the mark "m")
jj"ay`m (go down two lines, pull into buffer "a" the contents of the current line all the way back to buffer "m")
G"ap (go to the end of the file, dump contents of buffer "a")

Your file should look like
1
2
3
4
5
2
3

This technique is extremely useful when moving between multiple files. You can use many buffers, you just need to remember which is in each! If you want to include your current line, change the following line from
jj"ay`m (backtick)
to
jj"ay'm (single quote)

MrH