How to check if my .vim/buflist.txt file is empty or contains whitespace(s) in _vimrc of my gVim on Windows 10?

I tried this:

let BufferListFile = $HOME . "/.vim/buflist.txt"

echo BufferListFile

" This function SHOULD return true if the defined
" buffer list file is empty = contains
" either nothing, or white space
" but is does not work

function BufferListFileEmpty()
    return (empty(glob(g:BufferListFile)))

if (BufferListFileEmpty())
    echo "Empty"
    echo "Not Empty"

I always get:

Not Empty

no matter if the file contains literally nothing, what am I doing wrong?

  • I could be wrong, but I dont think empty works on file contents; rather, it checks if it’s arguments are empty strings or not
    – D. Ben Knoble
    Jul 4, 2018 at 23:34

3 Answers 3


As you found out you are only working with the file name. In order to check the contents of the file you can use readfile(). It will return a list with all the lines from the file. You can then iterate through the lines in a :for loop and check for whatever you want. Or you can filter the list items with filter(). But I think the fastest way is to use match():

function! BufferListEmpty()
  let BufferListFile = $HOME . "/.vim/buflist.txt"
  let lines = readfile(BufferListFile)    " read the file *contents* into a list
  let matched_index = match(lines, '\S')  " find the first entry with a non-space
  return matched_index == -1              " if no match was found -1 was returned
if BufferListEmpty()
  echo "Empty"
  echo "Not"

You might want to read these help topics:

help readfile()
help match()
help filter()
help :for

You can get the size of the file using getfsize(), but to check if it has whitespace in it, you'll either have to read the file in Vim, or use an external command (like grep, or Windows' find). Since you have to read the file anyway for the whitespace check anyway, you could perform both checks like so:

let BufferListFileContents = readfile(BufferListFile)
let BufferListFileIsEmpty = empty(BufferListFileContents)
let BufferListFileIsWS = ! empty(filter(BufferListFileContents, 'v:val =~ "\\S"')) 

Where the last line filters out lines containing only whitespace, and then checks if the result list is not empty.


I found the manual:

:help empty

empty({expr}) empty()

Return the Number 1 if {expr} is empty, zero otherwise.

  • A List or Dictionary is empty when it does not have any items.
  • A String is empty when its length is zero.
  • A Number and Float is empty when its value is zero.
  • v:false, v:none and v:null are empty, v:true is not.
  • A Job is empty when it failed to start.
  • A Channel is empty when it is closed.

For a long List this is much faster than comparing the length with zero.

So, it does not work, because empty does not check the file contents at all.

Still, I have not figured out, as to how to do this, if even possible.


I am now able to determine if the file is completely empty with the following code, I structured it to functions, because they might be handy. This does not solve the problem of whitespace(s) in the buffer list file. So, I will be testing the other solution posted. And if working, accepting it.

" This defines the name and path of my buffer list file
let BufferListFile = $HOME . "/.vim/buflist.txt"

" This function returns true if the defined
" buffer list file exists
" No matter if it is readable by the user
" Gotten from https://stackoverflow.com/a/23496813/1997354
" Needs verification
function BufferListFileExists()
    return (!empty(glob(g:BufferListFile)))

" This function returns true if the defined
" buffer list file is readable by the user
function BufferListFileReadable()
    return (filereadable(g:BufferListFile))

" This function returns true if the defined
" buffer list file is completely empty
function BufferListFileEmpty()
    return (getfsize(g:BufferListFile) == 0)

" Debugging
echom BufferListFileExists()
echom BufferListFileReadable()
echom BufferListFileEmpty()

if (BufferListFileExists() && BufferListFileReadable() && BufferListFileEmpty())
    echo "Empty"
    echo "NotEmpty"

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.