1

Is it possible to pipe list of files e.g. from grep and have this file list opened in vim file manager in similar fashion like when you open a folder with vim?

Note that I do not want to just write the file names into vim buffer like a text. I want vim to treat them as if these files were in a folder and I would be able to choose which one to open.

EDIT: Thanks to your great advice I could add these two functions in my .bashrc, they do almost exactly what I want:

# Find files by name in the notes and open them as subset in vim, opens first file in the search in the buffer:
function notefv {
    vim --cmd 'set efm=%f' -c copen -q <(find ~/Notes -name "*$1*")
}

# Grep notes for a keyword, open the result in vim quickfix list, opens first file in the search in the buffer
function noteg {
    vim --cmd 'set efm=%f' -c copen -q <(grep -irl --color $1 ~/Notes/*)
}

This uses native vim functionality. if I'll have nothing to do, I'll try to rewrite it so that the subset of files is opened in dervish or NerdTree.

4
  • Welcome to Vi and Vim! – filbranden Dec 11 '20 at 23:04
  • 1
    The file manager (netrw) is not really suitable to display a subset of files, or files in separate directories... The quickfix list is typically the place to do that. If you have a list of filenames, one quick-and-dirty approach is to use something like vim --cmd 'set efm=%f' -c copen -q <(find . -type f). See :help -q in Vim to see how to populate the quickfix list when invoking Vim. With grep it might be even easier, since Vim might be expecting that kind of format for the quickfix list.. – filbranden Dec 11 '20 at 23:41
  • In addition to @filbranden s comment: if you wanted to pipe it into the arguments list in vim, look at xargs, e.g. ls | xargs vim -o -- # open all files in split. I would suggest to add th --, because otherwise you might run into issues if you had files starting with a - ;) – Michael Dec 11 '20 at 23:59
  • Regarding your second paragraph, are you already aware of the gf command? – Rich Dec 12 '20 at 18:14
2

Here are three options depending on what you are trying to achieve:

  1. If you specifically want the output of grep, then you can just use :grep/:vimgrep from within vim. This will populate the quickfix window with a list of files which you can browse and press Enter to open.

  2. If you have a different command that generates your list of files, but it is relatively static (or perhaps just takes a single argument) then the native way to do this is to write your own function making use of setqflist() to populate the quickfix window. I use this in my own configuration as shown here.

  3. Finally, if the command to generate the files changes a lot, then as Matt notes, a plugin like vim-dirvish can achieve this. Using this plugin, any buffer with filetype=dirvish will be treated as a list of paths to then manipulate (including open). This essentally provides some niceities around gf. You can then make use of :redir and system().

I have assumed that you meant to open in vim itself and not vifm when you said "vim file manager".

2

Unlike netrw, there are Vim file managers who follow a different paradigm: simply fill in some lines and present'em "as if it's a file list".

The most well-known "buffer-like file manager" is probably vim-dirvish. As a shameless plug, I also wrote one of my own called vim-drvo.

So you can create a buffer containing random file names, type :setf drvo and :setl bufhidden=hide (to keep contents if buffer becomes hidden) and it just works out-of-the-box.

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.