madhukar93
madhukar93

Reputation: 515

Running vim autocommands only on recognized filetypes

I want to run autocmd CursorHold,CursorHoldI * update only for files 'understood' by vim filetypes instead of *. For instance, I don't want to run it for an unsaved buffer. I understand that the expression can be guarded by an if but I can't figure out how to write this condition.

Upvotes: 1

Views: 280

Answers (1)

romainl
romainl

Reputation: 196886

Here is what you want to achieve, expressed in the simplest terms:

I want something to happen only on recognised filetypes

which you can then express with some pseudocode:

if Vim assigned a filetype to this buffer
    do something
endif

Now, what better way to know if the current buffer has a filetype than ask Vim?

if &filetype != ""
    update
endif

which is not pseudocode anymore and only needs to be inlined:

if &filetype != "" | update | endif

to be used in your autocommand:

augroup MyAutoSave
    autocmd!
    autocmd CursorHold,CursorHoldI * if &filetype != "" | update | endif
augroup END

Upvotes: 3

Related Questions