2

I am writing an autocmd which will warn me if the file opened is not in UTF-8 format. This is what I have got:

augroup non_utf8_file
    autocmd!
    autocmd BufRead * if &fileencoding != 'utf-8' | echomsg 'file not in utf-8 format!' | endif
augroup END

Unfortunately, the above autocmd does not work in Neovim when I tried to open a file which is not utf-8 encoded. No message is shown.

But if I open the same file in Vim using the above settings, the warning message is shown correctly.

Version info:

  • Neovim: 0.4.0
  • Vim: 7.4
  • What's the output of the command :verbose set fileencodings? when run in Neovim and Vim? – Rich May 14 at 10:53
  • 1
    @jdhao Try to prefix :echom with :unsilent; i.e. unsilent echomsg 'file not in utf-8 format!' – user938271 May 14 at 11:35
  • 4
    I don't know, I don't use Nvim. I tried to find more information from the issue tracker, and only found this issue. FWIW, because of the last issue, I never test whether a piece of code is processed via :echom but via a counter (let g:counter = get(g:, 'counter', 0) + 1). If g:counter exists, your piece of code was processed. The benefit of the counter is it can also tell you how many times it was processed. – user938271 May 14 at 11:45
  • 2
    Thanks for pointing it out. It seems the reason. If I remove F flag from shortmess option, it seems that this issue is fixed. – jdhao May 14 at 11:53
  • 2
    @Rich, I will write one. – jdhao May 15 at 2:33
4

This issue is caused by the F flag in the shortmess option[1]. If you remove the F flag from shortmess, you can see the displayed message correctly. This issue has been filled as #8675 and #9870.

As an alternative, if you change the autocmd event from BufRead to BufEnter or BufWinEnter, the message can be shown without removing the F flag.

I have also filed an issue in the Neovim GitHub repo. The developer says that it is an internal implementation and didn't give the reason for this behavior.


[1] Thanks to user938271 for pointing out that the F flag is added in this neovim commit.

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.