sponsor Vim development Vim logo Vim Book Ad

increment_new.vim : Subsitute a pattern with incremented values ENHANCED

 script karma  Rating 34/10, Downloaded by 1251  Comments, bugs, improvements  Vim wiki

created by
Ely Schoenfeld
 
script type
utility
 
description
This is a great (I think) improvement of the plugin increment.vim made by William Natter (natter at magma.ca) (vimscript #842).

I tried to contact William, but his mailbox does not exist... So here it goes.

As William wrote:
"This is a handy plugin that makes easy replacing your favorite pattern ("@" by default) with a sequence of incremented values using the "Inc" command.  "

Besides what the original script can do, it has two new features:
    a) Now you are able to search for a pattern and replace the Nth word from the beginning of the line with the incremental number.
    b) And also you can get a confirmation message before change.

The help file is also greatly improved.

Here is an example:

       You have: (beginning with spaces only)
           ----- cut -----
                   LMsgInstall6 = 7; // @
                 LMsgInstall7 = 8;   // @
               LMsgInstall8 = 9;     // @

               {STANDALONE}
               LErrConnectionStandAlone1 = 10; //THIS
               LErrConnectionStandAlone3 = 7; //THIS
               LErrConnectionStandAlone5 = 12; //THIS
           {some text}
               LMsgExitConfirm = 30;
           ...
           ----- cut -----

       You do: (with confirmation)
           :'<,'>Inc p^\ *\\\<\[A-Za-z0-9_]*\\\>\ =\ [0-9]* w3 i25 s100 c
                                   (Note that I had to use tree "\" before the "<" sign. Since Vim 7 the "<" and ">" have to be skiped by a "\")

       You get:
           ----- cut -----
                   LMsgInstall6 = 100; // @
                 LMsgInstall7 = 125;   // @
               LMsgInstall8 = 150;     // @

               {STANDALONE}
               LErrConnectionStandAlone1 = 175; //THIS
               LErrConnectionStandAlone3 = 200; //THIS
               LErrConnectionStandAlone5 = 225; //THIS
           {some text}
               LMsgExitConfirm = 250;
           ...
           ----- cut -----

       Then you do: (without confirmation)
           :'<,'>Inc pTHIS w3 i3 s5

       You get:
           ----- cut -----
                   LMsgInstall6 = 100; // @
                 LMsgInstall7 = 125;   // @
               LMsgInstall8 = 150;     // @

               {STANDALONE}
               LErrConnectionStandAlone1 = 5; //THIS
               LErrConnectionStandAlone3 = 8; //THIS
               LErrConnectionStandAlone5 = 11; //THIS
           {some text}
               LMsgExitConfirm = 250;
           ...
           ----- cut -----

       Then you do: (without confirmation)
           :'<,'>Inc

       You get:
           ----- cut -----
                   LMsgInstall6 = 100; // 0
                 LMsgInstall7 = 125;   // 1
               LMsgInstall8 = 150;     // 2

               {STANDALONE}
               LErrConnectionStandAlone1 = 5; //THIS
               LErrConnectionStandAlone3 = 8; //THIS
               LErrConnectionStandAlone5 = 11; //THIS
           {some text}
               LMsgExitConfirm = 250;
           ...
           ----- cut -----

Hope it helps.

Keywords: Inc Increment increment increment.vimnumber numbers num replace substitute subst pattern regex
 
install details
- Put increment_new.vim into a plugin directory.
- Put increment_new.txt into a doc directory.

  (see :set runtimepath)

For example in *nix OS:
    - The plugin directory should be: ~/.vim/plugin
    - The doc directory should be: ~/.vim/doc

    Something like:
        mkdir ~/.vim
        cd ~/.vim
        tar -zxvf increment_new.tar.gz

For example in Windows:
    - The plugin directory should be: %PROGRAMFILES%\Vim\vimfiles\plugin
    - The doc directory should be: %PROGRAMFILES%\Vim\vimfiles\doc

    (To extract the files from a tar.gz in windows, I use a great program called 7Zip (http://www.7-zip.org)

-Install Help
    To be able to do:
        :help Inc
    in vim, you have to do, for example:
        :helptags ~/.vim/doc
or
        :helptags C:\Program Files\Vim\vimfiles\doc

 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
increment_new.tar.gz 1.1 2007-06-23 7.0 Ely Schoenfeld Changes in documentation for better installation instructions and since something changed in the regular expression matching in Vim7.
Now I have to escape the "<" and ">" with "\"
increment_new.tar.gz 1.0 2005-02-04 6.0 Ely Schoenfeld Initial upload
ip used for rating: 3.135.198.49

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github