How can I set autocmd in vimrc to only run on certain file types?

Explanation:

I recently purchased the .vimrc file from the git repository and found it incredibly useful so far. One of the useful tools that came with it is that it automatically removes the trailing space when you write the file.

However, I just started using markdown, which gives a clear format for writing text files, which makes it easy to convert these files to different types such as html.

The problem is that two trailing spaces are used to denote a newline. My .vimrc removes them automatically. I found autocmd that does this. It:

autocmd BufWrite * :call DeleteTrailingWS()

      

DeleteTrailingWS is the function that actually removes white space.

My question is:

How do I change this so that it starts / installs this autocmd if the file type is not markdown? (.md) Please explain in such a way that I can name common functions and not just the above. Also, how do you do it with multiple file types. For example, run / install this command only if the file is not of type .md, .abcd or .efgh?

Thanks everyone.

+3


source to share


4 answers


Just check the autocommand for the file type:



autocmd BufWrite * if &ft!~?'markdown'|:call DeleteTrailingWS()|endif

      

+4


source


+2


source


Christian's answer works well if you want some file types other than. Alternatively, defining autocmds for only certain file types, the usual approach is to define buffered local autocmds via

:autocmd BufWrite <buffer> call ...

      

this can be done with another added :autocmd Filetype ... autocmd ...

or plugin file type in~/.vim/ftplugin/...

+1


source


*

in an automatic command specifies what type of file should work, so you just need to replace it with *.md

.

autocmd BufWrite *.md :call DeleteTrailingWS()

      

0


source







All Articles