Upon opening a new file in an active buffer in the current window, the message line at the very bottom of the screen shows "%f" [New File].

How does Vim know that the buffer contains a new file, rather than an existing file? I want to detect this in order to test whether quickfix has correctly parsed a file name from an error message. Since getqflist() gives 'bufno' instead of 'filename', I can't use filereadable() for this.

I've also tried checking getbufvar() but until you actually try to jump to the error with :cc, any buffers created by quickfix after parsing an error message for a file name are unlisted and getbufvar() returns an empty dictionary. I want to determine whether the buffer will contain a new or existing file and intervene before jumping to the error and opening the file.


The list of built-in functions includes bufname, which can retrieve the filename displayed in the output of ls! from an unlisted buffer, before/without listing it:

bufname( {expr})      String  Name of the buffer {expr}

So, if you know the number of the buffer, you can use filereadable() as in the following example:

let qflist = getqflist()
for item in qflist
    let readable = filereadable(bufname(item.bufnr))
    echo bufname(item.bufnr) . " is " . (readable ? "" : "not ") . "a readable file."

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.