25

Just watched this http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-advanced-tutorial-videos/#expression-register, and I can't see any benefit to using <c-r>= vs <c-o>normal or something like that (although I'm sure that is just because I am not understanding something)

2 Answers 2

42

It has many interesting uses, many many. In Derek's video, it was used to call an external script. The point is that you can do that without leaving insert mode. You said you don't see benefits over <c-o>, but how would you to that?

The expression register inserts the result from an expression. You don't need to :call a function for example, as demonstrated in the video. I'll try to show you another two uses that I find interesting, and use frequently.

1.Evaluating Math

The expression 2 evaluates to 2, right (like in VimScript)? So you can use the expression register to insert some numbers as result from a calculation. For example, considering you're in insert mode in this file:

... the total sum is $

Now hit <c-r>= and type

5*6+3.2*8+5.52<enter>

And the result is:

... the total sum is $61.12

Practical, eh?

2.Using Variable Values

Let's say you need to number headings in a text. Headings start with a # like in:

# Heading

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.

# Another Heading

Consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

Considering it's a long list, you'd need to find a way to do it quickly. Here is one approach:

  1. Create a variable to hold the current value
  2. Find the next heading
  3. Insert the contents of that variable (using the expression register)
  4. Increment the variable

This can be done with by first creating the variable:

:let i=1

And then with a macro:

qa            ; start recording
/^#<CR>       ; find next heading
0w            ; move to the first word
i             ; switch to insert mode
<c-r>=i<CR>   ; insert the number
.<esc>        ; insert a dot and back to normal mode
:let i+=1<CR> ; increment the variable
q             ; stop recording

And now you can press @a and use @@ to subsequently insert the numbers in your headings.

2
  • I was thinking something like <c-o>:normal system('script'), but thinking about it again, that will do the call but won't insert the results. <c-o>r!script would work, but as you said, it is more about arbitrary VimL then it is about executing external stuff. Great answer, thanks :) Commented Aug 11, 2011 at 16:54
  • FWIW ^A = increment, which provides a simpler way to number things (although does not demonstrate "=)
    – lxs
    Commented Sep 20, 2013 at 8:36
12

I use the expression register like this: <C-r>=618+27<CR>. It's very useful when doing CSS.

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.