Can I do this in Vimscript?
let [a, b] = s:MyFunc()
function! s:MyFunc()
return [1, 2]
endfunction
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityCan I do this in Vimscript?
let [a, b] = s:MyFunc()
function! s:MyFunc()
return [1, 2]
endfunction
Yes you can, but Vim calls it list unpacking. It can be found in Vim's help:
:h let-unpack
Vim allows you to create and populate two variables from a list:
let [var1, var2] = mylist
which is equivalent to:
let var1 = mylist[0]
let var2 = mylist[1]
There is also an (untagged) section on that help page titled "List unpack". It says you can do:
let [var1, var2; rest] = mylist
to collect an optional unknown number of items in a list.
And you can use a related trick to perform operations on multiple existing variables at once:
let [var1, var2] += 1
There is lots to like about this language.
However I don't believe any such magic is available for Dictionaries as of yet.
let [personality1, personality2] = MeAsksMeAlsoAnswers()
:)
– VanLaser
Aug 19 '15 at 0:03