Vim Tips Wiki
Register
No edit summary
 
m (Reverted edits by Emprego.curitiba (talk | block) to last version by JohnBot)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=573
 
|id=573
  +
|previous=572
|title=Repeating a substitute from current cursor position
 
  +
|next=575
|created=October 3, 2003 7:30
+
|created=October 3, 2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Charles E. Campbell, Jr.
 
|author=Charles E. Campbell, Jr.
 
|version=6.0
 
|version=6.0
 
|rating=4/3
 
|rating=4/3
  +
|category1=
|text=
 
  +
|category2=
The :RS /pattern/subpattern/{flags} command+function as shown
 
 
}}
 
The :RS /pattern/subpattern/{flags} command+function as shown below allows one to repeat a RS-substitute after the current cursor position.
   
  +
For example:
below allows one to repeat a RS-substitute after the current
 
  +
<pre>
  +
" The cursor position is indicated with '^' on next line.
 
one two three one two three one two three
  +
^
   
 
:RS /two/TWO/
cursor position.
 
 
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:
Ex.
 
 
one TWO three one two three one TWO three
  +
</pre>
   
 
Put the following into your vimrc if you'd like to be able to do this.
cursor starts...
 
   
  +
<pre>
one two three one two three one two three
 
 
" RS: repeat substitution command
 
 
com! -range -nargs=* RS call RepeatSubst(&lt;q-args>)
o^here
 
 
" RepatSubst:
 
 
fun! RepeatSubst(subexpr)
 
 
if a:subexpr != ""
 
 
let g:repeatsubst= a:subexpr
:RS /two/TWO/
 
  +
endif
 
 
let curcol= col(".")
one TWO three one two three one two three
 
 
let sep = strpart(g:repeatsubst,0,1)
 
 
let pat = substitute(g:repeatsubst,'^.\(.\{-}\)'.sep.'.*$','\1','')
one TWO^cursor ends up here
 
 
s/\%#./\r&/
 
 
let curcol= curcol + matchend(getline("."),pat)
 
 
exe "s".g:repeatsubst
 
 
norm! k
move cursor:
 
  +
j!
 
 
exe 'norm! '.curcol.'|'
one TWO three one two three one two three
 
 
endfun
 
  +
</pre>
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 &lt;.vimrc&gt; if you'd like to be able to do this:
 
 
 
 
" ---------------------------------------------------------------------
 
 
" RS: repeat substitution command
 
 
com! -range -nargs=* RS call RepeatSubst(&lt;q-args&gt;)
 
 
 
 
" 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/\%&#35;./\r&amp;/
 
 
let curcol= curcol + matchend(getline("."),pat)
 
 
exe "s".g:repeatsubst
 
 
norm! k
 
 
j!
 
 
exe 'norm! '.curcol.'|'
 
 
endfun
 
 
" ---------------------------------------------------------------------
 
 
 
}}
 
   
== Comments ==
+
==Comments==
<!-- parsed by vimtips.py in 0.547058 seconds-->
 

Latest revision as of 15:32, 8 November 2011

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
  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.'|'
endfun

Comments[]