如何将输出重定向到Gvim作为要打开的文件列表?

E.B*_*ach 9 windows vim findstr

我想findstr /m background *.vim | gvim打开*.vim包含background在单个gvim实例中的所有文件- 但我无法让管道工作.

这与此问题非常相似,但我不希望捕获stdin输出,而是希望GViM将输出视为要打开的文件列表 - 在Windows系统上,因此无法保证xargs.有任何想法吗?

DrA*_*rAl 6

我可以想到几种方法:

使用vimgrep

使用vimgrep:运行gvim后,输入:

:vimgrep /background/ **/*.vim
Run Code Online (Sandbox Code Playgroud)

这将填充quickfix列表与所有的比赛(可能不止一个每个文件)的,所以你可以使用喜欢的东西:copen,:cw,:cn等导航(见:help quickfix)


使用vim的内置聪明

使用findstr给你的文件列表,然后让VIM打开这些文件:

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 %
Run Code Online (Sandbox Code Playgroud)

这将加载每个文件,但也将保持文件列表打开(您可以随时加载它或其他).

编辑:

:tabedit在文件列表上使用不起作用(我只测试过:badd).您可以通过使用badd然后使用bufdo(如上所述)或通过执行此类操作(将其放入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
Run Code Online (Sandbox Code Playgroud)

然后只需打开包含所有必需文件名的文件,然后键入:

:OpenListedFiles
Run Code Online (Sandbox Code Playgroud)

使用Vim的服务器功能和一些糟糕的批处理脚本

使用服务器功能和一些批处理脚本魔术(我使用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
Run Code Online (Sandbox Code Playgroud)

Eeeurrgh.


如果是我,我会选择"使用vim的内置聪明"选项.实际上,这不是真的:我使用cygwin的bash脚本并且只使用bash,但如果我不想使用本机工具,我会使用内置的聪明方法.


sas*_*nin 5

在bash中:

grep -l background *.vim | xargs gvim
Run Code Online (Sandbox Code Playgroud)

关键是xargs。它从标准输入中获取行并将它们作为命令行参数传递。grep -l仅打印匹配的文件名。


另一个想法,如果你没有xargs并且无法下载它们,那么你可以将行转换为vim命令(:edit filename)并让vim执行它们,即打开所有文件。同样,在我的环境中,我有sed

grep -l background *.vim | sed 's/^/:edit /' > files
vim -s files
Run Code Online (Sandbox Code Playgroud)

即使没有sed,也可以用vi -e( ed) 代替。