Override syntax highlighting settings - vim

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).

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.

vim snippet default snippet file issue

I installed Vundle, got the snippetMate running. But when vim launched, the snippet it loaded is _.snippets. With my Understanding, it is the default for snippet. However, i want to use other snippet such as sh.snippets and tex.snippets. I try to run the SnippetMateOpenFile in Vim to locate the snippets file It doesn't appear. I checked and see that all of the snippet file is there.
Yes, the _.snippets contains the global snippets. In order to use the other ones, the correct filetype has to be set. The snippet plugin reuses the same mechanism that Vim uses for syntax highlighting and settings (like indent) that are specific to a certain programming language. Usually, filetype detection works automatically; you can check with
:verbose setlocal filetype?
This needs to print sh for shell scripts, to use sh.snippets. If it doesn't, you have to fix / enable filetype detection (see :help filetype-detect), or, for a one-time fix, set it manually:
:setf sh
(I'm not sure about your particular snippet plugin; I guess it's snipMate, but there are multiple variants around.)
I found out what happened. the snippets won't recognize the snippet files rightaway. So i saved and exit the text and reopen vim again. It works, yet seems like there must be a certain tag in order for vim to recognize the format of the file.

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.

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.

Resources