Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add vim modeline in markdown document

Tags:

vim

markdown

I would like to know how to use vim modelines in a Markdown document. Is it possible, or does modelines only recognise certain comment markers?

I have tried using this as the first line of my file:

<!-- vim: set ft=markdown -->

I also tried all these suggestions here with no luck.

like image 548
Alex Harvey Avatar asked Nov 20 '18 05:11

Alex Harvey


People also ask

How do I add Markdown to vimrc?

Add the following statement to ~/.vimrc to set filetype=markdown for all .md files. This statement says that when starting to edit a new file that doesn't exist or when starting to edit a new buffer, after reading the file into the buffer, if the file matches the pattern *.md then set filetype=markdown.

How do I add a modeline to a vimrc?

With " set ", the modeline ends at the first colon not following a backslash. Without " set ", no text can follow the options, so for example, the following is invalid: With the following in your vimrc and the default leader key, you could type \ml to add a modeline based on your current settings: " Append modeline after last line in buffer.

What is the difference between set and invalid modeline in Vim?

With 'set', the modeline ends at the first colon not following a backslash. And without 'set', no text can follow the options. For example, /* vim: noai:ts=4:sw=4 */ is an invalid modeline. Sadly, Vim's Modeline feature can be used to compromise security.

How to convert a tab to a white space in Vim?

To enable the feature, open the .vimrc file (located in your home directory), and then add the following line to it: Now, whenever you enter a tab and save the file (where the expandtab modeline command was entered), the tab will automatically convert into white spaces. Let's consider another use-case.


2 Answers

Your modeline syntax is off. Add a colon at the end, and it will work:

<!-- vim: set ft=markdown: -->

Modeline doesn't care about comment markers. There are two different modeline formats:

  • [text]{white}{vi:|vim:|ex:}[white]{options}

    This form does not use the keyword set, does not require a final colon, and most importantly, does not allow random text after options (such as -->). This means, any paired comment markers must necessarily use the second form:

  • [text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text]

    This form requires the use of set keyword, requires terminating options with a colon (:), and allows the terminating colon to be followed by random text.

like image 101
Amadan Avatar answered Nov 01 '22 23:11

Amadan


I apply some markdown related settings according to the autocmd + filetype.

In my vimrc I have:

if has("autocmd")
    autocmd FileType markdown vmap <Leader><Bslash> :EasyAlign*<Bar><Enter>
endif

Not sure that it's the most traditional way.

like image 37
dshil Avatar answered Nov 01 '22 23:11

dshil