Vim Tips Wiki

Repeating a substitute from current cursor position

Revision as of 15:32, November 8, 2011 by Chrisbra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,619pages on
this wiki
Tip 573 Printable Monobook Previous Next

created October 3, 2003 · complexity intermediate · author Charles E. Campbell, Jr. · version 6.0

The :RS /pattern/subpattern/{flags} command+function as shown below allows one to repeat a RS-substitute after the current cursor position.

For example:

" The cursor position is indicated with '^' on next line.
one two three one two three one two three

:RS /two/TWO/
one TWO three one two three one two three

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

(I think tip author intended to repeat :RS here?)

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

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
  let curcol= col(".")
  let sep = strpart(g:repeatsubst,0,1)
  let pat = substitute(g:repeatsubst,'^.\(.\{-}\)'.sep.'.*$','\1','')
  let curcol= curcol + matchend(getline("."),pat)
  exe "s".g:repeatsubst
  norm! k
  exe 'norm! '.curcol.'|'


Around Wikia's network

Random Wiki