History Report a problem
Article Edit this page Discussion

Step increment and replace

From Vim Tips Wiki

(Redirected from VimTip1114)
Jump to: navigation, search

Tip 1114 Previous TipNext Tip

Created: January 25, 2006 Complexity: intermediate Author: Gerald Lai Minimum version: 6.0 Karma: 13/7 Imported from: Tip#1114


This allows a search, and step-by-step confirm & replace, while having the replace string incremented each time if it contains a number at the end of the string. To search for occurrences of "abc", to replace with "xyz0" for the first "abc", "xyz1" for the second "abc", etc, simply do:

:SReplace abc xyz0 1

At any step, you can change from "xyzN" to "defN", and pick up where you left off with the same increment.

Usage: SReplace <search> <substitute> <increment>
command -nargs=+ SReplace call StepReplace(<f-args>)
"makes use of register y
function StepReplace(...)
  if a:0 == 1
    let @y = input("Replace with: ", @y)
    let y = @y
    if @y =~ "\\d\\+$"
      let n = substitute(@y, ".\\{-}\\(\\d\\+\\)$", "\\1", "") + a:1
      let @y = substitute(@y, "\\(.\\{-}\\)\\d\\+$", "\\1".n, "")
    endif
    return y
  elseif a:0 == 3
    let @y = a:2
    execute "%s/".a:1."/\\=StepReplace(".a:3.")/".(&gdefault ? "" : "g")."c"
  else
    echo "Usage: SReplace <search> <substitute> <increment>"
  endif
endfunction

[edit] Comments


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Step increment and replace"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation