1

Because on Linux, there is possible that file exists but is not readable by the user (I rather don't think it is feasible on Windows, but...), I seek for VimL functions to differentiate between those two states, which I could put into my _vimrc of my gVim.

1 Answer 1

1

I tried to write these functions myself, but feel free to correct me in your own answer.

" This defines the name and path of my buffer list file
" For I am on Windows, I can use \\, i.e. escaping
" But it is not necessary and also non-portable

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

"-----------------------------------------------------------------------------

" This function returns true if the defined file exists
" No matter if it is readable by the user
" Gotten from https://stackoverflow.com/a/23496813/1997354

function FileExists(FileName)

    return !empty(glob(a:FileName))

endfunction

"-----------------------------------------------------------------------------

" This function returns true if the defined file
" is readable by the user
" There is no reason to define the function myself
" as there already is the following built-in function

" filereadable()
2
  • 1
    So far I've never needed to make the distinction. The functions should: - take parameters instead of working with global variables - be non global, but defined in autoload plugins -- unless you always need them in your vimrc and never anywhere else - and BTW, return isn't a function, you don't need parentheses. - Forget '\\'. '/' always works with every OS. Commented Jul 5, 2018 at 9:36
  • You could make your function support re-definition by banging them. Actually I wouldn't define s:BufferListFileReadable. I only define functions that overlap with builtin functions when there is a chance the end user may have a version of vim that doesn't implement that function. Also I would give a more generic name like: s:fileexists() more in sync with what it really does. Commented Jul 5, 2018 at 14:31

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.