1

I set a variable b:aaa which has the value that I can see via echo b:aaa, how can I copy that to text? I know I can do it by

set @a = b:aaa
"ap

to get it. But can I copy it to the an open file directly?

9
  • 3
    Copy to what text? It's all text. If you mean using it in insert mode: <c-r>=b:aaa
    – Tommy A
    Commented Apr 10, 2017 at 14:26
  • @TommyA I'd like to accept your answer. Commented Apr 10, 2017 at 15:26
  • 1
    That's a technical detail that's left to you. But yes, wrapping it in string() would add safety.
    – Tommy A
    Commented Apr 10, 2017 at 16:25
  • 2
    Are you just wanting to put the contents of a variable into a buffer? e.g. :put=b:aaa Commented Apr 10, 2017 at 16:35
  • 1
    Additionaly see getreg() and setreg() functions for setting the register to a specific value. And also see :h let-option for setting the option to a variable. Commented Apr 10, 2017 at 17:07

2 Answers 2

4

To put the value of a variable into a buffer use the :put command with the expression register, @=:

put=b:aaa

You can use a range with :put to put it at a specific location. Put below the 9th line:

9put=b:aaa

Note: use :put! to put above the current line

For more help see:

:h :put
:h @=
:h :range
1

Registers works like variables, so you are almost done. I think this would work for you:

let @a = b:aaa
"ap
4
  • 2
    Better use setreg() function. Commented Apr 10, 2017 at 17:07
  • I didn't know about the setreg() function, it seems to be more functional because allow line wise and block wise selection. Also the :put solution Peter Rincker mentions is good
    – rbernabe
    Commented Apr 10, 2017 at 18:22
  • 2
    Why is setreg() better @ChristianBrabandt? I thought it just existed to support things like setreg(l:some_var, "val")? Commented Apr 11, 2017 at 20:08
  • 1
    @Carpetsmoker because you can force the register to actually behave like linewise, characterwise or blockwise Commented Apr 11, 2017 at 20:21

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.