views:

82

answers:

2

First off, I was about to write a long list of if/else statements in vim and realized that 1) there was a better way to do what I was trying to do and 2) SO would be ripe with help on the subject. So! I have a variety of files spread about like

foo/src/file01.C
foo/src/file02.cc
foo/src/file03.c
foo/include/file01.hh
foo/include/file02.h
foo/include/file03.h

If you notice that the C/H, cc/hh, c/h extension may or may not match then you are keen and I'd like you to please help. I've look at things like the following vim scripts from the Vim wiki for "Easily switch between source and header file" and although I only dumped a few hours into a.vim without success, it doesn't seem that the others would work via the docs on that page. So can anyone help out on how to make this work?

A good lead I had was a quick How to Easily Switch between Header and Source topic, but still couldn't make it work.

I guess what I really want is how to avoid the multiple if statements and use real matching to do what I want. I want to look into another directory and if look for a header file of the same name with any familiar extension if it was a source C/C++ file, or look for a source file of any regular extension if it was a header file. Thanks for your help!

UPDATE: I specifically want to open the file in a new tab. I live on vim tabs!

A: 

IMO your best option is to adopt existing scripts to use :tabf instead of :e or whatever the scripts use right now to open the counterpart file. You can also try to make the change configurable and submit it to the script author. (I'm pretty sure many would find the enhancement useful.)

That reminded me of a trick I used very long time ago. Instead of guessing where the corresponding source/header files are, I have used at the top of file special comment containing relative path to the counterpart file. Switching was as simple as finding the special comment, extracting file name and opening it. Problem was similar to yours in that file extensions were not predictable. My rational solution was to stop guessing and denote counterparts explicitly in the source code. (This days I would have probably tried to put the relationship table into an external file with a standard name and look it up in VIM using the upward search.)

Dummy00001
A: 

Just to make sure I was using the most current version, I downloaded the latest a.vim script (2.18) and copied it into my ~/.vim/plugin directory.

You can define certain variables in your ~/.vimrc file to get a.vim to recognize alternate file extensions.

To get the files in your example to match their alternates I added the following to my ~/.vimrc:

let g:alternateExtensions_C = "H,hh"
let g:alternateExtensions_hh = "C"

These are global variables that allow you to override what's already defined. You'll have to define both relationships (they don't work both ways).

You can see what the current mappings are by typing:

:echo g:alternateExtensionsDict

If you need to define other mappings, follow the same pattern. What comes after the underscore is the file extension you're editing. What's in the double quotes is a comma-separated list of alternate extensions.

let g:alternateExtensions_<ext> = "<list,of,alt,ext>"

If you have a different directory structure, you can define what paths to search by overriding the g:alternateSearchPath variable. ../src and ../include are already included by default.

:echo g:alternateSearchPath

To open the alternate file in a new tab:

:AT

By the way, the a.vim script is pretty well documented. You might want to open it up and take a look. I found a setting or two that I didn't know about and I've been using it for years ;o)

I hope that helps.

Curt Nelson
Thanks for your help. I'll delve into it as soon as I can.
vgm64