shved
shved

Reputation: 406

vimrc autocmd: not-equal Filetype in

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.

Upvotes: 6

Views: 2221

Answers (2)

Christian Brabandt
Christian Brabandt

Reputation: 8248

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).

Upvotes: 16

Kent
Kent

Reputation: 195029

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.

Upvotes: 4

Related Questions