JuanPablo
JuanPablo

Reputation: 24764

vim : if filereadable

I use this functions for load configuration for particular extension

function! LoadSnippets(extension)                                                        
    let file = expand("~/.vim/templates/".a:extension.".snippets.vim")                   
    if filereadable(file)                                                                
        silent! execute 'source '.file                                                   
    endif                                                                                
endfunction  

autocmd BufRead,BufNewFile * silent! call LoadSnippets('%:e')

but the if never is True. Without the if, the function work ok.

why the filereadable don't find the file?

EDIT

I try with

fnamemodify(file,':p')

but is the same.

Upvotes: 1

Views: 3325

Answers (1)

glts
glts

Reputation: 22694

In the autocommand you are passing the string '%:e' to your function. Then, in the first line of your function this string is concatenated to form "~/.vim/templates/%:e.snippets.vim".

At this point % and :e do not have any special meaning to Vim. They are not expanded by expand() and the resulting path will never point to a readable file.

Passing '%:e' directly to expand() will work though, and this would fix it:

autocmd BufRead,BufNewFile * silent! call LoadSnippets(expand('%:e'))

Upvotes: 1

Related Questions