Vim Tips Wiki
No edit summary
 
(Added categories, clean up)
Line 10: Line 10:
 
|text=
 
|text=
 
To switch between header and source files very quickly, all you need to do is add a few key mappings in your filetype plugin files. Let me explain with an example:
 
To switch between header and source files very quickly, all you need to do is add a few key mappings in your filetype plugin files. Let me explain with an example:
 
 
   
 
Let's say that you're editing C files, so all you would have to do is edit your ftplugin/c_extra.vim file and include
 
Let's say that you're editing C files, so all you would have to do is edit your ftplugin/c_extra.vim file and include
   
 
nmap ,s :find %:t:r.c<cr>
 
 
nmap ,S :sf %:t:r.c<cr>
 
nmap ,s :find %:t:r.c<cr>
 
 
nmap ,S :sf %:t:r.c<cr>
 
 
 
   
 
to switch to the corresponding source file, and
 
to switch to the corresponding source file, and
   
 
nmap ,h :find %:t:r.h<cr>
 
 
nmap ,H :sf %:t:r.h<cr>
 
nmap ,h :find %:t:r.h<cr>
 
 
nmap ,H :sf %:t:r.h<cr>
 
 
 
   
 
to switch to the corresponding header file.
 
to switch to the corresponding header file.
 
 
   
 
The built-in 'find' command will search (recursively or not) for the specified file anywhere in your vim 'path' setting. The 'sf' is short for split-find, meaning that if vim finds your file it will open it in a split window. Simply add the 'vert' keyword before 'sf' if you want a vertical split.
 
The built-in 'find' command will search (recursively or not) for the specified file anywhere in your vim 'path' setting. The 'sf' is short for split-find, meaning that if vim finds your file it will open it in a split window. Simply add the 'vert' keyword before 'sf' if you want a vertical split.
 
 
   
 
See these help pages for a full description of these built-in features:
 
See these help pages for a full description of these built-in features:
   
help expand # for a description of the %, :t, :r expansion
+
*{{help|expand}} for a description of the %, :t, :r expansion
   
help find # for a description of the 'find' and 'sf' features
+
*{{help|find}} for a description of the 'find' and 'sf' features
 
help ftplugin # for a description of how filetype plugins work
 
 
help path # for a description of how the path setting works
 
   
 
*{{help|ftplugin}} for a description of how filetype plugins work
   
 
*{{help|path}} for a description of how the path setting works
   
 
This method is also highly configurable. All you have to do is change the 'path' setting when switching to different projects, and modify the corresponding filetype plugin to support other languages.
 
This method is also highly configurable. All you have to do is change the 'path' setting when switching to different projects, and modify the corresponding filetype plugin to support other languages.
   
 
This tip is very similar to {{Script|id=31}} by Mike Sharpe, however this method only takes a few lines, and his script spans several pages!
 
 
This tip is very similar to [/scripts/script.php?script_id=31 vimscript #31] by Mike Sharpe, however this method only takes a few lines, and his script spans several pages!
 
 
}}
 
}}
   
Line 63: Line 43:
 
Two things:
 
Two things:
   
1) The mappings should be made buffer-specific with a <buffer> modifier.
+
# The mappings should be made buffer-specific with a <buffer> modifier.
2) If you use an if clause in your ftplugin/c.vim file (that checks to see if the current file ends in a .h or .c), you might be able to use the same mapping to go to the .h file if you're editing the .c and vice versa. You could easily map the sequence to a simple function that does a variation of expand("%") (with a modification flag) to extract the extension. . .
+
# If you use an if clause in your ftplugin/c.vim file (that checks to see if the current file ends in a .h or .c), you might be able to use the same mapping to go to the .h file if you're editing the .c and vice versa. You could easily map the sequence to a simple function that does a variation of expand("%") (with a modification flag) to extract the extension. . .
   
 
salmanhalim--AT--hotmail.com
 
salmanhalim--AT--hotmail.com
 
, December 6, 2002 22:11
 
, December 6, 2002 22:11
 
----
 
