Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vimrc autocmd: not-equal Filetype in

Tags:

vim

I can use autocmd to make my cmd to run if file of specific file type loaded. For example for python:

autocmd FileType python make me happy with python

But is there any way to make my cmd to run if loaded file is NOT of specific type? Something like:

autocmd FileType !python make me happy without python

example above doesn't work and autocmd! will remove autocmd.

Any suggestions? Thanks.

like image 868
shved Avatar asked Dec 06 '22 22:12

shved


2 Answers

There are several possibilities:

  • The easy way is to call a function and make your function check the filetype (and abort, if the filetype is python).
  • An alternative approach is to set a flag for python filetypes and make you function check the flag.
  • Use the * pattern and call your code only inside an if condition checking the filetype (something similar to this): autocmd Filetype * if &ft!="python"|put your code here|endif
  • The hard way is to create a pattern, that doesn't match python. Something like this should do it: :autocmd FileType [^p],[^p][^y],[^p][^y][^t],[^p][^y][^t][^h],[^p][^y][^t][^h][^o],[^p][^y][^t][^h][^o][^n] :put your code here

(the last part is untested and should illustrate, why usually any of the other possibilities are used).

like image 139
Christian Brabandt Avatar answered Dec 22 '22 07:12

Christian Brabandt


You can make an autocmd which triggered by all (*) filetype, then call a function.

In the function, you can check the ft option (&ft) to decide what should be done for certain filetype. There you can do any matching logic with the value of &ft.

like image 45
Kent Avatar answered Dec 22 '22 08:12

Kent