Ricky schrieb:
> Hi, All
>
> One line contains a number of same words:
> ---
> I've never saw a saw saw a saw
> She never saw a saw saw a saw
> ...
> He never saw a saw saw a saw
> ---
>
> I just want to replace the 2nd or 3rd "saw", Do you have any idea?
Example:
:%SedS/saw/foo/3
to replace the 3rd "saw" in a line.
------------------------------------------------------------
" idea from A. Politz, Jan 2008
com! -nargs=1 -range SedS <line1>,<line2>call SedS(<q-args>)
func! SedS( s_expr ) range
if a:s_expr !~ '\d\+$'
exec a:firstline.','.a:lastline.'s'.a:s_expr
return
endif
try
let sav_lz = &lz
set lazyredraw
call inputsave()
let nth = matchstr(a:s_expr,'\d\+$')+0
let fk_expr = repeat('n',nth-1)."yq\r"
let s_expr = strpart(a:s_expr,0,strlen(a:s_expr)-strlen(nth))
let nchanges = 0
for line in range(a:firstline,a:lastline)
call feedkeys(fk_expr)
exec 'sil' line.'s'. s_expr .'gce'
sil let nchanges += input("") !~ "y"
endfor
if nchanges > 0
call setpos("'[", [0,a:firstline,1,0])
call setpos("']", [0,a:lastline,1,0])
endif
if nchanges > &report
echo nchanges "substitutions on" nchanges "lines"
elseif nchanges == 0
echohl ErrorMsg
echomsg "SedS: Pattern not found:" @/
echohl none
endif
" return nchanges
finally
call inputrestore()
let &lz = sav_lz
endtry
endfunc
--
Andy
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---