使用提示删除包含vim中某些文本的行的有效方法

Amr*_*aMe 13 vi vim

目前我可以搜索文字

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


rom*_*inl 8

你可以混合:help global:help substitute:

:g/text/s/.*\n//c
Run Code Online (Sandbox Code Playgroud)

这将在删除包含text以下内容的每一行之前要求确认:

在此输入图像描述


Luc*_*tte 6

我试图找到一种使用globaland 的方法:substitute,它可以正确处理连续行的匹配和第一行的匹配,但唉,我没有受到启发。

所以,我回到了我的基础:我已经实现了我认为缺少的东西::confirm global.

结果已推送到我的库插件中

这个怎么运作:

  1. 我准备了一个状态变量,它会在重要时记住之前的用户选择(always、或quitlast)。
  2. 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产生一个不太有指导意义的提示


ZMJ*_*ZMJ 5

最有效的方法是将 :glboal 和 :norm 结合起来

:g/test/norm dd
Run Code Online (Sandbox Code Playgroud)