How to open a file in a list of files in Vim?

Viewed 26673

I have a longish list of files opened in vim that looks like this:

/dir1/file1
/dir2/file2
/dir2/file3
.....

How can I open all of them one by one the easiest way possible in the same session of vim either with split or edit?

11 Answers

I'd say with -p for tabs

vim -p `cat yourlistoffiles`

You can use quickfix mode, as following

:set errorformat=%f
:cf myfilelist

at this point you can use the normal quickfix shortcuts to go through your files, :cn for the next file, :cp for the previous one and :cr to go to the first again.

EDIT:

oh, if you want to read the list from the current buffer, use :cb instead of :cf in in the instructions above

You can do the following

cat file | xargs vim

Where "file" contains your list of files, this will open the files in the same vim session. As usual when opening multiple buffers, you can navigate forward with :bn and backward :bp.

I'm going to assume you have the file list open inside Vim, and want to simulate the "gf" command across the whole list...

Edit your .vimrc to include this function:

function Openall()
    edit <cfile>
    bfirst
endfunction

You can then highlight the entire file (or the set of paths you want to open) using visual mode (1G, Shift-V, G) and typing ":call Openall()". Afterwards the command row will show this:

:'<,'>call Openall()

This will run the new Openall() function across all highlighted lines.

Press Enter and all the files will be opened in background buffers. You can then access them using the usual buffer commands. :ls will display them as a list.

For my use case, where you already have a buffer open with the list of files in it, I have found the best way to do this. You can type this in vim cmd mode:

:% normal gf<C-v><C-o>

(where <C-v> is you literally typing Ctrl and v, same for <C-o>)

and it becomes:

:% normal gf^O

How this works:

  • % runs the command for every line in the file
  • normal runs everything after it in normal mode
  • gf opens the file under the cursor in a new buffer
  • <C-v> lets you enter a control-code (<C-o> in this case)
  • <C-o> goes back to the last location, which will always be this buffer with the list of files

Potential usage:

# open buffer with list of files from grep
$ grep -rl "new User()" mycode/ libraries/ | vim -

# then runs this in vim:
:% normal gf^O

Bonus: Make a keyboard shortcut

Add the following to your ~/.vimrc :

" Open all files in current buffer (e.g. after piping to `vim -`)
nnoremap <leader>oa :% normal gf^O<cr>

This maps \oa to run the command (since <leader> is \ for me).

An alternate method using a macro:

qogf<C-o>j

which runs gf (go file) then uses <C-o> to get back to the initial unnamed buffer, then just goes down one line with j, ready to be repeated.

Then see how many lines in the buffer (12) and run the macro like this:

12@o

It's as simple as typing

vim /dir1/file1 /dir2/file1 /dir2/file2 ...

Once you're in vim, you can switch betwen then with ":n" to go to the next file, ":prev" to go to the previous file.

My searchInRuntime plugin has a :Sp and a :Vsp commands that'll do the trick when called banged. However, the files have to exist.

Related