2

I want a shortcut for evaluating a yanked expression and pasting the result after the cursor.

Suppose I've written i1+1<ESC>0y$$, now I'd like to paste the result of that, 2, after the cursor. Is that possible?

I can take that expression and replace it using bc, e.g. i1+1<ESC>!!bc<CR>, but that's not what I want (although it's quite neat).

I can evaluate expressions and insert them into the text using i<C-R>=1+1<CR>. I have set clipboard=unnamedplus so I could take my yanked expression and paste it with ctrl+shift+v into the i<C-R>= prompt, and then I pretty much have what I want.

Now then, how do I create a shortcut of that, i.e. of (pseudo) i<C-R>=<C-Shift-V><CR>?

If you know any other way of doing this sort of thing then that's fine too, thanks.

  • 1
    That works, thanks! I guess I'll see which register will make most sense to use, but anyway, using <C-R>. or other register inside the expression prompt was the key. noremap == i<C-R>=<C-R>+<ESC> was pretty much what I needed (I'll tweak it as I go along). Make an answer out of your suggestion and I'll accept it. – Erik Vesterlund Feb 15 at 16:45
2

You can use <C-R> from the prompt of the = expression to insert the contents of a register. For example, <C-R>" for the contents of the default register, which is where your yanked expression is stored.

This would work to insert the result of the expression in the default register:

i<C-R>=<C-R>"<CR><Esc>

You can also use <C-R>. for your last insertion (so you don't even need to yank an expression.) After i1+1<Esc>, you can use i<C-R>=<C-R>.<CR><Esc> to insert the result of the inserted expression at the current location.

|improve this answer|||||
  • 1
    ”=<C-R>”<CR>p is a tiny bit shorter/more direct than entering insert mode. – Rich Feb 15 at 19:34
  • 1
    Nice use of the dot register! – Rich Feb 15 at 19:42
  • @Rich Those right double quotation marks probably won't work well as a vim command if someone tries to copy/paste that sequence. – B Layer Feb 21 at 3:32
  • @BLayer Ah the perils of browsing vi.se on my phone. Well spotted! – Rich Feb 21 at 11:00

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.