7

While working a Markdown plugin, we've wanted to change the filetype from mkd to markdown to increase compatibility with other plugins.

To avoid breaking users .vimrc that had autocommands like:

autocmd FileType mkd echo 'a'

we've tried using:

set filetype=mkd.markdown

from ftdetect/markdown.vim, since help 'ft' says both types would be tried.

Now the syntax works fine, but the autocmd does not run. The only way to make it run is to use:

autocmd FileType mkd.markdown echo 'a'

which is obviously bad.

Is there a way to avoid breaking user autocommands in this case?

4
  • Did you try to separate set ft=mkd and set ft=mkd.markdown?
    – romainl
    Commented Sep 28, 2015 at 9:46
  • @romainl I don't understand: separate how? Add another set ft line? The file: github.com/plasticboy/vim-markdown/blob/master/ftdetect/… Commented Sep 28, 2015 at 9:48
  • No, both on the same line so that there's an "mkd" event triggered before the "mkd.markdown" event.
    – romainl
    Commented Sep 28, 2015 at 10:18
  • @romainl set ft=mkd || set ft=mkd.markdownin ftdetect seems to work, but when I open a markdown file it echos an empty line and requires me to press enter, not sure why. But that is a bit ugly, specially I'd also have to add a set ft=markdown to make it work for users who update to the new filetype. Commented Sep 28, 2015 at 10:52

2 Answers 2

7

Use

:autocmd FileType *markdown* :echom "a"

This will match for the mk.markdown pattern as well as the regular markdown pattern. It's important to keep the '*' to have the pattern match even when other subtypes have been provided (otherwise markdown would not match the string mkd.markdown, since this is implictly anchored to the start and end and can be seen by using :echo glob2regpat('markdown')

3
  • This is a possible workaround, but a bit ugly as it loses match specificity ;-) Commented Sep 28, 2015 at 14:55
  • How many filetypes do you know, that contain the string markdown? Commented Sep 28, 2015 at 14:56
  • Yes, not likely a problem for markdown :-) Commented Sep 28, 2015 at 14:58
1

To avoid false positives with a pattern like *markdown*, use * and then filter in the code that you run from the autocmd:

autocmd FileType * call s:CheckMarkdown()

function s:CheckMarkdown()
  if match(&filetype, '\v<markdown>') != -1
    return
  endif

  " ... run your desired side-effects
endfunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.