Vim Tips Wiki
Register
Advertisement

Previous TipNext Tip

Tip: #573 - Repeating a substitute from current cursor position

Created: October 3, 2003 7:30 Complexity: intermediate Author: Charles E. Campbell, Jr. Version: 6.0 Karma: 4/3 Imported from: Tip#573

The :RS /pattern/subpattern/{flags} command+function as shown

below allows one to repeat a RS-substitute after the current

cursor position.


Ex. 
cursor starts... 
one two three one two three one two three 
o^here 


:RS /two/TWO/ 
one TWO three one two three one two three 
one TWO^cursor ends up here 


move cursor: 
one TWO three one two three one two three 
one TWO three one two ^ 


cursor ends up: 
one TWO three one two three one TWO three 
one TWO three one two three one TWO^ 


(normally I'd have left the characters preceding the ^ as blanks but I'm 
trying to avoid problems with proportional fonts) 


Put the following into your <.vimrc> if you'd like to be able to do this:


" ---------------------------------------------------------------------

" RS: repeat substitution command

com! -range -nargs=* RS call RepeatSubst(<q-args>)


" RepatSubst:

fun! RepeatSubst(subexpr)

if a:subexpr != "" 
let g:repeatsubst= a:subexpr 
endif 
let curcol= col(".") 
let sep = strpart(g:repeatsubst,0,1) 
let pat = substitute(g:repeatsubst,'^.\(.\{-}\)'.sep.'.*$','\1',) 
s/\%#./\r&/ 
let curcol= curcol + matchend(getline("."),pat) 
exe "s".g:repeatsubst 
norm! k 
j! 
exe 'norm! '.curcol.'

Comments

Advertisement