----
Since nobody mentioned it so far, I will do it: [/scripts/script.php?script_id=31 vimscript #31] (a.vim) provides
+
Since nobody mentioned it so far, I will do it: {{Script|id=31}} (a.vim) provides
exectly the functionality you mentioned and can be configured for various
+
exactly the functionality you mentioned and can be configured for various
 
filetypes (not only C/Cpp).
 
filetypes (not only C/Cpp).
 
   
 
'''Anonymous'''
 
'''Anonymous'''
Line 79: Line 58:
 
My my... do we have an urge to over-complicate things? =D I think the <buffer> idea is a good one, but adding a function to first inspect the suffix seems to be of little benefit. The idea here is simplicity!
 
My my... do we have an urge to over-complicate things? =D I think the <buffer> idea is a good one, but adding a function to first inspect the suffix seems to be of little benefit. The idea here is simplicity!
   
One reason this idea is better than [/scripts/script.php?script_id=31 vimscript #31] (IMHO) is because additional file types can be added with very little effort and without modification to the original script (a.vim). Also it's 10,000% shorter. ;-D
+
One reason this idea is better than {{Script|id=31}} (IMHO) is because additional file types can be added with very little effort and without modification to the original script (a.vim). Also it's 10,000% shorter. ;-D
   
 
bubbleboy
 
bubbleboy
Line 86: Line 65:
 
A few notes :)
 
A few notes :)
   
1) :find foo.c when foo.c does not exist can be a rather long time consuming search, particularly when foo.c does not exist. [/scripts/script.php?script_id=31 vimscript #31] does not have this problem. And if foo.c does not exist the switch still works...you do not get the E345 error message.
+
# :find foo.c when foo.c does not exist can be a rather long time consuming search, particularly when foo.c does not exist. {{Script|id=31}} does not have this problem. And if foo.c does not exist the switch still works...you do not get the E345 error message.
 
# {{Script|id=31}} works with multiple extensions, C, C++, ada etc, can be configured for more without editing the script. Read some of the comments in the 10000 line file :)
 
 
# {{Script|id=31}} works with different extension styles .C .cxx .cpp .CPP .cc <--> .h .hpp .H .HPP all at the same time. :A will switch between any of the source extensions to any of the header extensions. Different extension styles are sadly common in large projects.
2) [/scripts/script.php?script_id=31 vimscript #31] works with multiple extensions, C, C++, ada etc, can be configured for more without editing the script. Read some of the comments in the 10000 line file :)
 
 
# 10000 lines?? less than 200 once comments are stripped and utility functions could be moved to a common.vim for reuse if size is an issue.
 
3) [/scripts/script.php?script_id=31 vimscript #31] works with different extension styles .C .cxx .cpp .CPP .cc <--> .h .hpp .H .HPP all at the same time. :A will switch between any of the source extensions to any of the header extensions. Different extension styles are sadly common in large projects.
 
 
4) 10000 lines?? less than 200 once comments are stripped and utility functions could be moved to a common.vim for reuse if size is an issue.
 
 
 
   
 
anonymous
 
anonymous
Line 106: Line 80:
 
hi. thanks, i like it a lot. based on your tip i've made:
 
hi. thanks, i like it a lot. based on your tip i've made:
   
  +
<pre>
function! SwitchSourceHeader()
+
function! SwitchSourceHeader()
"update!
+
"update!
if (expand ("%:t") == expand ("%:t:r") . ".cpp")
+
if (expand ("%:t") == expand ("%:t:r") . ".cpp")
find %:t:r.h
+
find %:t:r.h
else
 
  +
else
find %:t:r.cpp
+
find %:t:r.cpp
endif
 
  +
endif
endfunction
+
endfunction
   
nmap ,s :call SwitchSourceHeader()&lt;CR&gt;
+
nmap ,s :call SwitchSourceHeader()<CR>
  +
</pre>
   
 
it's still pretty simple (i think) and you can switch just by pressing ,s
 
it's still pretty simple (i think) and you can switch just by pressing ,s
Line 122: Line 98:
 
inetic
 
inetic
 
, April 29, 2005 13:14
 
, April 29, 2005 13:14
  +
----
  +
See also [[VimTip839]]. [[User:Ipkiss|Ipkiss]] 12:19, 21 July 2007 (UTC)
 
