2

I have some text lines as shown below

text1
text1
text1
text1

I want to change them to

text1
text2
text3
text4

Is there an easy way to do it in gvim. I know visual block with ctrl+v can replace one letter with other. But I dont know how to generate sequential numbers.

Help appreciated. Thanks

Marth
  • 23,920
  • 3
  • 60
  • 72
Vivek
  • 19
  • 1
  • actually in the question above each word "text1" is in a different line. Though I typed all the words in different lines, all words are shown in one line. – Vivek Mar 17 '16 at 11:01
  • The question is about editor, not on programming. It's off-topic. – Farside Mar 17 '16 at 11:12
  • Well then consider if instead of text1, text2, text3, etc. you wanted arr[0], arr[1], arr[2], etc. Programming! – Ben Mar 17 '16 at 15:06
  • Possible duplicate of [Vim - incremental numbering via regular expression search and replace](http://stackoverflow.com/questions/26681541/vim-incremental-numbering-via-regular-expression-search-and-replace) – imbichie Mar 18 '16 at 03:39

3 Answers3

5

You could select your text, starting from the second line, and use g<ctrl-a>.

From :h v_g_CTRL-A :

                            *v_g_CTRL-A*
{Visual}g CTRL-A    Add [count] to the number or alphabetic character in
                    the highlighted text. If several lines are
                    highlighted, each one will be incremented by an
                    additional [count] (so effectively creating a
                    [count] incrementing sequence).  {not in Vi}
                    For Example, if you have this list of numbers:
                        1.
                        1.
                        1.
                        1.
                    Move to the second "1." and Visually select three
                    lines, pressing g CTRL-A results in:
                        1.
                        2.
                        3.
                        4.
Marth
  • 23,920
  • 3
  • 60
  • 72
  • 2
    Note: this was added somewhere in the many 7.4.x patches, so you'll need to download a nightly build or Vim without Cream or something like that to have this feature. – Ben Mar 17 '16 at 15:08
  • 1
    @Ben The Vim site offers some more recent(ish) builds now as well (gvim74-1024 at the time of writing), not sure if this is new enough, though... – Martin Tournoij Mar 17 '16 at 17:15
1

You can try out this,

:for i in range(1,4) | put ='text'.i | endfor.

It works fine in vim without any plug-in.

Prakash Darji
  • 990
  • 6
  • 13
0

There is a pretty powerful plugin VisIncr, maybe you can try it.

Examples:

    :I
                Use ctrl-V to
    Original    Select, :I
       8            8
       8            9
       8            10
       8            11
       8            12

    :I -1
                Use ctrl-V to
    Original    Select, :I -1
       8            8
       8            7
       8            6
       8            5
       8            4
Tony
  • 354
  • 1
  • 3
  • 11