Home » Questions » Computers [ Ask a new question ]

Is it possible to combine :%s/ \= and | in vim?

Is it possible to combine :%s/ \= and | in vim?

The following is roughly what I am trying to do in vim

Asked by: Guest | Views: 208
Total answers/comments: 2
Guest [Entry]

"You can use :global as its command is executed for every line.

:let g:ID = 1
:g//s//\=g:ID/ | let g:ID = g:ID + 1

If you want to constrain the replacement, either limit by the :global pattern, or prefix :global with a range, or both.

:10,20g/foo/s/^/\=g:ID/ | let g:ID = g:ID + 1

That should prepend the incremented g:ID for every line that matches foo from line 10 to 20 (inclusive)."
Guest [Entry]

"Try <bar> instead of |
Have a look at: http://vim.wikia.com/wiki/Best_Vim_Tips"