gpt4 book ai didi

vim - 如何只对 中指定的模块执行某些命令?

转载 作者:行者123 更新时间:2023-12-04 19:40:00 25 4
gpt4 key购买 nike

我要执行MyCommand需要访问 b:somevar对于 <afile> 指定的缓冲区.现在我正在做类似的事情

function F()
let l:a = bufnr(expand("%"))
let l:b = bufnr(expand("<afile>"))
execute "bufdo call G(" . l:b . ")"
execute "buffer " . a
endfunction

function G(d)
let l:a = bufnr(expand("%"))
if l:a == a:d
execute 'MyCommand'
endif
endfunction

autocmd BufDelete *.hs :call F()

所以 F()检查每个加载的缓冲区是否在 <afile> 中.它有效,但感觉很疯狂,应该有更好的方法。

最佳答案

MyCommand只需要访问 b:somevar (也许缓冲区内容通过 getbufline() ),然后它可以使用 getbufvar(expand('<abuf>'), 'somevar') .


另一方面,如果它需要直接在缓冲区上执行命令,则需要临时在窗口中显示缓冲区,如下所示:

function! ExecuteInVisibleBuffer( bufnr, command )
let l:winnr = bufwinnr(a:bufnr)
if l:winnr == -1
" The buffer is hidden. Make it visible to execute the passed function.
let l:originalWindowLayout = winrestcmd()
execute 'noautocmd silent keepalt leftabove sbuffer' a:bufnr
try
execute a:command
finally
noautocmd silent close
silent! execute l:originalWindowLayout
endtry
else
" The buffer is visible in at least one window on this tab page.
let l:currentWinNr = winnr()
execute l:winnr . 'wincmd w'
try
execute a:command
finally
execute l:currentWinNr . 'wincmd w'
endtry
endif
endfunction

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com