4

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 :

  1. delete the content of ~/.vim/view
  2. change the event to watch from BufNewFile,BufRead to BufEnter.

But out of curiosity I would like to understand a few things:

  1. Why is the BufRead event not working ?
  2. What's the difference between BufRead and BufEnter ?
  3. For each file previously edited in vim, the ~/.vim/view folder 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 BufRead event. So, what's the purpose of this event if it's unable to bypass what's inside the view/ folder ?
  4. If I want to be sure to automatically change any setting for a file with an autocmd, is the BufEnter event the right one to choose ?
  • 1
    How are views configured in your vimrc? The way it is described it appears to be set to automatically create these view files, as opposed to invoking :mkview manually? If possible I would like to reproduce what you're experiencing in order to look for an answer. – timss Oct 29 '15 at 13:57
  • You're right, I completely forgot, I had those 2 lines inside my vimrc : au BufWinLeave ?* mkview and au BufWinEnter ?* silent loadview I suppose it was to automatically save and restore folds. – saginaw Oct 29 '15 at 14:15
  • 1
    I had my suspicions, glad to hear my hint was of any use! I posted an answer as well, so this question can be marked as solved if you consider it so. – timss Oct 29 '15 at 14:36
  • I marked the question as solved. Thank you very much for taking the time to help me. – saginaw Oct 29 '15 at 14:44
2

As far as I know, there is no precedence for views using mkview and loadview over buffer triggers such as BufRead. Whatever is executed last, will define which configuration is to be used.

Per discussed in the comments, there are two possible solutions to this question:

  • Remove the BufWinLeave and BufWinEnter autocommands which make and load views.
  • Or place the BufRead etc. commands after the view commands.

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.