Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 4 lines.

foo ["bar"]
baz ["k"]
daskhfkjhadsfj ["askfjhfjdsh"]
dfjhafkjhfha ["skkksks"]

I'd like to do a macro that takes the word and yanks it into a register, but I want the register to increment. e.g.:

f"l"ayf"j0
f"l"byf"j0
f"l"cyf"j0
f"l"dyf"j0

Is there a way to get this command done in such a way that the registers autoincrement, so I can later paste them:

jjjj
f"ap   
j
f"bp

I'm aware of register numbering and yi" - i'd like to use this with alpha registers instead. I'm also aware that a collision might occur with my chosen register: You can assume that my macro will be recorded in z and I want to use, say, a-f for registers used.

Can you suggest a way to get this accomplished using a macro?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

With a macro? Maybe. With two macros, sure!

First some setup:

:set nrformats=alpha
:let @y="\"a"

And now some crazy Vim register voodoo:

qzf"l@yyf"GoEsc"yp^A0"yy$dd^Oj0q

The strategy is to use one macro (y) to keep track of which register to use next, and modify it using the other macro.

  • f"l@yyf" is from your original, only I've used @y in place of "a. We're using the y macro to type "a instead of typing it directly.
  • GoEsc"yp goes to the end of the file, adds a new line there, and pastes the contents of register y.
  • ^A increments the letter under the cursor. If it was a, now it's b. We set nrformats=alpha earlier so that this would work with letters.
  • 0"yy$ yanks the updated text back into register y.
  • dd^O cleans up our scratch line and jumps back to where we were before, and
  • j0 is the rest of your original.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.