7

I was messing with the expression register (:h @=) and noticed that it is not possible to put text directly in it. The doc says:

The expression register is read-only; you cannot put text into it.

Let's say my buffer contains an operation like that:

1+2+3+4

If I want to get the result in the expression register, I have to do the following:

  • yf4: Put the expression into the unnamed register
  • "=: Start editing the expression register
  • Ctrl-r"enter: Put the content of the unnamed register into the expression register

If the expression register weren't read only I could have done it more easily:

  • "=yf4 Select the expression register and put the line into it.

So my question is not about how I could change this behavior but what can explain this implementation?

Is it because yanking directly into the expression register would be "against the Vim way"? Is it because of a technical limitation? Is there another reason?


EDIT

According to the comments, the states of the expression register has changed recently:

My :version says:

enter image description here

And :h @=:

enter image description here

But @romainl and @lcd047 told me that the doc says the register is read-write. So I looked up the list of the patches but didn't find one about this change... I have to admit that I'm pretty confused by the situation now.

  • 1
    I have never used this. What's a typical use for "=? – muru Jun 7 '16 at 12:55
  • 2
    According to the amazing practical Vim it is used to do back-of-the-envelope calculations. What you put into this register is evaluated as a vimscript expression, so you can make simple arithmetic calculations for example. Here I was using it in a file containing several numbers to add and multiply so I was creating a macro to fill my file automatically with the results. – statox Jun 7 '16 at 13:06
  • 3
    @muru The main use is to apply functions in replacements, f.i. I'm doing something like this to shift page numbers in TOCs: :%s/\v\d+$/\=submatch(0)+9/. But it's also quite useful in other contexts, see Drew Neil's vimcast on it. – lcd047 Jun 7 '16 at 20:41
  • 2
    @statox :h @= from my Vim 7.4.1905 says "The expression register is read-write." – lcd047 Jun 7 '16 at 20:43
  • 2
    @lcd047 huh. I never realised that \= indicated a register. I always thought it was just Vim's way of doing what /e does in Perl. – muru Jun 7 '16 at 20:43
1

The expression register expects an expression. yf4 is not an expression so there's no reason to expect the expression register to make any sense of it.

If you want to use what you just yanked as an expression, use @".

| improve this answer | |
  • Well yf4 was just an example. In my case I just mean that a line in my buffer is an expression which can be accepted by the expression register. I think it is reasonable to imagine that a buffer may contain an expression to be processed. – statox Jun 7 '16 at 13:11
  • If you want to use the content of register a with the expression register, do =@a<CR><CR>. Use @" for the unnamed register. If you want to use the text on the current line, do =getline('.')<CR><CR>. – romainl Jun 7 '16 at 13:28
  • 2
    I understand that it is possible to put some text in the = register without using <c-r>register but my question was about why the creators of Vim made it read only and not about the different ways to set its content. Still thank you for taking time to write an answer. – statox Jun 7 '16 at 13:41
  • 6
    The expression register is not "read-only". It's "read-write": you "write" an expression in that register and you "read" the output of that expression. The only difference with more regular registers like "a or "/ is that what you read from "= is usually different from what you write into it. – romainl Jun 7 '16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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