mr-edd.co.uk :: horsing around with the C++ programming language

Edit counterpart vim script

[17th May 2008]

I recently decided to learn either Vim or Emacs. For no particular reason I picked Vim.

One of the things I wanted was the ability to open the header corresponding to the currently open source file (or vice versa) using a simple shortcut. So I wrote a Python script to do this for me. It's pretty useful, so I've decided to share it here.

Now I can hit Ctrl-c to go to the counterpart file, or any of Ctrl-h, Ctrl-j, Ctrl-k and Ctrl-l to open the counterpart file in a split window to the left, below, above or to the right of the current window, respectively.

Counterparts are looked for in the directories surrounding the directory of the current file. This includes:

Various common C and C++ file extensions are supported (.c/.h, .cpp/.hpp, .cxx/.hxx, .C/.H, .cc/.hh and so on).

In my .vimrc I have the following to plumb in the script:

python << EOF
import sys, os.path
plugin = os.path.expanduser('~/.vim/plugin')
if not plugin in sys.path: sys.path.append(plugin)
import cpp
EOF

map <C-c> :py cpp.edit_counterpart()<CR>
map <C-j> :py cpp.edit_counterpart_split('^')<CR>
map <C-k> :py cpp.edit_counterpart_split('v')<CR>
map <C-h> :py cpp.edit_counterpart_split('<')<CR>
map <C-l> :py cpp.edit_counterpart_split('>')<CR>

Downloads

Comments

Richard

[31/08/2008 at 13:50:00]

See also a.vim, Alternate Files quickly (.c –> .h etc)
http://www.vim.org/scripts/script.php?script_id=31

(optional)
(optional)
(required, hint)

Links can be added like [this one -> http://www.mr-edd.co.uk], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.