Tuesday, July 26, 2011

Re: unexpected behavior of :let-@

Reply to message «unexpected behavior of :let-@»,
sent 20:36:08 26 July 2011, Tuesday
by Ben Fritz:

Not related to documentation fix, but you can use `setreg()' to avoid this
behavior.

By the way, while
let @/=""
clears the last search pattern, using `n' for some reason results in
E486: Pattern not found: ^M

Original message:
> 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."

No comments: