1

I want to put a specified number after each pasted line automatically and this number will increment every past.

Sometimes I want to declare many variables. So I write one declaration and copy(yank) it and past to a second line. Then I can repeat this operation with dot operator ".". But it's very annoying to make these variables vary.

I want to achieve something like this:

variable (yy)     variable1 (yy)
variable (p)      variable2 (p)
variable (.)  =>  variable3 (.)
variable (.)      variable4 (.)
variable (.)      variable5 (.)

Is it possible to perform such a operation in just the vim?

Miszo97
  • 303
  • 2
  • 11

3 Answers3

1

I don't know if someone can do it with ultisnips interpolation, I think it is possible but I came up with this solution

yy4p .......... copy 4 times
v} ............ select block
g<Ctrl-a> ..... increase the sequence

Until here you have the main solution that is increasing the numbers fast

fy ............ jump to the first 'y'
<Ctrl-v> ...... start selection block
3jl ........... extend selection
c.<Esc> ....... swich 'yy' with .
fy. ........... finishes

Using macro

qa ............ start recording marcro 'a'
yyp ........... copy line
ci(.<Esc> ..... change first ()
Ctrl-a ........ increase
ci(. .......... change second ()
Esc ........... stop recording macro 'a'
3@a ........... 4x macro 'a'
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
0
:%s/variable/\= printf("variable%d",line('.')-6)/

or if you have a vim with perl support (default in many linux distributions)

:perldo s/variable/$& . ++$n/e
JJoao
  • 4,891
  • 1
  • 18
  • 20
0

My UnconditionalPaste plugin has gpp and gPp mappings for this (and many more paste variations). The first only increments the first decimal number, the second one all numbers.

So, starting with

variable1 = 'foo1'

yygpp will create:

variable1 = 'foo1'
variable2 = 'foo1'

whereas yygPp will do this:

variable1 = 'foo1'
variable2 = 'foo2'

These support a [count] and can be repeated with ., too.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324