如何制作Vim的全局命令:g /每次出现的工作

时间:2010-06-02 08:22:18

标签: vim

通常Vim的全局命令:g //基于每行工作。是否可以使其在每次出现的基础上工作,因为一行上可能出现多次。

2 个答案:

答案 0 :(得分:0)

不是直接答案,但你可以使用类似:rubydo,它将在每行代码中运行一些ruby scriptlet。将它与ruby中的gsub结合使用可以让你在每次匹配时都能做任何事情。当然,您需要使用ruby代码来执行此操作,这可能无法让您轻松访问您可能需要的所有内容(例如,注册附加会令人讨厌)

:[range]rubyd[o] {cmd}  Evaluate Ruby command {cmd} for each line in the
                        [range], with $_ being set to the text of each line in
                        turn, without a trailing <EOL>.  Setting $_ will change
                        the text, but note that it is not possible to add or
                        delete lines using this command.
                        The default for [range] is the whole file: "1,$".

答案 1 :(得分:0)

您可以尝试:

command! -bang -nargs=1 -range OGlobal 
    \ <line1>,<line2>call s:Global("<bang>", <f-args>)

function! s:Global(bang, param) range
  let inverse = a:bang == '!'

  " obtain the separator character
  let sep = a:param[0]
  " obtain all fields in the initial command
  let fields = split(a:param, sep)

  " todo: handle inverse
  let l = a:firstline
  while 1
    let l = search(fields[0], 'W')
    if l == -1 || l > a:lastline 
      break 
    endif
    exe fields[1]
  endwhile
endfunction

您可以将其用作:global,但命令名称不同(并且bang选项尚未实现)