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?

  • 3
    Copy to what text? It's all text. If you mean using it in insert mode: <c-r>=b:aaa – Tommy A Apr 10 '17 at 14:26
  • @TommyA I'd like to accept your answer. – Lerner Zhang Apr 10 '17 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 Apr 10 '17 at 16:25
  • 2
    Are you just wanting to put the contents of a variable into a buffer? e.g. :put=b:aaa – Peter Rincker Apr 10 '17 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. – Christian Brabandt Apr 10 '17 at 17:07
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
  • 2
    Better use setreg() function. – Christian Brabandt Apr 10 '17 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 Apr 10 '17 at 18:22
  • 2
    Why is setreg() better @ChristianBrabandt? I thought it just existed to support things like setreg(l:some_var, "val")? – Martin Tournoij Apr 11 '17 at 20:08
  • 1
    @Carpetsmoker because you can force the register to actually behave like linewise, characterwise or blockwise – Christian Brabandt Apr 11 '17 at 20:21

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.