如何获取当前在vim中比较的文件列表 [英] How to get list of files which are currently being diffed in vim

查看:27
本文介绍了如何获取当前在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

  1. 获取所有列出的缓冲区的列表
  2. 对于每个缓冲区,确定 diff 是否为 1
  3. 如果 diff1 使用 bufwinnr() 获取窗口号.
  4. 根据窗口编号确定哪一个是左边的,哪一个是右边的.左边的窗口编号较小
  5. 然后使用当前缓冲区的 winnr 确定当前缓冲区(其中 alt-left`alt-right` 被按下)是向左还是向右.
  1. Get a list of all listed buffers
  2. For each of this buffers determine if diff is 1 for that
  3. If diff is 1 use bufwinnr() to gets it window number.
  4. From the window numbers determine which one is left and which one is right. left one will have smaller window number
  5. 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.

推荐答案

  1. 循环遍历所有可能的缓冲区编号,从 0bufnr('$') 并使用 bufexists(nr) 检查此缓冲区是否存在>.
  2. 使用 let curbuf=bufnr('%') 保存当前缓冲区编号.
  3. 对于每个现有的缓冲区,执行buffer".bufnumber 并检查 &diff 变量.记住两个数字,但不要检查bufwinnr().
  4. 执行buffer".curbuf.
  5. 最后为步骤 3 中找到的两个数字调用 bufwinnr(nr).
  1. Cycle through all possible buffer numbers from 0 to bufnr('$') and check, whether this buffer exists using bufexists(nr).
  2. Save current buffer number using let curbuf=bufnr('%').
  3. For each existing buffer do execute "buffer ".bufnumber and check &diff variable. Remember two numbers, but do not check bufwinnr().
  4. Do execute "buffer ".curbuf.
  5. 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屋!

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