Can I do this in Vimscript?

let [a, b] = s:MyFunc()

function! s:MyFunc()
  return [1, 2]

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.

  • 6
    Who's "you" ? let [personality1, personality2] = MeAsksMeAlsoAnswers() :)
    – VanLaser
    Aug 19 '15 at 0:03

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.