Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. <block>
  2. <foo val="bar"/>
  3. <foo val="bar"/>
  4. </block>
  5. <block>
  6. <foo val="bar"/>
  7. <foo val="bar"/>
  8. </block>
  9.  
  10. <block>
  11. <foo val="bar1"/>
  12. <foo val="bar"/>
  13. </block>
  14. <block>
  15. <foo val="bar1"/>
  16. <foo val="bar"/>
  17. </block>
  18.  
  19. :let a = ['', '1']
  20. :%s/barzs/=reverse(a)[0]/g
  21.  
  22. :let a = ['a', 'b', 'c']
  23. :%s/barzs/=add(a, remove(a, 0))[-1]/g
  24.  
  25. :h :s
  26. :h range
  27. :h /zs
  28. :h :s=
  29. :h reverse(
  30. :h :s_flags
  31. :h Lists
  32. :h add(
  33. :h remove
  34.  
  35. :%s/bar/bar1/gc
  36.  
  37. qv start recording in register v
  38. /"bar"/e<cr> search for "bar" and position the cursor at the end of the match
  39. i1<esc> insert 1 before the cursor and go back to normal mode
  40. n jump to next match
  41. q stop recording
  42.  
  43. :%s/bar(.*)n(.*)bar/bar11r2bar
  44.  
  45. :let dosubs=1
  46. :%s/bar/=[dosubs?'bar1':submatch(0),extend(g:,{'dosubs':!dosubs})][0]/g
  47.  
  48. :%SubstituteSelected/<bar>/&1/ yn
  49.  
  50. ":[range]SubstituteSelected/{pattern}/{string}/[flags] {answers}
  51. " Replace matches of {pattern} in the current line /
  52. " [range] with {string}, determining whether a particular
  53. " match should be replaced on the sequence of "y" or "n"
  54. " in {answers}. I.e. with "ynn", the first match is
  55. " replaced, the second and third are not, the fourth is
  56. " again replaced, ...
  57. " Handles & and , 1 .. 9 in {string}.
  58. function! CountedReplace()
  59. let l:index = s:SubstituteSelected.count % len(s:SubstituteSelected.answers)
  60. let s:SubstituteSelected.count += 1
  61.  
  62. if s:SubstituteSelected.answers[l:index] ==# 'y'
  63. if s:SubstituteSelected.replacement =~# '^\='
  64. " Handle sub-replace-special.
  65. return eval(s:SubstituteSelected.replacement[2:])
  66. else
  67. " Handle & and , 1 .. 9 (but not u, U, n, etc.)
  68. let l:replacement = s:SubstituteSelected.replacement
  69. for l:submatch in range(0, 9)
  70. let l:replacement = substitute(l:replacement,
  71. '%(%(^|[^\])%(\\)*\)@<!' .
  72. (l:submatch == 0 ?
  73. '%(&|\'.l:submatch.')' :
  74. '\' . l:submatch
  75. ),
  76. submatch(l:submatch), 'g'
  77. )
  78. endfor
  79. return l:replacement
  80. endif
  81. elseif s:SubstituteSelected.answers[l:index] ==# 'n'
  82. return submatch(0)
  83. else
  84. throw 'ASSERT: Invalid answer: ' . string(s:SubstituteSelected.answers[l:index])
  85. endif
  86. endfunction
  87. function! s:SubstituteSelected( range, arguments )
  88. let l:matches = matchlist(a:arguments, '^(i@!S)(.*)%(%(^|[^\])%(\\)*\)@<!1(.*)%(%(^|[^\])%(\\)*\)@<!1(S*)s+([yn]+)$')
  89. if empty(l:matches)
  90. echoerr 'Invalid arguments'
  91. return
  92. endif
  93. let s:SubstituteSelected = {'count': 0}
  94. let [l:separator, l:pattern, s:SubstituteSelected.replacement, l:flags, s:SubstituteSelected.answers] = l:matches[1:5]
  95.  
  96. execute printf('%ssubstitute %s%s%s=CountedReplace()%s%s',
  97. a:range, l:separator, l:pattern, l:separator, l:separator, l:flags
  98. )
  99. endfunction
  100. command! -bar -range -nargs=1 SubstituteSelected call <SID>SubstituteSelected('<line1>,<line2>', <q-args>)
  101.  
  102. v%(block>_{-})zsbar
  103. %s,,&1,g
  104.  
  105.  
  106.  
  107. v ............ very magic (avoid lots of scapes)
  108. % ............ ignore whats flows
  109. ( ............ starts (ignored) group
  110. _ ............ multiline search
  111. .{-} .......... non-greedy
  112. zs ........... start pattern for substituition
  113. bar .......... pattern we want to change
  114.  
  115. % ............. whole file
  116. s ............. substituition
  117. ,, ............ use last search (could be //)
  118. & ............. use searched pattern
  119. 1 ............. add 1 after it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement