如何将输出重定向到 Gvim 作为要打开的文件列表? [英] How do I redirect output into Gvim as a list of files to be opened?

查看:21
本文介绍了如何将输出重定向到 Gvim 作为要打开的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 Windows system so xargs isn't guaranteed. Any ideas?

推荐答案

我能想到几种方法来做到这一点:

I can think of a few ways of doing this:

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

Use vimgrep: after running gvim, enter:

:vimgrep /background/ **/*.vim

这将使用所有匹配项(每个文件可能不止一个)填充 quickfix 列表,因此您可以使用诸如 :copen:cw:cn 等导航(见:help quickfix)

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)

使用 findstr 给你一个文件列表,然后让 vim 打开这些文件:

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).

编辑:

在文件列表上使用 :tabedit 不起作用(我只测试了 :badd).您可以通过使用 badd 然后使用 bufdo(如上所述)或通过执行类似操作(将其放入您的 vimrc)来解决此问题:

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

<小时>

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

使用服务器功能和一些批处理脚本魔法(我不明白,因为我使用 bash)


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

呃呃.

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

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.

这篇关于如何将输出重定向到 Gvim 作为要打开的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