2

I have a file/buffer that contains multiple file names with line numbers. Example:

Declaration of x
    fileA.h +3

Usage of x
    fileB.c +10

I can open a file with the cursor in the given line number with gF. I can list all file names with :g/ +\d/. But how can I open all files listed in this buffer?

I have tried :g/ +\d/normal gF. But this opens only fileA.h, not fileB.c.

3 Answers 3

2

My guess is, as soon as you start editing the new buffer fileA.h, Vim will abort the :g command, because, ... well you moved away from the buffer 🤷‍♂️

So I would instead try to use the following: Open all matched files in a new tabpage and immediately move back to the original tabpage.

Something like this should do it:

:g/ +\d/exe ":norm \<c-w>gF"|norm gT

This does the following:

  • find each line matching +\d and
  • execute <CTRL-W>gF on the match, which is basically the same as gF except that it will open the file in a new buffer (and leave the current window and tab alone)
  • and immediately jump back to the previous tabpage gT to allow the :g command to continue.
4
  • Thanks. It works. But I don't want to use tabs. Shouldn't it work for buffers instead of tabs like this? :g/ +\d/exe ":norm gF"|norm \<C-^> (It doesn't. I don't know why.)
    – MaxGyver
    May 2 at 19:46
  • I just noticed that with this tab approach files are opened in reverse order :-(
    – MaxGyver
    May 2 at 19:49
  • 1
    I guess you want :g/ +\d/exe ":norm gF"|exe ":norm \<C-^>" then. May 2 at 19:53
  • Awesome! This is exactly what I need. Thank you!
    – MaxGyver
    May 2 at 19:58
1

I don't think there is a simple way to do that.

It can be done with a recording:

" record macro
qq          " start recording in register q
/+\d<CR>    " search for line number
b           " move cursor back one word to the filename
<C-w>gF     " open file at line in new tab page
q           " stop recording

" playback macro from register q as many times as needed
23@q        " 23 times for 23 remaining filenames

If you need it often, it might be wise to invest in a bit of scripting.

1
  • Thanks. (Actually I don't want to open files in a tab. But I can simply skip <C-w>.) This is strange: I executed the macro only once and both files were opened!
    – MaxGyver
    May 2 at 19:42
0

A different way to approach this would be to populate the Quickfix list

:setlocal efm=%f\ +%l
:cbuffer

Now you should be on the first file location. You can use :cnext/:cprev to go through the list. Use :copen to see your list. Can even use :cdo/:cfdo if you want to execute a command on each location/file

One side effect is this does set the local 'errorformat' setting.

Another low-tech way to do something similar is to set an uppercase mark, e.g. mM. Then gf to the file you want to go. When you are done return with 'M

For more help see:

:h 'efm'
:h :cbuffer
:h :cn
:h :cope
:h m

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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