tags:

views:

88

answers:

2

Typically Vim's global command :g// works on per line basis. Is it possible to make it work on per occurrence basis as there could be more than one occurrence on a line.

A: 

Not a direct answer, but you could use something like :rubydo, which will run some ruby scriptlet per line of code. Combining that with gsub in ruby should get you the ability to do just about anything per occurrence of a match. Of course, you will need to do it with ruby code, which may not give you access to everything that you might need without hassle (register appending would be annoying for instance)

:[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,$".
David
A: 

You can try:

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

Which you can use as :global, except that the command name is different (and that the bang option as not been implemented yet)

Luc Hermitte