Saving ViM Macros

March 11, 2009 at 09:32 AM | categories: tips, vim | View Comments

I use ViM macros (aka complex-repeat) all the time. Occasionally, I find that it would be nice to reuse one of my macros across editing sessions. Turns out it's pretty easy to save the macro. Just add a line like the following to a file that gets sourced when your file is opened by ViM (This example is from ~/.vim/ftplugin/rst.vim).

let @h = "yypVr"

Now, whenever I open a ReStructured Text file, I can hit @h and my macro will run.

Read and Post Comments

More Useful ViM Tags

March 04, 2009 at 08:35 PM | categories: tips, vim | View Comments

Frequently, I find myself writing C code that requires the use of struct ifreq. There are plenty of fields there and of course there are other structures that I never remember. I could always look in the headers, but it ends up being a good amount of digging before I find the real definition and all its accompanying pieces. So, I decided to let ctags and ViM do the work for me. I created ~/bin/update_local_tags with the following contents:

#!/bin/sh
[ -d ~/.localtags ] || mkdir ~/.localtags
ctags -f ~/.localtags/usrinclude.ctags --exclude=vector\*.hpp -R /usr/include >/dev/null 2>&1

Then I created ~/.vim/ftplugin/c.vim with the following contents:

setlocal tags+=$HOME/.localtags/usrinclude.ctags

For good measure I copied c.vim to cpp.vim so it would be loaded for C++ file types also.

Now I just have to run update_local_tags to generate a tag file for everything in /usr/include and ViM automatically includes that list whenever I edit a C or C++ file. Updating the tags file is still a manual process, I should probably attempt to hook it into apt. Observant readers will have noticed that I exclude vector*.hpp when generating the tags. Boost includes a few generated header files that match this pattern and swell the tag file to almost 750 MiB.

Read and Post Comments

Editing ViM Macros

March 17, 2007 at 03:16 PM | categories: tips, vim | View Comments

At the BYU UUG meeting this week, Peter mentioned a great ViM trick that I've never seen before. Here's my feeble attempt to document it.

Because ViM macros are stored in registers they can be edited. This means that if you create a long macro and then realize that you forgot to send the cursor to the beginning of the line before finishing the recording, you don't have to create the entire macro again, you can just add the motion command. You use it like this:

  1. Start recording your macro by typing q and then the single character ([0-9a-zA-Z"] are allowed). The single character is the register your macro will be stored in.
  2. Enter the commands you want included in the macro.
  3. Type q to finish recording the macro.
  4. At this point you could run the macro by using @ and the register name.
  5. To modify the macro, move to a blank line and type "Rp replacing R with the register name of your macro. This will paste all the commands in your macro to the current line.
  6. After making the changes you need, save the macro by typing 0"Ry$ replacing R with the register name you want to use for the macro. The register name does not have to be the same as the original.
Read and Post Comments