Tuesday, July 26, 2011

unexpected behavior of :let-@

It took me quite some time to figure this out, so I thought I'd share.
Perhaps someone knows a good workaround.

:help :let-@ says

If the result of {expr1} ends in a <CR> or <NL>, the
register will be linewise, otherwise it will be set to
characterwise.

This makes sense, however it has an unfortunate side effect when it
comes to editing recorded macros. The best example is probably as
follows:

qa:print<Enter>q → record a simple macro into the 'a' register, to
print the current line
@a → note that running the macro prints the current line, and does
nothing else
:reg a → prints ":print^M"
:let @a='<C-R><C-R>a' → how you would normally recall the 'a' register
for editing

Instead of editing the 'a' register, just hit <Enter> again to assign
'a' to its current contents. However, note that the register 'a'
contains text ending in <CR>, therefore the register becomes linewise.
So:

:reg a → prints ":print^M^J"
@a → prints the current line AND moves down a line

As a workaround, I can add an "a<Esc>" to the end of the macro, so
that it no longer ends in <CR>, but is there a better way?

Perhaps the :help needs an update? We could add text after the
"register will be linewise..." sentence, which says "This means
assigning a register to use as a recorded macro will not work as
intended if the contents end in a <CR> or <NL> character."

--
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: