How to make a global Vim: g / command work in each case

Usually the global Vim: g // command works on the basis of each line. Is it possible that this worked in each case, since there could be more than one occurrence in a line.

+3
source share
2 answers

Not a direct answer, but you can use something like: rubydo, which will execute some ruby ​​script on a line of code. Combining this with gsub in ruby ​​should give you the ability to do something in every match. Of course, you will need to do this with a ruby ​​code, which may not give you access to everything that you might need without the hassle (for example, adding registers would be annoying)

:[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,$".
0
source

:

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 )

0

Source: https://habr.com/ru/post/1748110/


All Articles