Neovim – run autcmd on all filetypes EXCEPT

There are several things we need to clear.

First, I think some commands are inherently global even though you have only run it for specific filetypes, for example, the above colorscheme command is a global command, you can not make it work only for one buffer.

To verify, you can open several files of different types, if some file does not match your ignore type, colorscheme command will run. Then for all the files, colorscheme will be set to that theme you choose, even though you haven’t specified colorscheme for the ignored file types.

Second, you can not use &ft to check the file type of a file when reading it, at that time, file type detection is being conducted, and the type of file haven’t been determined. You can use file suffix to check a file’s type like this: expand('%:p:e') (this will get the suffix of a file).

In summary, you are doing two things wrong: (1) use a inherently global options or commands and expect it to work only for a certain file type (2) use &ft to decide a file’s type during its opening.

If you use some options that can be local to a buffer and change the way you detect a file’s type, you will find that the command can be local to that file. For example, use the following toy.vim config:

let suffix_to_ignore = ['md', ]                                                                         
autocmd BufWinEnter * if index(suffix_to_ignore, expand("%:p:e")) < 0 | setlocal tabstop=4 | endif

Activate vim with vim -u toy.vim. Then edit a vim file, :e toy.vim, set tabstop? shows that tabstop is set to 4. Open a markdown file, :e test.md, set tabstop? shows that tabstop is still 8. So the command is indeed only run for other filetypes other than Markdown.

References

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top