unexpected behavior of :let-@

687 views
Skip to first unread message

Ben Fritz

unread,
Jul 26, 2011, 12:36:08 PM7/26/11
to vim_use
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."

ZyX

unread,
Jul 26, 2011, 1:57:07 PM7/26/11
to vim...@googlegroups.com
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:

signature.asc

Benjamin R. Haskell

unread,
Jul 26, 2011, 2:05:57 PM7/26/11
to vim...@googlegroups.com
On Tue, 26 Jul 2011, ZyX wrote:

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

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

Interesting. I get that under Gentoo. Under OpenSUSE I get:
E35: No previous regular expression

I also get E35 under Gentoo with -u NONE -i NONE -N

Something weird with a default plugin?

--
Best,
Ben

ZyX

unread,
Jul 26, 2011, 2:28:06 PM7/26/11
to vim...@googlegroups.com
Reply to message «Re: unexpected behavior of :let-@»,
sent 22:05:57 26 July 2011, Tuesday
by Benjamin R. Haskell:

It is a last substitute search pattern which causes this behavior. I forgot that
there are two «last search» patterns that vim remembers. Script to reproduce:
vim -u NONE -i NONE -s <(<<< $'/abc\n:s/-//e\n:let @/=""\nn')

Original message:

signature.asc

Ben Fritz

unread,
Jul 26, 2011, 2:40:24 PM7/26/11
to vim_use


On Jul 26, 12:57 pm, ZyX <zyx....@gmail.com> wrote:
> 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.
>

Nice! I'd forgotten about setreg(), finding it completely unnecessary
after being pointed to :let-@. I guess I'll be revisiting it. I do see
that for it to work as I want, I need to explicitly give the 'c' flag
as the type.

:help setreg() indicates that you should be able to change the type of
a register which already has content:

You can also change the type of a register by appending
nothing:
:call setreg('a', '', 'al')

But, I tried:

:let @a='^M'
:reg a → ^M^J
:call setreg('a','','ac')
:reg a → ^M^J

Did I misunderstand how this is supposed to work? I would expect the
^J to be removed after changing the type.
Reply all
Reply to author
Forward
0 new messages