Let me start out with a list of some of the keys and what they do.
1G
takes you to the top of the file.0G
takes you to to bottom of the file.d$ and D
delete from the cursor to end of line (especially good if 'end' key is mapped.)d0
deletes from the cursor to beginning of line (see above about mapping.)CTRL-G
shows the line number you are on.m#
-where # is a letter, sets a mark, '#
takes you back to that mark, `# takes you back to the exact same place.I would like to start out by saying that there are many versions of vi out there. There is vi itself, vim, elvis, nvi, etc. Most of the commands that I talk about here should work with all or at least many of them. However, I personally use vim and I will cast my recommendation for that particular flavor of vi. I would also like to note that I use vi to refer to any of the vi flavors, not just the original
The last time I talked about vi I talked about textural substitution. Now I would like to talk about key mapping. vi will let you map any key to any series of commands. These mappings will work as long as you are in command mode. I should forewarn you that vi has no problem with you changing existing key definitions. I do not recommend this however; you could be begging for trouble. These mappings can be applied either at the ":" prompt or in your .exrc, .vimrc,.elvisrc, etc... file. Let me share one of my favorite key mappings with you.
map T ^[I ^[^[OB
Here "^[" is CTRL-v then ESC and "^[OB" is CTRL-v then Down-arrow.
This little macro takes care of one of my greatest editorial banes. Whenever I program I always find that I need to add one more encompassing 'if' statement. What this means to all of you non-programmers is that I need to tab over a whole bunch of lines by hand. What this macro does is
This macro works well for all versions of vi. However, most versions of vi support a command that does almost this exact thing for you. A line is shifted to the right with the command "<<", and the command to shift a line the other way is ">>." To adjust the width of the shift you can set the shiftwidth variable(in vim). ":set shiftwidth=#". There is one big advantage to using the built in version; you can precede the command with a number to tell it how many lines you want to tab over.
Not all systems and not all versions of vi have the insert, delete, end and home keys defined. These can all be easily defined in the vi rc file.
This method will also let you make use of those underused F-keys. Want a save button. Boom, there it is. Want a save and exit, Exit without save, spell check button, compile button? Any command that vi will let you execute you can set up as a key-mapped macro.
Another good mapping is the map the backspace button to esc-X
. This way the backspace will behave more like you are used to. You can also map it to esc-X-i
; this will put you back in insert mode without a hitch. 8)
Well, this is a lot of fun and I have a lot more interesting vi tricks to share, but they will have to wait for next time. Some of the things I hope to show you are:
Well, that ought to just about do it for vi then. It shouldn't take more than a few more articles to finish this off. If anyone is interested in having me learn emacs and write some stuff up on that, please let me know, otherwise I will leave it at that and move on to other things. If I miss any interesting aspects of vi please let me know.