Set spelling exception in vimrc - vim

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.

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

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.

Override syntax highlighting settings

I have the following in a vim plugin, and I have also moved it to the end of my .vimrc:
syn keyword javaScriptIdentifier const
However, this doesn't work and const is not highlighted properly in JavaScript files. If I run :syn keyword javaScriptIdentifier const after opening a file, then it will be highlighted properly.
I am using pathogen with quite a few plugins, but I would think these all run before then end of my .vimrc. Some of them are using an after directory, but I don't see const anywhere in those plugins. I also don't see it in the system JavaScript syntax highlighting.
Is there any way to ensure that syntax highlighting or other operations take priority?
Putting syn in your vimrc will never work, since existing syntax rules are cleared when changing the syntax highlighting. Consider:
:set syntax=javascript
" Oops, this is a Python file, change the syntax
:set syntax=python
What would happen if existing syntax rules wouldn't get cleared?
You'll need to use the after directory, which you already seem to be familiar with, or use an autocommand:
autocmd Filetype javascript syn keyword javaScriptIdentifier const
You can view an autocommand as roughly similar to events in JavaScript, in the sense that they run a piece of code whenever the user does some action.
I am using pathogen with quite a few plugins, but I would think these all run before then end of my .vimrc
That depends on the plugin. You can hook into autocommands like above, and in addition to that many plugins use the autoload feature (meaning a file won't get loaded until it's needed).

Mixing two syntax highlighting scripts

I write blog posts with Jekyll, and for that I end up using three different languages in the same file: YAML at the start for post metadata, Markdown in the body, and C++ in code snippets.
I tried to setup a script so that I can have vim highlight all three properly for me, and ended up with something like this in syntax/jekyll.vim:
" Build upon Markdown syntax
runtime! syntax/markdown.vim
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in YAML syntax for front matter
syntax include #Yaml syntax/yaml.vim
syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=#Yaml
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in C++11 syntax for code snippets
syntax include #Cpp syntax/cpp.vim
syntax region cppCodeSnippet matchgroup=Comment start=/^{% highlight cpp %}$/ end=/^{% endhighlight %}$/ keepend contains=#Cpp
let b:current_syntax='jekyll'
I also set up a file detection script to set ft to this syntax.
It almost works. When I open a file that gets detected as this type, I get everything correct except for the C++ highlights. However, if I type :syn on after that, everything works fine. I can delete the buffer and open the file again and all highlights are ok. If I close vim and start it again, I need to run :syn on again.
What am I missing? How can I debug this issue?
Quick fix: append syntax on to the last line of your .vimrc, which is the same as setting :syn on in the live session.
Not So Quick:
Looks like you might have installed the custom 'jekyll' syntax alongside the default syntax files in $VIMRUNTIME.
According to Vim wiki section on custom syntax, it's preferable to keep all personal customizations within ~/.vim. For example, putting your jekyll.vim syntax in ~/.vim/syntax/.
Do not use a directory containing the files distributed with Vim because that will be overwritten during an upgrade (in particular, do not use the $VIMRUNTIME directory).
In the Vim syntax docs:
:syntax enable runs ':source $VIMRUNTIME/syntax/DEFAULT_SYNTAX.vim'.
:syn on (or :syntax on) will "overrule your settings with the defaults".
So if setting :syntax on makes your custom syntax work, it must be contained in the default syntax set.
Try keeping all the custom stuff in ~/.vim and see if that settles things.

Enabling markdown highlighting in 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

Resources