I use gf to open "filename.h" from line like:

#include "filename.h"

in some C\C++ file.

Sometimes there is more than one "filename.h" that gf command can find in path. But gf gets me to the first file it finds - sometimes that's not what I want.

Is there a way to open the next file matching that name?

  • 3
    gf accepts a count, so you can try 2gf. Is that what you want?
    – muru
    Apr 19 '17 at 8:49
  • Didn't expect that - but it works. Any idea if it's possible to cycle through the results? Apr 19 '17 at 10:53
  • @muru please add your comment as an answer so I can mark it as the correct response. Don't think there is anything better out of the box. Apr 20 '17 at 10:22

From :h gf:

                                                       gf E446 E447
[count]gf               Edit the file whose name is under or after the cursor.
                        If a [count] is given, the count'th file that is found
                        in the 'path' is edited.

So you can do 2gf.

To browse the list of matches is trickier. The only way I found was to use use :find and Vim's command-line tab completion:

:find <cfile>Tab

Vim will cycle through matches and <cfile>:

enter image description here

  • 1
    Shouldn't that be <cfile>? <cfile> is replaced with the path name under the cursor (like what gf uses)
    – Shahbaz
    Apr 21 '17 at 2:08
  • @Shahbaz indeed, corrected.
    – muru
    Apr 21 '17 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.