views:

453

answers:

3

I would like findstr /m background *.vim | gvim to open all *.vim files containing background in a single instance of gvim - but I can't get the piping to work.

This is very similar to this question but instead of capturing the stdin output I would like GViM to treat the output as a list of files to be opened - and on a dos system so xargs isn't guaranteed. Any ideas?

Thanks!

+1  A: 

Open up Powershell:

gvim $(findstr /m background *.vim)
Paul Betts
Thanks but powershell isn't an option. I need a solution with barebones dos.
Edward Beach
+2  A: 

In bash:

grep -l background *.vim | xargs gvim

The key is xargs. It takes lines from standard input and passes them as command line arguments. grep -l just prints file names with matches.


Another idea, if you don't have xargs and cannot download them, then you can transform lines into vim commands (:edit filename) and make vim execute them, i.e. open all files. Again, in my environment, where I have sed:

grep -l background *.vim | sed 's/^/:edit /' > files
vim -s files

Even if you don't have sed, you can probably replace it with vi -e (ed).

jetxee
Again thanks but I'm looking for dos specific solutions; grep and xargs are not available to me and downloading them is not an option.
Edward Beach
I didn't notice dos tag. Sorry.
jetxee
Another idea with vim -s. It needs a temporary file though. And also sed or some ed magic (ed is part of vi, so you surely have it).
jetxee
Doing this with vim could mess up your terminal. I would suggest gvim $(grep -l background *.vim)
graywh
+4  A: 

I can think of a few ways of doing this:

Use vimgrep

Use vimgrep: after running gvim, enter:

:vimgrep /background/ **/*.vim

This will populate the quickfix list with all of the matches (possibly more than one per file), so you can use things like :copen, :cw, :cn etc to navigate (see :help quickfix)


Use vim's built-in cleverness

Use findstr to give you a list of files and then get vim to open those files:

findstr /m background *.vim > list_of_files.txt
gvim list_of_files.txt

" In Gvim, read each file into the buffer list:
:g/^/exe 'badd' getline('.')

" Open the files in tabs:
:bufdo tabedit %

This will load each file, but will keep the list of files open as well (you can always bunload it or whatever).

Edit:

Using :tabedit on a list of files didn't work (I'd only tested :badd). You can get round this by either using badd and then bufdo (as above) or by doing something like this (put it in your vimrc):

command! -range=% OpenListedFiles <line1>,<line2>call OpenListedFiles()

function! OpenListedFiles() range
    let FileList = getline(a:firstline, a:lastline)
    for filename in FileList
        if filereadable(filename)
            exe 'tabedit' filename
        endif
    endfor
endfunction

Then simply open the file containing all of your required file names and type:

:OpenListedFiles

Use Vim's server functionality and some awful batch scripting

Use the server functionality and some batch script magic (which I don't understand as I use bash)

@echo off
REM Welcome to the hideous world of Windows batch scripts
findstr /m background *.vim > list_of_files.txt
REM Run GVIM (may not be required)
gvim
REM Still in command prompt or .bat file here
REM for each line in the file "list_of_files.txt", pass the line to OpenInTab
for /f %%i in (list_of_files.txt) do call:OpenInTab %%i
goto:eof

:OpenInTab
REM Open the file in a separate tab of an existing vim instance
gvim --remote-tab %~1
goto:eof

Eeeurrgh.


If it were me, I would go with the "Use vim's built-in cleverness" option. Actually, that's not true: I'd use cygwin's bash script and just use bash, but if I HAD to do it with the native tools, I'd use the built-in cleverness approach.

Al
So far I like using the vim's built-in cleverness option...but findstr output is in relative paths and vim needs the absolute path to open the file - how can I preprend the current directory findstr's output?
Edward Beach
How about :%s/\v(.*)/$ESCAPED_DIR\1/g, where $ESCAPED_DIR would be the path to the appropriate directory (including a final path separator) with path separators escaped with backslashes (e.g. C:\\path\\to\\pwd\\)? (The \v turns on super-magical behaviour of regex special characters.)
Michał Marczyk
Vim shouldn't need the absolute path as long as the path is relative to the directory you're in. Therefore, you could do `:cd common_path` before running the `:g` command. If you want to change the paths, simply do `:%s@^@C:/path/to/folder/@` (vim can cope with forward slashes and it saves having to escape the path).
Al
Note also that if list_of_files.txt is in the "common path" location, you can do `:cd %:p:h` to change to the common path.
Al
Thanks for the help on this. From the batch script I can also issue this vim command `-c "lcd %CD%"` and that seems to work just find. My problem is now with the `:g/^/exe 'tabedit' getline('.')` command. It only seems to work with the top most file. Any ideas?
Edward Beach
It seems that the first `tabedit` stops the `:g` command from running. I've modified the description above to add a couple of (tested) alternatives.
Al
Answered! Thanks Al.
Edward Beach