目前我可以搜索文字
/text
Run Code Online (Sandbox Code Playgroud)
然后删除行使用dd
,如果我不想删除我可以去下一个匹配n
.
但有没有更快的方法来做到这一点!
下面的命令删除包含文本的所有行,但问题是它一次删除所有行,有时该文本在某行中是异常.
:g/text/d
Run Code Online (Sandbox Code Playgroud)
但我想要一些简单的东西
:%s/text/some_other_text/gc
Run Code Online (Sandbox Code Playgroud)
因为这提供了替代或不替代的选择.
Lie*_*ers 15
您不需要全局命令.替换命令本身就足够了
例
%s/.*text.*\n//gc
Run Code Online (Sandbox Code Playgroud)
你可以混合:help global
和:help substitute
:
:g/text/s/.*\n//c
Run Code Online (Sandbox Code Playgroud)
这将在删除包含text
以下内容的每一行之前要求确认:
我试图找到一种使用global
and 的方法:substitute
,它可以正确处理连续行的匹配和第一行的匹配,但唉,我没有受到启发。
所以,我回到了我的基础:我已经实现了我认为缺少的东西::confirm global
.
这个怎么运作:
global
在模式上执行,对于每场比赛,我检查用户希望做什么。
StatusLineNC
带有echo "\rmessage"
+的突出显示组:redraw
。这是一个非常古老的技巧,我们甚至在 Vim 6 IIRC 之前就使用过。相关代码如下:
" Function: lh#ui#ask(message) {{{3
function! lh#ui#ask(message) abort
redraw! " clear the msg line
echohl StatusLineNC
echo "\r".a:message
echohl None
let key = nr2char(getchar())
return key
endfunction
" Function: lh#ui#confirm_command(command) {{{3
" states:
" - ask
" - ignore
" - always
function! s:check() dict abort
if self.state == 'ignore'
return
elseif self.state == 'always'
let shall_execute_command = 1
elseif self.state == 'ask'
try
let cleanup = lh#on#exit()
\.restore('&cursorline')
\.restore_highlight('CursorLine')
set cursorline
hi CursorLine cterm=NONE ctermbg=black ctermfg=white guibg=black guifg=white
let choice = lh#ui#ask(self.message)
if choice == 'q'
let self.state = 'ignore'
let shall_execute_command = 0
" TODO: find how not to blink
redraw! " clear the msg line
elseif choice == 'a'
let self.state = 'always'
let shall_execute_command = 1
" TODO: find how not to blink
redraw! " clear the msg line
elseif choice == 'y'
" leave state as 'ask'
let shall_execute_command = 1
elseif choice == 'n'
" leave state as 'ask'
let shall_execute_command = 0
elseif choice == 'l'
let shall_execute_command = 1
let self.state = 'ignore'
endif
finally
call cleanup.finalize()
endtry
endif
if shall_execute_command
execute self.command
endif
endfunction
function! s:getSID() abort
return eval(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_getSID$'))
endfunction
let s:k_script_name = s:getSID()
function! lh#ui#make_confirm_command(command, message) abort
let res = lh#object#make_top_type(
\ { 'state': 'ask'
\ , 'command': a:command
\ , 'message': a:message . ' (y/n/a/q/l/^E/^Y)'
\ })
call lh#object#inject_methods(res, s:k_script_name, 'check')
return res
endfunction
" Function: lh#ui#global_confirm_command(pattern, command, message [, sep='/']) {{{3
" Exemple: to remove lines that match a pattern:
" > call lh#ui#global_confirm_command(pattern, 'd', 'delete line?')
function! lh#ui#global_confirm_command(pattern, command, message, ...) abort
let cmd = lh#ui#make_confirm_command(a:command, a:message)
let sep = get(a:, 1, '/')
exe 'g'.sep.a:pattern.sep.'call cmd.check()'
endfunction
" Function: lh#ui#_confirm_global(param) {{{3
function! lh#ui#_confirm_global(param) abort
let sep = a:param[0]
let parts = split(a:param, sep)
if len(parts) < 2
throw "Not enough arguments to `ConfirmGlobal`!"
endif
let cmd = join(parts[1:])
call lh#ui#global_confirm_command(parts[0], cmd, cmd . ' on line?', sep)
endfunction
command! -nargs=1 ConfirmGlobal call lh#ui#_confirm_global('<args>')
Run Code Online (Sandbox Code Playgroud)
从这里你可以输入:
:call lh#ui#global_confirm_command(pattern, 'd', 'delete line?')
:ConfirmGlobal/pattern/d
产生一个不太有指导意义的提示