|
Thursday, 13 April 2006 |
Vi Macros - A quick Look Submitted by Sean Brown vi macros can be very useful for coding. They can be used to automatically print out a skeleton for classes, int main() in C/C++ and whatever else you want with just a few keystrokes. Just open up the .exrc file or create your own file and use the map command. Here is an example of how to output an int main in C/C++ when you type in !qq during command mode: map !qq ^[iint main(int argc, char* argv[])^M{^M} This mess needs an explanation and, believe it or not, this is a lot easier than it looks. I'll skip the map !qq portion because they were mentioned above. The ^[iint portion is actually three things at once. ^[ is the esc character, the i is just an i (used to enter text-enter mode) and int is the return type for main. This is equivalent to manually typing in esc, i and int into vi. If you don't want to remember the ^[ then just remember ctrl-v. For those who don't know, ctrl-v and esc will print ^[ and vi knows that ^[ is the esc key. Let's continue. The main(int argc, char* argv[]) is just C/C++ syntax. ^M is a carriage return. The curly brackets are just C/C++ open and closed curly brackets. If you didn't put your macros in the vi resource file then you can load your macros into vi by going to the text buffer and typing the source command followed by the file name that contains all your macros. For example, if the file macros.txt held all my map commands then the syntax would look like the following: source macros.txt Tips: Remember to start of the map command with the ^[i. This tells vi that the macro wants to enter text-enter mode. If you don't type this then vi interprets everything as a command and then who know's what your output will be! ctrl-v is your friend and can be used to print out the string that corresponds to any non-alphanumeric keys. A good thing to do is start off your command with a symbol such as !, @, or something else that vi doesn't already use in command mode. This is good if you want a command that is more than one keystroke long. Also, unused alphabetic commands are rare in vi.
|
|
Last Updated ( Tuesday, 10 April 2007 )
|