如何获取当前在vim中比较的文件列表 [英] How to get list of files which are currently being diffed in vim
问题描述
我正在编写一个 vim 插件,我需要在其中确定当前正在差异的所有文件.这是设置 diff
的那些.我一直在浏览手册,但找不到太多内容.
I am writing a vim plugin in which i need to determine all those files which are currently being diffed. That is the ones for which diff
is set. I have been going through the manual but could not find much.
可以这样做吗.
这个问题实际上与问题how-to-有关在vim中检测窗口位置.在那个问题中,我试图获得窗口的位置,以便检测哪个差异是正确的,哪个是左差异.我得到的解决方案是使用 winnr()
This question is actually related to question how-to-detect-the-position-of-window-in-vim. In that question i was trying to get the position of window, so as to detect which one of the diffs is the right one and which is left one. The solution i got was to use winnr()
该解决方案仅在只有 2 个窗口(被差异化的窗口)时才有效.我想让它通用,这样即使在 vim 中打开了多个窗口,我也可以确定哪个在左边,哪个在右边.这就是我想解决的问题
That solution can work only if there are only 2 windows(the ones being diffed). I want to make it generic so that even if multiple windows are open in vim, i can determine which one is on left and which one is right. This is what i was thinking to solve the problem
- 获取所有列出的缓冲区的列表
- 对于每个缓冲区,确定
diff
是否为1
- 如果
diff
是1
使用bufwinnr()
获取窗口号. - 根据窗口编号确定哪一个是左边的,哪一个是右边的.左边的窗口编号较小
- 然后使用当前缓冲区的 winnr 确定当前缓冲区(其中
alt-left
`alt-right` 被按下)是向左还是向右.
- Get a list of all listed buffers
- For each of this buffers determine if
diff
is1
for that - If
diff
is1
usebufwinnr()
to gets it window number. - From the window numbers determine which one is left and which one is right. left one will have smaller window number
- And then determine if current buffer(in which
alt-left
`alt-right` is pressed) is left or right using winnr of current buffer.
现在缺少的部分是 1 和 2.对于 1 ls 可以使用,但我需要解析其输出.是否有一种直接的方法来获取所有列出的缓冲区的列表.然后有没有办法检查该缓冲区的 diff
是否为 1.
Now the pieces that are missing are 1 and 2. For 1 ls can be used but i need to parse its output. Is there a straightfwd way to get list of all listed buffers. And then is there a way to check if for that buffer diff
is 1 or not.
对于更简单的解决方案的任何建议也表示赞赏.
Any suggestions for a simpler solution are also appreciated.
推荐答案
- 循环遍历所有可能的缓冲区编号,从
0
到bufnr('$')
并使用bufexists(nr)
检查此缓冲区是否存在>. - 使用
let curbuf=bufnr('%')
保存当前缓冲区编号. - 对于每个现有的缓冲区,
执行buffer".bufnumber
并检查&diff
变量.记住两个数字,但不要检查bufwinnr()
. - 做
执行buffer".curbuf
. - 最后为步骤 3 中找到的两个数字调用
bufwinnr(nr)
.
- Cycle through all possible buffer numbers from
0
tobufnr('$')
and check, whether this buffer exists usingbufexists(nr)
. - Save current buffer number using
let curbuf=bufnr('%')
. - For each existing buffer do
execute "buffer ".bufnumber
and check&diff
variable. Remember two numbers, but do not checkbufwinnr()
. - Do
execute "buffer ".curbuf
. - Finally call
bufwinnr(nr)
for two numbers found in step 3.
UPD:另一种解决方案
UPD: another solution
let g:wlist={"0": [], "1":[]}
windo call add(g:wlist[&diff], bufnr('%'))
let g:diffbuffers=g:wlist.1
" here you have list of buffers with &diff option set in g:diffbuffers
这篇关于如何获取当前在vim中比较的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!