sponsor Vim development Vim logo Vim Book Ad

a.vim : Alternate Files quickly (.c --> .h etc)

 script karma  Rating 3596/1232, Downloaded by 90839  Comments, bugs, improvements  Vim wiki

created by
Mike Sharpe
 
script type
utility
 
description
A few of quick commands to swtich between source files and header files quickly.

:A switches to the header file corresponding to the current file being edited (or vise versa)
:AS splits and switches
:AV vertical splits and switches
:AT new tab and switches
:AN cycles through matches
:IH switches to file under cursor
:IHS splits and switches
:IHV vertical splits and switches
:IHT new tab and switches
:IHN cycles through matches
<Leader>ih switches to file under cursor
<Leader>is switches to the alternate file of file under cursor (e.g. on  <foo.h> switches to foo.cpp)
<Leader>ihn cycles through matches

E.g. if you are editing foo.c and need to edit foo.h simply execute :A and you will be editting foo.h, to switch back to foo.c execute :A again.

Can be configured to support a variety of languages. Builtin support for C, C++ and ADA95
 
install details
Drop a.vim into your favorite plugin directory or source the script from your .vimrc file
Drop alternate.txt into you doc directory and run helptags
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
a.vim 2.18 2007-06-07 7.0 Mike Sharpe Recent versions since 2.16 broke user defined alternate specifications. This patch fixes that.
a.vim 2.17 2007-05-23 7.0 Mike Sharpe Fix a bug were spaces in filenames and/or directory names would prevent the alternate file being openned. Thanks to Nathan Stien (for the bug report and patch) and Soeren Sonnenburg (for the bug report).
a.vim 2.16 2007-03-16 7.0 Mike Sharpe Recent patches broke the script in some area, mainly in the area of file extensions which contain a dot...e.g. aspx.cs. Switched to using a dictionary instead of the curly brace variable things.
alternate.txt 2.15 doc 2006-10-27 7.0 Mike Sharpe Docs for 2.15
a.vim 2.15 2006-10-27 7.0 Mike Sharpe Initial support for jumping to files under the cursor. New commands IH, IHS, IHV, IHT and IHN. Added sample macros for jumping to the source file corresponding to the header file under the cursor (e.g. jumping on #include <myfile.h> will find myfile.cpp).

See documentation for more details.
a.vim 2.14 2006-05-14 7.0 Mike Sharpe Added new :AN command which cycles through all matches after switching to a new file. E.g. vi a.c, :A switches to a.h, :AN might switch to a.hpp if a.h and a.hpp exist. Will only likely be an issue in a big project with with same named source/header files around the place. Will likely only be an issue if you are using the search path and regex features of a.vim.
alternate.txt 2.14 doc 2006-05-14 7.0 Mike Sharpe Documentation for a.vim v2.14. Drop it in your doc directory and run helptags.
a.vim 2.13 2006-05-07 7.0 Mike Sharpe Added new "AT" command which finds the alternate file and opens in a new tab. Similarly to the "A" and "AS" commands, if the buffer is already open it simply switches to that buffer in the corresponding tab/window. This script will only work with VIM7 from this version forward.
a.vim 2.12 2005-04-12 6.0 Mike Sharpe Added alternate mappings for OCaml. Nothing more. No reason to upgrade...unless you use OCaml.
a.vim 2.11a 2004-10-19 6.0 Mike Sharpe Fix a minor bug when adding alternate extensions. Thanks ilya.
a.vim 2.11 2004-09-24 6.0 Mike Sharpe Fix some bugs which have recently crept in. I gave this some regression testing...so I hope all are fixed now. Mot notably, vim foo/a.c would not alternate to foo/a.h. Not sure how that broke...but it is fixed now. Other features still appear to work. Let me know if there are issues.
a.vim 2.10 2004-09-15 6.0 Mike Sharpe Added a new variable (g:alternateNoDefaultAlternate) which can be set in the .vimrc/_vimrc file to prevent a.vim from creating new files. This is useful when it is not desired to have a.vim to alternate to the default alternation for a particular extension. E.g. if a.c is being editted and a.h does not exist anywhere and :A is done then if g:alternateNoDefaultAlternate is non-zero a.h will not be created. By default the value of g:alternateNoDefaultAlternate is 0 to maintain existing behaviour.
a.vim 2.9 2004-09-10 6.0 Mike Sharpe Allow the extension of a file to be more than the text after the last dot. E.g. adding settings like

let g:alternateExtensions_{'aspx'} = "aspx.cs,aspx.CS"
let g:alternateExtensions_{'aspx.cs'} = "aspx,ASPX"

will allow a.vim to alternate between foo.aspx and foo.aspx.cs and vice versa. Previous versions would not accept .aspx.cs as an exstension. This version is ok with that.
a.vim 2.8 2004-07-01 6.0 Mike Sharpe [CORRECTION]More enhancements from Bindu Wavell. A new search path type
of "reg:" wasadded. This version allows alternation of files based on regxes. E.g.
"reg:/inc/src/g/" will replace every instance of 'inc' with 'src' in the source
file path. This is useful for alternating between /some/path/inc/project/foo.h
to /some/path/src/project/foo.c. Similarly "reg:/src/inc/g/" is the opposite
alternation. See the comments for ExpandAlternatePath() for more information.
Soon I will produce some formal documentation for this...until then the script
is reasonably well commented.
a.vim 2.7 2004-05-16 6.0 Mike Sharpe Reworked all the logic for finding alternate files. The files in memory are prefered to files on disk now too. When there are multiple matches for a file found the file in memory is favour, files in the current directory are favoured over files on the path. Hopefully this fixes the inconsistencies in previous versions. Hopefully everything still works too.
a.vim 2.6a 2004-03-14 6.0 Mike Sharpe Remove reference to Decho which was accidentally left in after previous debuging. Sorry all.
a.vim 2.6 2004-03-13 6.0 Mike Sharpe Implemented a fix from Matt Perry, vi test.cc junktest.h and the :A would alternate to junktest.h and not test.h. Also some minor clean up of the directory search code.
a.vim 2.5 2004-01-20 6.0 Mike Sharpe Fixed an inconsistency. In 2.4 :A would always switch to the alternate file, regardless of whether unsaved changes were in the current file. In some cases it was possible to lose changes. This update changes :A back to 2.3 behaviour, but with a nicer error message, and provides support for a :A! command which will force the switch. This seems more consistent with the builtin vim/vi commands. Please let me know if there are any problems.
a.vim 2.4 2003-05-21 6.0 Mike Sharpe Fix error which occured when alternating from a file which had not been saved.
a.vim 2.3 2002-09-24 6.0 Mike Sharpe Added support provided by Bindu Wavell to search for the alternate file across directories.
a.vim 2.2 2002-03-06 6.0 Mike Sharpe minor clean up. Remove repeated code used to
setup the default alternate file mappings. No changes in functionality. No reason to upgrade.
a.vim 2.1 2002-02-02 6.0 Mike Sharpe simplified the config using vim's curly brace variables. Should behave the same way version 2.0X behaves....no huge reason to upgrade.
a.vim 2.0.1 2002-01-29 6.0 Leif Wickland Adds support for vertically splitting via the command :AV
a.vim 2.0b 2001-11-29 6.0 Mike Sharpe Added support for Ada95 extensions
minor bug fixed too.
a.vim 2.0a 2001-11-27 6.0 Mike Sharpe bug fix. handle the cases were the is no alternate file.
a.vim 2.0 2001-11-27 6.0 Mike Sharpe Complete rewrite. Now the supported extensions are configurable instead of being part of the code.
Notes on how to configure are in the comments in the file. Expect bugs, please be brave. Will fix reported issue quickly.
a.vim 1.4 2001-11-27 6.0 Mike Sharpe Added support for .hpp/.HPP files.
a.vim 1.3 2001-11-10 6.0 Mike Sharpe Take care of some issues with case-sensitivity of the extension on windows platforms.
a.vim 1.2 2001-11-10 6.0 Mike Sharpe Minor changes to cater for "files" which are in vim buffers but not on disk (yet).
a.vim 1.1 2001-09-12 6.0 Leif Wickland This is a plugin-ized version of 1.1 for vim 5.7
a.vim 1.1 2001-08-22 5.7 Mike Sharpe Added copyright information.
Support .cc, .cxx, .C extensions for C++ too.
a.vim 1.0 2001-07-09 5.7 Mike Sharpe Corrected vim version. The script works with 5.7, but should work with 6.0 too.
ip used for rating: 54.85.255.74

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github