Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <block>
- <foo val="bar"/>
- <foo val="bar"/>
- </block>
- <block>
- <foo val="bar"/>
- <foo val="bar"/>
- </block>
- <block>
- <foo val="bar1"/>
- <foo val="bar"/>
- </block>
- <block>
- <foo val="bar1"/>
- <foo val="bar"/>
- </block>
- :let a = ['', '1']
- :%s/barzs/=reverse(a)[0]/g
- :let a = ['a', 'b', 'c']
- :%s/barzs/=add(a, remove(a, 0))[-1]/g
- :h :s
- :h range
- :h /zs
- :h :s=
- :h reverse(
- :h :s_flags
- :h Lists
- :h add(
- :h remove
- :%s/bar/bar1/gc
- qv start recording in register v
- /"bar"/e<cr> search for "bar" and position the cursor at the end of the match
- i1<esc> insert 1 before the cursor and go back to normal mode
- n jump to next match
- q stop recording
- :%s/bar(.*)n(.*)bar/bar11r2bar
- :let dosubs=1
- :%s/bar/=[dosubs?'bar1':submatch(0),extend(g:,{'dosubs':!dosubs})][0]/g
- :%SubstituteSelected/<bar>/&1/ yn
- ":[range]SubstituteSelected/{pattern}/{string}/[flags] {answers}
- " Replace matches of {pattern} in the current line /
- " [range] with {string}, determining whether a particular
- " match should be replaced on the sequence of "y" or "n"
- " in {answers}. I.e. with "ynn", the first match is
- " replaced, the second and third are not, the fourth is
- " again replaced, ...
- " Handles & and , 1 .. 9 in {string}.
- function! CountedReplace()
- let l:index = s:SubstituteSelected.count % len(s:SubstituteSelected.answers)
- let s:SubstituteSelected.count += 1
- if s:SubstituteSelected.answers[l:index] ==# 'y'
- if s:SubstituteSelected.replacement =~# '^\='
- " Handle sub-replace-special.
- return eval(s:SubstituteSelected.replacement[2:])
- else
- " Handle & and , 1 .. 9 (but not u, U, n, etc.)
- let l:replacement = s:SubstituteSelected.replacement
- for l:submatch in range(0, 9)
- let l:replacement = substitute(l:replacement,
- '%(%(^|[^\])%(\\)*\)@<!' .
- (l:submatch == 0 ?
- '%(&|\'.l:submatch.')' :
- '\' . l:submatch
- ),
- submatch(l:submatch), 'g'
- )
- endfor
- return l:replacement
- endif
- elseif s:SubstituteSelected.answers[l:index] ==# 'n'
- return submatch(0)
- else
- throw 'ASSERT: Invalid answer: ' . string(s:SubstituteSelected.answers[l:index])
- endif
- endfunction
- function! s:SubstituteSelected( range, arguments )
- let l:matches = matchlist(a:arguments, '^(i@!S)(.*)%(%(^|[^\])%(\\)*\)@<!1(.*)%(%(^|[^\])%(\\)*\)@<!1(S*)s+([yn]+)$')
- if empty(l:matches)
- echoerr 'Invalid arguments'
- return
- endif
- let s:SubstituteSelected = {'count': 0}
- let [l:separator, l:pattern, s:SubstituteSelected.replacement, l:flags, s:SubstituteSelected.answers] = l:matches[1:5]
- execute printf('%ssubstitute %s%s%s=CountedReplace()%s%s',
- a:range, l:separator, l:pattern, l:separator, l:separator, l:flags
- )
- endfunction
- command! -bar -range -nargs=1 SubstituteSelected call <SID>SubstituteSelected('<line1>,<line2>', <q-args>)
- v%(block>_{-})zsbar
- %s,,&1,g
- v ............ very magic (avoid lots of scapes)
- % ............ ignore whats flows
- ( ............ starts (ignored) group
- _ ............ multiline search
- .{-} .......... non-greedy
- zs ........... start pattern for substituition
- bar .......... pattern we want to change
- % ............. whole file
- s ............. substituition
- ,, ............ use last search (could be //)
- & ............. use searched pattern
- 1 ............. add 1 after it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement