Sunday, August 26, 2012

Re: Help me understand @. macros

Am 20.08.2012 22:58, schrieb cybrown:
> I'm a relatively new VIM user, and I've been playing vimgolf (www.vimgolf.com) to learn new skills. This challenge has a bizarre solution that I don't understand:
> http://www.vimgolf.com/challenges/4d1db1b8de2f897c2a00014a
>
> Here's the shortest solution posted:
> a.<BS><CR><Esc>24@.ZZ
>
> First off, I didn't realize @. was a valid macro combination. So, my questions:
> 1) Is @. any different than .? Why would I want to use one instead of the other?
> 2) What's going on with this solution? Why does it work but:
> a<CR><Esc>24@.ZZ
> doesn't work? What's so special about inserting the dot specifically?

All in all, it's a tricky one; in order to congrat the author I'll give a lengthy explanation :-)

You basically want
a<CR><Esc>....... (more dots)
and you try to use a count to make it shorter:
a<CR><Esc>24.
but above line doesn't work, because
24a<CR><Esc>
(which is the equivalent command) inserts a lot of empty lines.
So you wrap the `.' into a macro (here into register `q') and apply the count to the macro call:
a<CR><Esc>qq.q23@q
but this is too long.
The following saves one keystroke:
qqa<CR><Esc>q24@q
with final `ZZ' this is 12 keystrokes, but the winner still needs one less keystroke.
He implicitly records into the `.' register by typing `.' in Insert mode ("recording" is later stopped with `<Esc>');
the dot is not wanted for the line break, so he also hits `<BS>'.
`@.' is executed in Normal mode where the sequence `.<BS><CR>' gets a different meaning.
For this special case, `<BS><CR>' is a no-op, so `24@.' executes `.' 24 times in a row, with the intended result.

--
Andy

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: