这方面没有本地方法。典型的方法是记录宏并重复执行它。确保在宏末尾使用n
或/
以跳转到下一个匹配项。现在跳过简单地使用n
和@@
来执行宏。
自定义:Global
命令
但是如果您真正想要带有确认的:global
命令,可以通过在您的命令中使用confirm()
来模拟这个功能。基本思路是这样的:
:g/pat/if confirm("&yes\n&no", 2) == 1 | cmd | endif
以下方法存在以下问题:
- 无法确定光标所在位置。需要使用类似于
:match
和:redraw
的东西。
- 无法有效地终止。需要一种抛出异常以终止的方式。
- 输入太繁琐。
我想到了以下确认性:Global
/:G
命令:
command! -nargs=+ -range=% -complete=command Global <line1>,<line2>call <SID>global_confirm(<q-args>)
command! -nargs=+ -range=% -complete=command G <line1>,<line2>call <SID>global_confirm(<q-args>)
function! s:global_confirm(args) range
let args = a:args
let sep = args[0]
let [pat, cmd; _] = split(args[1:], '\v([^\\](\\\\)*\\)@<!%d' . char2nr(sep), 1) + ['', '']
match none
let options = ['throw "Global: Abort"', cmd, '', 'throw "Global: Abort"']
let cmd = 'exe ''match IncSearch /\c\%''.line(''.'').''l''.@/.''/'''
let cmd .= '| redraw'
let cmd .= '| exe get(options, confirm("Execute?", "&yes\n&no\n&abort", 2))'
try
execute a:firstline . ',' . a:lastline . 'g'.sep.pat.sep.cmd
catch /Global: Abort/
finally
match none
endtry
endfunction
注意:按原样使用。使用
IncSearch
进行高亮并强制使用
\c
。
现在您可以运行
:G / foo / d
。
自定义
:Confirm
命令
如果您更喜欢使用与@Randy Morris提供的技术类似的技术,并使用以下
: Confirm {cmd}
命令在执行之前确认
{cmd}
。
command! -nargs=+ -complete=command Confirm execute <SID>confirm(<q-args>) | match none
function! s:confirm(cmd)
let abort = 'match none | throw "Confirm: Abort"'
let options = [abort, a:cmd, '', abort]
match none
execute 'match IncSearch /\c\%' . line('.') . 'l' . @/ . '/'
redraw
return get(options, confirm('Execute?', "&yes\n&no\n&abort", 2), abort)
endfunction
这将允许您使用
:g/foo/Confirm d
。
查看更多帮助,请参阅:
:h @
:h q
:h confirm()
:h :exe
:h get()
:h :match
:h :redraw