20

In vim, I am editing a file of filetype "markdown", but which contains latex math expressions such as $x_i$. Vim's syntax highlighting for markdown thinks the pattern *_* (letter-underscore-letter) is an error, and highlights the underscore in such patterns bright red. I would like to turn this off by adding a line to my .vimrc:

autocmd BufEnter *.Rmd "Dear vim, please don't highlight the pattern *_*"

What is the appropriate command to do that? Is it possible at all to do that in .vimrc, without editing a syntax file?

Note: I want to keep the markdown syntax highlighting in general, only turn off that particular feature.

sieste
  • 8,296
  • 3
  • 33
  • 48

2 Answers2

19

If you want to remove _ from the markdown error pattern, you can redefine it. In my case I want to turn off error notifications of underscores in a word as I put a lot of URLs in my documents.

There's a line that defines the error pattern inside syntax/markdown.vim file

" Original error pattern
syn match markdownError "\w\@<=_\w\@="

Remove the _ from the pattern and add that to ~/.vim/after/syntax/markdown.vim.

" New error pattern without the underscore
syn match markdownError "\w\@<=\w\@="
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
16

You have to modify the Markdown syntax for that. One way would be to remove the parsing of the error:

:syn clear markdownError

But that would cause the other syntax rules to start an italic section on that _ char. Better just clear the error highlighting with:

:hi link markdownError Normal

To maintain the general error highlighting, but only define exceptions for the special $x_i$ string, define an overriding syntax group; luckily, this is easy because no existing syntax is there:

:syn match markdownIgnore "\$x_i\$"

(Adapt the regular expression to match all possible math expressions.) This needs to be put into ~/.vim/after/syntax/markdown.vim to be executed after the original syntax script.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • That works, thanks. Is there a way to apply this to the specific pattern only? (I will accept your answer otherwise) – sieste Oct 02 '13 at 13:25
  • That's indeed better; I've added that. – Ingo Karkat Oct 02 '13 at 13:32
  • This also works in vimrc. The line I put in is `autocmd BufNewFile,BufRead,BufEnter *.Rmd,*.rmd :syn match markdownIgnore "\$.*_.*\$"` – sieste Oct 02 '13 at 13:38
  • ... after the line `autocmd *.Rmd,*.rmd set filetype=markdown` – sieste Oct 02 '13 at 13:44
  • 1
    Try `:autocmd Syntax markdown syn match markdownIgnore "\$x_i\$"`, but this must come after the `:syn on` in your `.vimrc`. If that still doesn't work, try prepending `:autocmd VimEnter * ...` to it. – Ingo Karkat Oct 02 '13 at 13:44
  • Your approach goes around the built-in filetype detection and won't work on explicit `:setfiletype markdown` or `:setl syntax=markdown`. That may be fine for you, but you've been warned. – Ingo Karkat Oct 02 '13 at 13:47
  • Another recommendation: Putting everything into `.vimrc` doesn't scale; you'll end up with a very long monster sooner or later. Better use the facilities for encapsulation that Vim offers. – Ingo Karkat Oct 02 '13 at 13:48