Enabling markdown highlighting in Vim - vim

I'm using Vim in a terminal on my MacBook Air with OS X Lion, and I can't seem to find a good plugin for Markdown syntax highlighting.
So far I've tried the plasticboy plugin and Tim Pope's plugin. The plasticboy plugin worked OK but was causing white space at the end of lines to be highlighted, and I haven't figured out how to turn that off. (It's really annoying, because every time I hit space when I'm writing it highlights the character.)
Tim's plugin didn't seem to do very much in the way of highlighting, other than maybe headers that use ###. Code blocks and bullets are ignored. I might be missing something there. I do use the .md extension on my Markdown files, so it should be picking up the filetype.
I've also seen a reference to Vim 7.3 having Markdown support built in, but without one of those two plugins I don't get any highlighting at all.
Do either of these require specific color schemes to work?

About the native syntax highlight for markdown I think it only works for files with the extension .markdown by default.
I was having problems with markdown syntax highlight for my .md files.
I tried:
:set syntax=markdown
And it worked.
So i included the following line in my .vimrc:
au BufNewFile,BufFilePre,BufRead *.md set filetype=markdown
Now my vim have syntax highlight for my .md files.
BufFilePre is needed for :sav

Native syntax highlighting
Native syntax highlighting for Markdown only works by default for the .markdown file extension.
The following line in .vimrc yields the best results for both vim and gvim:
autocmd BufNewFile,BufFilePre,BufRead *.md set filetype=markdown.pandoc
Explanation:
1. Specify your Markdown flavour!
If you work with mainly one flavour of Markdown (e.g. Pandoc), be sure to mention this as well! Doing so, allows for mixing and matching of both Markdown- and Pandoc-specific Vim plug-ins. For example: I have found the vim-pandoc-syntax plug-in particularly suitable for my highlighting needs. Nevertheless, I use the more general vim-markdown-folding for Markdown folding.
By the way, only one flavour is allowed, separated by a dot, e.g.: filetype=markdown.pandoc
2. gvim requires BufFilePre
gvim requires an additional BufFilePre in the autocommand line for Markdown file type recognition with the Save As… :sav command.

This should work to disable the end-of-line space highlighting when using the plasticboy mkd plugin:
:syn clear mkdLineBreak
You could autocmd that for the necessary file extensions so that you needn't do it every time you load a markdown file.
Note that this specific highlight exists because Markdown treats lines ending with 2 or more space characters specially by inserting a <br>, so it is useful.
The plasticboy plugin uses TODO highlighting for this rule, which is a bit too much as it's designed to, by default, be really garish - yellow background - so that it stands out. You can make this less visually striking by changing that highlight rule. One quick way to do this would be something like:
:hi link mkdLineBreak Underlined
Now those end-of-line spaces will show up as underlined. Try linking to other highlight groups for something that may appeal to you more. Instead of using link you can get even more specific about those end-of-line spaces: for instance you could specify that they show up as just slightly lighter/darker than the normal background, using your own highlight command, specifying custom ctermfg, ctermbg, guifg, guibg settings.
As above, you could autocmd this to apply your specific settings.
For more information about link highlight groups, type: :help group-name and you'll see a list of groups that can be linked that themselves should helpfully be displayed using their current highlight rules. Also: :help highlight.

In Tim's plugin the .md extension works only for README.md because filetype.vim specifies so.
" Markdown
au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,README.md setf markdown

If you don't like putting all of your configuration in ~/.vimrc, you can create ~/.vim/ftdetect/markdown.md (or its equivalent on Windows) with the following contents.
au BufNewFile,BufRead *.md setf markdown

Related

How do you turn off syntax highlighting for new vim windows without a filename or file type?

After I installed the 'artesanal' theme for vim and turned syntax highlighting on, every vim window has syntax highlighting including brand new empty windows [No Name], without a name or file type. I'm wondering if any of you know how to keep syntax highlighting on for every file with an extension but have it disabled for any file without a name or file extension.
This should not happen. I don't know artesanal (and "theme" is an undefined term inside Vim; it has colorschemes, filetype plugins, and syntax scripts; I hope it's not a full Vim "distribution" like spf-13 and Janus, which lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult)).
It looks like a syntax is active even for plain files. Usually, the syntax is determined by the filetype, so check :verbose setlocal filetype? first. If this returns a value, you need to look into the detection of :help filetypes.
If this is empty, it could also be that something sets 'syntax' directly. You can check in the same way: :verbose setlocal syntax?.
Now, if that also is empty, and :syntax list doesn't show something, the highlighting could also come from :match or :call matchadd() commands; :call clearmatches() would remove this then. (And you still would need to find the source that defines those matches.)
You can check to see if a filetype has been set
if &filetype != ""
syntax enable
endif

Set spelling exception in vimrc

