Copy dictionary to register in Vim

172 Views Asked by At

I have a dictionary in my vimrc that I would like to copy and paste into whatever text file I'm editing:

let info = { 'tom':12345, 'bob':54689 }

I tried using put as I would with an option setting but I get an error:

:put=@info

How do I copy the contents of "info" to the register so that I can paste it in my text document?

2

There are 2 best solutions below

3
builder-7000 On BEST ANSWER

Try quoting the json data. And removing @ from put command (@ is used to accessed registers, not for variables):

:let info="{ 'tom':12345, 'bob':54689 }"
:put=info
0
Ingo Karkat On

If you want to maintain the exact representation, you have to store the dictionary as a string already (as per @sergio's answer), and you can :put it directly.

If it's fine to have Vim render the dictionary (and potentially reorder its elements), you just have to explicitly convert to a string, in order to overcome the E731: using Dictionary as a String (as it's :help explains, these types are not automatically converted). The string() function will do that:

:put =string(info)