I am trying to come up with a way to populate the vim quickfix window with the output from :ls

I tried some variants like cexpr ":execute ls" but it doesn't work.

Can someone please help me with the correct Vimscript to get the :ls command output into quickfix?

apparently {expr} is wrong

1 Answer 1


VimScript makes a difference between commands and expressions.

Whatever you type in the command-line is a command. Commands can output some text, but they cannot "return" anything, and so they cannot be nested one into another (except string arguments).

On the other hand, expression cannot stand in the command-line alone. E.g. 2+2 is an expression, but in order to print it you have to provide a command, e.g. :echo 2+2

Hence, :cexpr expects an expression and does not accept commands such as :ls or :execute. That's the problem.

A very general approach is :h execute() function (hence an expression), so

:cexpr execute("ls")

is valid.

However, that's not the best solution. Making use of specialized VimScript functions should be preferred whenever possible. As an example:

cexpr map(getbufinfo({'buflisted': 1}), {_, v -> v.name})
  • 1
    Far simpler :) i was coming to post this
    – D. Ben Knoble
    Dec 30, 2020 at 15:13
  • exactly was I was looking for :) and super thanks for the high quality explaination. Dec 30, 2020 at 15:48
  • 1
    Good to know about cexpr and execute. We can use this trick to run external commands like lint tools etc., and populate their output in quickfix list: :cexpr execute("!flake8 %") Feb 1, 2022 at 17:30

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.