1

The following ftdetect-file doesn't work as expected:

augroup dummy
  au!
  au BufRead bla* setf dummy
  au BufRead bla* call s:detect_ft()
augroup END

echom 'one'

function! s:detect_ft() abort
  echom 'two'
endfunction

It always displays only one, regardless of the order of the two autocmds. The filetype dummy is always correctly assigned, but the function s:detect_ft() is never triggered.

Why? I don't see any reason this should not work?

1
  • 1
    Yes, I am. I noticed that this does not work, because the real logic wasn't executed. I then reduced it to this simple test case to check it. I deliberately used echom to be able to see it in the :messages list.
    – radlan
    Commented Sep 21, 2022 at 18:03

1 Answer 1

0

Your filetype is probably being overwritten by the built-in autocommands. You probably need to include BufEnter and BufNewFile:

augroup dummy
  au!
  au BufRead,BufNewFile,BufEnter bla* setf dummy
  au BufRead,BufNewFile,BufEnter bla* call s:detect_ft()
augroup END

echom 'one'

function! s:detect_ft() abort
  echom 'two'
endfunction
7
  • 1
    I thought so, too. But no other file type is assigned. Also that would mean both autocmd would not trigger. But the one setting setf dummy actually is triggered.
    – radlan
    Commented Sep 21, 2022 at 6:01
  • I just gave it a try and it seems to be working fine. Filetype correctly set as dummy and detect_ft function called.
    – r_31415
    Commented Sep 21, 2022 at 6:11
  • 1
    I will try it out when I am back at a computer.
    – radlan
    Commented Sep 21, 2022 at 6:15
  • So, I checked now. With your changes it works. Now, the next question is: why? Or better: Why does it not work with only BufRead? Remember: There is a difference, There first autocmd is executed in any case, the second only if BufNewFile and BufEnter are added..
    – radlan
    Commented Sep 21, 2022 at 18:06
  • which autocommand is executed first? If you're referring to setf dummy, only with BufRead, that's not being executed either
    – r_31415
    Commented Sep 21, 2022 at 23:13

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.