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:
- the directory of the file in the current buffer
- its ancestors up to a specified height
- the sub-directories of those ancestors down to a specified depth
- all of the above with either /include or /src appended, depending on whether the current buffer is a source or header file
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>
All original content copyright© Edd Dawson.
Any opinions expressed by Edd are his own and are not necessarily shared by his employer. Or by anyone else, in fact.
All source code appearing on this website that was written by Edd Dawson is made available under the terms of the Boost software license version 1.0 unless otherwise stated or implied by the license associated with the work from which the code is derived.