IssaRice
IssaRice

Reputation: 381

Vim: How to disable syntax altogether for certain filetype

I'm trying to disable syntax for Markdown files using autocmd in Vim. I have the following in my .vimrc:

function! SyntaxOff()
    echom "one"
    syntax off
    echom "two"
endfunction
autocmd filetype markdown exe "call SyntaxOff()"

When I open a Markdown file and check :messages, I can see the one and two, and yet the syntax is still on. If I open a dummy file and subsequently open a Markdown file, then I can see that syntax is disabled. If I then open a file of a different filetype, then syntax is still disabled. My guess is that something else in my .vimrc is re-enabling the syntax when I open Vim up for the first time, but then after the function runs syntax is permanently shut off.

So my questions are (1) How can I disable syntax for Markdown files when I open them directly (i.e. not using the maneuver described above)? (2) How can I get Vim to re-enable syntax when I then open another file of a different filetype?

Upvotes: 1

Views: 1317

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172530

@kev's answer shows you how to disable the Markdown filetype completely by modifying the filetype detection.

If you want to keep the filetype and indent plugins, and just disable the syntax, you can either:

turn off after-the-fact

Create ~/.vim/after/syntax/markdown.vim with:

setlocal syntax=

avoid turning on

Create ~/.vim/syntax/markdown.vim with:

if exists("b:current_syntax")
finish
endif
let b:current_syntax = "markdown"

Upvotes: 1

user427390
user427390

Reputation:

You should wrap your auto commands in a check and also in a group or else you will set multiple auto commands (resource leak) and Vim will slow down after a long time of use. It's also better to use FileTypes instead of regular expressions/matchers for auto commands. However, in some cases the regular expressions make sense. In this case I think you want to use a regex to match *.md as well as markdown files because Vim doesn't detect *.md as markdown.

if has("autocmd")
  augroup standard
    autocmd!

    autocmd BufNewFile,BufRead *.markdown,*.md setlocal setfiletype disabled

  augroup END
endif

Upvotes: 1

Related Questions