----
 
----
 
<!-- parsed by vimtips.py in 0.489252 seconds-->
 
<!-- parsed by vimtips.py in 0.489252 seconds-->
  +
[[Category:C]]
  +
[[Category:C plus plus]]

Revision as of 12:19, 21 July 2007

Previous TipNext Tip

Tip: #384 - Easily switch between source and header file

Created: December 6, 2002 15:26 Complexity: intermediate Author: bubbleboy Version: 5.7 Karma: 124/46 Imported from: Tip#384

To switch between header and source files very quickly, all you need to do is add a few key mappings in your filetype plugin files. Let me explain with an example:

Let's say that you're editing C files, so all you would have to do is edit your ftplugin/c_extra.vim file and include

nmap ,s :find %:t:r.c<cr> 
nmap ,S :sf %:t:r.c<cr> 

to switch to the corresponding source file, and

nmap ,h :find %:t:r.h<cr> 
nmap ,H :sf %:t:r.h<cr> 

to switch to the corresponding header file.

The built-in 'find' command will search (recursively or not) for the specified file anywhere in your vim 'path' setting. The 'sf' is short for split-find, meaning that if vim finds your file it will open it in a split window. Simply add the 'vert' keyword before 'sf' if you want a vertical split.

See these help pages for a full description of these built-in features:

  • :help find for a description of the 'find' and 'sf' features
  • :help path for a description of how the path setting works

This method is also highly configurable. All you have to do is change the 'path' setting when switching to different projects, and modify the corresponding filetype plugin to support other languages.

This tip is very similar to script#31 by Mike Sharpe, however this method only takes a few lines, and his script spans several pages!

Comments

Two things:

  1. The mappings should be made buffer-specific with a <buffer> modifier.
  2. If you use an if clause in your ftplugin/c.vim file (that checks to see if the current file ends in a .h or .c), you might be able to use the same mapping to go to the .h file if you're editing the .c and vice versa. You could easily map the sequence to a simple function that does a variation of expand("%") (with a modification flag) to extract the extension. . .

salmanhalim--AT--hotmail.com , December 6, 2002 22:11


Since nobody mentioned it so far, I will do it: script#31 (a.vim) provides exactly the functionality you mentioned and can be configured for various filetypes (not only C/Cpp).

Anonymous , December 9, 2002 7:52


My my... do we have an urge to over-complicate things? =D I think the <buffer> idea is a good one, but adding a function to first inspect the suffix seems to be of little benefit. The idea here is simplicity!

One reason this idea is better than script#31 (IMHO) is because additional file types can be added with very little effort and without modification to the original script (a.vim). Also it's 10,000% shorter. ;-D

bubbleboy , December 9, 2002 10:35


A few notes :)

  1. :find foo.c when foo.c does not exist can be a rather long time consuming search, particularly when foo.c does not exist. script#31 does not have this problem. And if foo.c does not exist the switch still works...you do not get the E345 error message.
  2. script#31 works with multiple extensions, C, C++, ada etc, can be configured for more without editing the script. Read some of the comments in the 10000 line file :)
  3. script#31 works with different extension styles .C .cxx .cpp .CPP .cc <--> .h .hpp .H .HPP all at the same time. :A will switch between any of the source extensions to any of the header extensions. Different extension styles are sadly common in large projects.
  4. 10000 lines?? less than 200 once comments are stripped and utility functions could be moved to a common.vim for reuse if size is an issue.

anonymous , December 10, 2002 6:03


Nice tip, I didn't know you could stack modifiers after %.

http://mixedvolume.blogspot.com , January 4, 2005 12:47


hi. thanks, i like it a lot. based on your tip i've made:

function! SwitchSourceHeader()
    "update!
    if (expand ("%:t") == expand ("%:t:r") . ".cpp")
        find %:t:r.h
    else
        find %:t:r.cpp
    endif
endfunction

nmap ,s :call SwitchSourceHeader()<CR>

it's still pretty simple (i think) and you can switch just by pressing ,s whether you are in *.h or *.cpp file.

inetic , April 29, 2005 13:14


See also VimTip839. Ipkiss 12:19, 21 July 2007 (UTC)