How to get list of files which are currently being diffed in vim

感情迁移 提交于 2019-12-10 18:18:00

问题


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.

Is it possible to do this.

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

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

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:


  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: 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


来源:https://stackoverflow.com/questions/2486420/how-to-get-list-of-files-which-are-currently-being-diffed-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!