When editing a text in markdown, I don't want to highlight bibliography entries. This can be achieved with the following command:
:syn match CitNoSpell '\[#[^[:space:]]\+\]' contains=#NoSpell
However, if I enter this command to .vimrc, it is ignored. I assume that is because spell file is loaded after vimrc has been read, and this definition is not kept.
How should I force vim to ignore this pattern? I prefer it to stay in .vimrc, as I synchronize the file across multiple systems, but another solution would also be welcome.
As the ~/.vimrc is loaded first (before any files), the syntax of an opened file is set only later, and syntax scripts :syntax clear any existing syntax stuff, including your definition.
The right place for your customization would be the :help after-directory; i.e. ~/.vim/after/syntax/markdown.vim, as this will be sourced after $VIMRUNTIME/syntax/markdown.vim.
If you insist on configuring this within your ~/.vimrc, you can try the following autocmd, which has to be put somewhere after :syntax on:
autocmd Syntax markdown syn match CitNoSpell ...
PS: For consistency, as you're tweaking the Markdown syntax, your added syntax group should also start with the syntax name, i.e. markdownCitNoSpell.

Highlighting set of specific keywords in gvim

I have tried highlighting specific words by adding them in .vimrc, colorscheme file and also in syntax.vim(I changed one at a time, not altogether).
syn match mygroupwords '\c\<\(-word1\|-word2\)'
hi def link mygroupwords colo_words12
hi colo_words12 guifg=red1 gui=bold guibg=white
But somehow it seems to be getting overwritten by default syntax highlighting
i need to highlight keywords irrespective of color-scheme or file-type which have following words-
Ex; -word1 , -word2
Any suggestions?
explanation of your failed attempts
A colorscheme just provides color definitions for predefined highlight groups; that's the wrong place for actual syntax matches! ~/.vimrc is the first configuration that is read; if a filetype is detected and a corresponding syntax script is loaded, that will override your syntax definition.
syntax extensions
If your desired highlightings are extensions to an existing syntax, you can place :syntax match commands in a syntax script in the after directory. For example, to extend Python syntax, put it in ~/.vim/after/syntax/python.vim. That may still fail if the original syntax obscures the match; sometimes, this can be solved via containedin=...
independent marks
If your highlightings are independent of syntax and filetype, there's a different built-in mechanism: :match (and :2match, and additional variants via :call matchadd(...)):
:match mygroupwords /\c\<\(-word1\|-word2\)/
This goes on top of (and is independent of) syntax highlighting. However, it is local to the current window. So, if you put this into your .vimrc, it will only affect the first window (but any files viewed in there). To apply this globally to window splits and tab pages, you have to use :autocmds. It's not trivial to get this totally right. If you need such a full solution, have a look at my Mark plugin; this supports multiple colors, allows presetting with :[N]Mark /{pattern}/ (similar to :match), and highlights in all windows.

Strange python highlighting using spf13-vim

So I have been using spf13-vim recently, and I have tried changing the default colorscheme, but for some reason when I do I get strange highlighting of words. The highlighting seems to depend on the filetype such that my .txt and .py file will look different.
As an example, the following image shows the highlighting effect on headnode, clusterRun, bwa, and vt. This only occurs when I alter the colorscheme from default. So is there a way to stop this from happening?
You need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. When you have the name of the offending syntax group, you can investigate where it comes from; (the last lines of) :scriptnames may help.
If you cannot find the highlighting this way, it may be spell checking; this goes on top of the syntax highlighting. You can list the corresponding highlight groups via
:hi SpellBad | hi SpellCap | hi SpellLocal | hi SpellRare
If you don't want spell checking,
:set nospell
turns it off.
Soap Box
Vim "distributions" like spf-13 and Janus lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult). Vim is incredibly customizable, using someone else's customization makes no sense.

Turn on Vim plugins for some files, not others?

I recently added filetype plugin indent on to my .vimrc in order to enable special indenting and syntax highlighting for Clojure code (*.clj files). However, it's also causing indenting in my LaTeX files (*.tex). This is annoying when I'm editing, and even more annoying, because the tab characters that get inserted confuse a custom program I use to process my LaTeX files. I know I can make indenting use spaces, but I really just want "intelligent" LaTeX indenting to go away. Actually, I want all intelligent indenting to go away, except where I specifically ask for it.
How can I get correct auto-formatting for Clojure code in Vim, but turn off all special handling of LaTeX files (except for syntax highlighting)?
Sorry if this has already been answered; I haven't succeeded in finding the answer yet.
(Irrelevant editorial comment: Sometimes Vim "upgrades" make me want to go back to Unix 'vi'. OK, not really.)
Each proper filetype plugin script has an inclusion guard at its beginning. If you don't want any of the filetype options for Latex files (i.e. filetype of tex), create a file ~/.vim/ftplugin/tex.vim with these contents:
:let b:did_ftplugin = 1
This causes the default ftplugin from $VIMRUNTIME to abort its execution. The same applies to indent: ~/.vim/indent/tex.vim and b:did_indent is the guard variable.
Alternative
On the other hand, if you just want to undo certain options (e.g. :setlocal expandtab to avoid inserting tabs), you'd put those overriding commands into the so-called after directory: ~/.vim/after/ftplugin/tex.vim.

Resources