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


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 '20 at 15:13
  • exactly was I was looking for :) and super thanks for the high quality explaination. Dec 30 '20 at 15:48

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.