I'm trying to learn a bit of vimscript and currently reading the 12th chapter of Learn Vimscript the Hard Way.
In this page, the author says :
A common idiom in Vim scripting is to pair the BufRead and BufNewFile events together to run a command whenever you open a certain kind of file, regardless of whether it happens to exist already or not.
So I thought I would try this for a problem I have. I've got a folder in which I would like to be sure all files have the filetype markdown. The folder contains existing files, which have the filetype text and don't have any extension.
I added the following line to my vimrc :
autocmd BufNewFile,BufRead /path/to/my/folder/* set filetype=markdown
Now, when I create a new file inside my folder, the filetype is automatically set to markdown. So the BufNewFile event is triggered as expected. However, when I open an existing file, the filetype remains text, as if the BufRead event was not triggered.
I found two solutions :
- delete the content of
- change the event to watch from
But out of curiosity I would like to understand a few things:
- Why is the
BufReadevent not working ?
- What's the difference between
- For each file previously edited in vim, the
~/.vim/viewfolder seems to have a corresponding file which stores its settings (filetype, indentation...). The settings stored in that file seem to have priority over an autocmd triggered by the
BufReadevent. So, what's the purpose of this event if it's unable to bypass what's inside the
- If I want to be sure to automatically change any setting for a file with an autocommand, is the
BufEnterevent the right one to choose ?