1

I happen to come across the = register. I checked :h @= and :h recording where it is mentioned. I am not able to understand how to use it. It could be helpful to know a valid use-case for the same.

1
  • 1
    You can check in this video from 0:52 sec to 1:09 sec. It is a demo of my homework.
    – 3N4N
    Commented Mar 14, 2019 at 8:28

2 Answers 2

4

So let's begin by quoting the doc :h @=

When typing the '=' after " or CTRL-R the cursor moves to the command-line,
where you can enter any expression (see |expression|).  All normal
command-line editing commands are available, including a special history for
expressions.  When you end the command-line by typing <CR>, Vim computes the
result of the expression.  If you end it with <Esc>, Vim abandons the
expression.

And it is also worth reading :h usr_41.txt about the expressions.

Now how can you use the expression register?

It is important to understand the the expression register is not meant to be used in a macro, it more common usage is to insert the result of an expression while you are in insert mode.

For example you can do simple math. Let's get this in our buffer:

1 + 2 + 3 + 4 = 

If you want to put the result you can do the following:

0               To go the the beginning of the line
yf4             To yank the text from 1 to 4
A               To start inserting at the end of the line
<C-r>           Here the magic begins, this allow to insert the content of a register
=               To start editing the content of the expression register
<C-r>"          To insert your yanked text in the expression register
<CR>            To validate the operation

And voila vim computes the content of the expression register (i.e. 1 + 2 + 3 + 4) and inserts the result (i.e. 10)

1 + 2 + 3 + 4 = 10

Now the actual use cases depends on your usage but evaluating some vimscript while you are in insert mode can sometimes be useful.

2

Assume you often need to insert a timestamp in your documents. Then you could define a mapping for that:

:inoremap <C-G>t <C-R>=strftime("%Y-%m-%dT%H:%M:%S%z")<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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