
.gvimrc
我在我的文件中使用以下 Vim 自動命令:
augroup MyAuGroup
autocmd MyAuGroup FileChangedShell * call FileChanedEvent_BuffUpdate()
augroup END
function FileChanedEvent_BuffUpdate()
let MyBn = bufname("%")
let MyStr = "Warning: File \"".MyBn."\" has changed since editing started\nSee \":help W11\" for more info."
let MyTest = confirm(MyStr, "&OK\n&Load File", 2, "W")
if MyTest == 2
edit
else
endif
endfunction
目的是在外部更改檔案時替換預設的 gVim 行為(請參閱這個問題)。但是,如果開啟多個視窗並顯示多個緩衝區,則該edit
指令適用於最後一個活動窗口,而不適用於包含已變更緩衝區的視窗。
如何確定哪個緩衝區導致了該FileChangedShell
事件,並將edit
命令應用於該緩衝區?
答案1
從:help FileChangedShell
:
NOTE: When this autocommand is executed, the current buffer "%" may be different from the buffer that was changed "<afile>".
您需要找到正在編輯對應文件的視窗。為此,緩衝區編號(在 中<abuf>
)甚至更簡單:
let winNr = bufwinnr(0 + expand('<abuf>'))
execute winNr . 'wincmd w'
edit
這同樣適用於緩衝區名稱;代替
let MyBn = bufname("%")
和
let MyBn = expand('<afile>')
答案2
感謝@IngoKarkat 的回答。我找到了替代解決方案。將if
函數中的區塊替換為:
if MyTest == 2
let v:fcs_choice = "reload"
else
let v:fcs_choice = ""
endif
這似乎可以解決問題。