sponsor Vim development Vim logo Vim Book Ad

basic Tip #839: switch editing: .c* to .h*

 tip karma   Rating 20/22, Viewed by 1407 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   December 29, 2004 11:28      complexity:   basic
author:   http://mixedvolume.blogspot.com      as of Vim:   5.7

This command allows you to
quickly switch editing between .c* and .h* files.

It is especially useful when the header file is
in different direcotry and filenames are long.
Example: press 'zh' when editing fxx1234.cxx to open fxx1234.hxx

:map zh :call Mosh_Flip_Ext()

" Since .h file can be in a different dir, call :find instead of :e
" :find file will be searched in this path
set  path=.,,..,../..,./*,./*/*,../*,~/,~/**,

" note there are double backslashes below, http post might mangle them.

function! Mosh_Flip_Ext()
    :if match(expand("%"),"\\.c") > 0
        let s:flipname = glob(expand("%:r").".h*")
        :exe ":find " s:flipname
    :elseif match(expand("%"),"\\.h") > 0
        let s:flipname = glob(expand("%:r").".c*")
        :exe ":find " s:flipname

 rate this tip  Life Changing Helpful Unfulfilling 

<< Load file from list of filenames in textfile | Show tags in a separate preview window >>

Additional Notes

http://mixedvolume.blogspot.com, December 29, 2004 14:20
I just realized that the glob won't work when the
header file is not in the same dir as the c file.
You can use 'substitute' to flip the extensions like this:

let s:flipname = substitute(expand("%"),"\\.h\\(.*\\)",".c\\1","")  

- M
Anonymous, December 30, 2004 0:15
Use this script instead:

none, January 4, 2005 11:58
A better solution is at vimtip #384.
Anonymous, January 4, 2005 18:07
You mean a half-assed solution right? a.vim has numerous more features.
anonymous, July 13, 2005 18:32
Sheesh. Be nice now. It isn't half assed, it's elegant. vimtip #384 is simple (allowing for easy changes), powerful and fast. vimscript #31 tries to handle every file type (nevermind the unknown file types) and duplicates much of vim's built-in features, making it unnecessarily bloated.
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 vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo