sponsor Vim development Vim logo Vim Book Ad

basic Tip #1075: beauty of gf - it can take a count

 tip karma   Rating 38/20, Viewed by 2430 

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

created:   December 10, 2005 16:14      complexity:   basic
author:   vnk      as of Vim:   5.7

gf stands for "goto file"
it opens the file under cursor. it is very useful for opening include files. it searches for files in the directories set by path option
in my case, i have the same include file name in different folders (for different projects, let us say) and my path option includes all these folders.
SO whenever i use gf on include file, it was opening an include file (which is not the one i want to open).
while reading the help, i found that gf can take a count and it will open the count' th file that is found

try the following:
gf
2gf
3gf etc

if there are several with the same names, one can give the count to open the exact file needed.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Detect encoding from the charset specified in HTML files | Hex or Unhex strings. >>

Additional Notes

Anonymous, December 10, 2005 18:44
It would be nice if there was a script or something that just took the name and would display a list of matching files to choose from (or directly open the file if it is the only one).
hermitte {at} free {dot} fr, December 12, 2005 6:34
Regarding the script to display the matches:
   :nnoremap glf :SearchInVar! &path Echo <c-r>=expand("<cfile>")<cr><cr>
Thanks to the plugin searchInRuntime (vimscript#229).

However, I suspect there is a more "standard" and simple method like:
   :echo globpath(&path, expand('<cfile>'))
from which we can count the number of matching files and propose which one to open, if several match.
jagit sing, December 21, 2005 21:20
you post a line from the manual and call it a beauty tip?
Ivan Tishchenko, December 22, 2005 5:09
Why not?  See vimtip #1.
Anonymous, December 22, 2005 13:38
Seems like something that could be hacked into a.vim easily. That script has a variety of path handling options and does cycling in a pretty clever way.
hermitte {at} free {dot} fr, January 12, 2006 17:01
I've updated searchInRuntime.vim (vimscript#229) to override gf and CTRL-W_f in a way that vim asks which file must be opened if several match. Alternatives for :sp and :vsp are also provided. Vim7+ required.
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.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo