Syntax file sourced from the plugin is overwritten by Vim - vim

I am creating a very simple plugin to make syntax highlighting better in Vim for baan syntax (basically an improved version of syntax/baan.vim).
This is my plugin directory.
One file inside ftdetect folder; baan.vim. It looks like this.
au BufRead,BufNewFile *.bc set filetype=baan
The file inside syntax folder; baan.vim. It is almost the same as baan.vim in syntax folder within Vim. With one line of change in syntax highlighting.
My problem is when I open any files with .bc extensions, two more syntax files are called; one before my own plugin syntax file, and other one is after.
bc.vim syntax file is called already from filetyp.vim, I guess. Because filetypes I want to set syntax is with extension .bc. This is the first issue. Second one is that I set filetype to 'baan' but Vim is looking and sourcing all baan.vim files. But once baan.vim is source from myplugin, it is still sourcing the one from Vim itself. How can I solve those issues elegantly without using /after directory?

This is normal. Consider the following.
$VIMRUNTIME/filetype.vim sources ftdetect/*.vim scripts near to the end. At this point setf bc was already executed and the first FileType event was triggered and processed. BTW. This is the reason why late set ft=baan works but setf baan no more. Shouldn't be a problem though, as :syn clear is executed in $VIMRUNTIME/syntax/synload.vim automatically. And no, you can't do anything with this unless you patch/replace filetype.vim.
synload.vim intentionally sources all matching files (:runtime!). This is the reason they respect b:current_syntax variable. The first to set it wins, others step aside by executing :finish at a top. Nonetheless, they all are sourced and get into :scriptnames. Note this also means that extending existing syntax with after/syntax (while not respecting b:current_syntax) is usually more preferable.

Related

Vim syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.

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.

How to get the value of minlines in Vim

Vim often shows wrong highlight when opening perl files contain pod paragraphs, use command
:syn sync minlines=9999
can handle this problem.I am curious about the value of minlines,so,which command will show minlines's value of current opened file? I didn't find that in vim reference manual.
I do not think there is a native command for that. I suggest you to check it out directly in syntax files. For example, about Perl, take a look at perl.vim. I am on Arch Linux and this file is available here: /usr/share/vim/vim80/syntax/perl.vim. If you go to line 435, you should see this: syn sync minlines=0.
Be aware that some syntax files define specific minlines rules. In Ruby/Java files, you should be able to call :echo ruby_minlines or :echo java_minlines respectively. This will not work with Python, PHP or JavaScript.
Finally, if you are ready to sacrifice a bit of performance for better ergonomics, you can add the following command to your .vimrc: autocmd BufEnter * :syntax sync fromstart
I use it to avoid annoying issues with syntax highlighting. It works great, but Vim will be extremely slow if you try to edit huge files...

Executing vim command based on filetype?

I'm trying to have the command
let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
run every time I open an html file. I tried putting the line
autocmd FileType html let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
in a file named html.vim in both my ftdetect and ftplugin folders and nothing happened. How do I have the command run everytime I'mm in an html file?
The command is to change the matching behavior of matchit btw.
In general, your autocmd is alright; the problem is that you're trying to redefine the b:match_words definition done in $VIMRUNTIME/ftplugin/html.vim, so the execution order becomes important.
The place for these customizations is in the after directory, i.e. ~/.vim/after/ftplugin/html.vim; just create a new file and put the :let command in there.
You can observe the sequence of sourced scripts via :scriptnames. In other cases, when you're not overriding default behavior, the :autocmd FileType is alright, but I prefer putting these (e.g. custom mappings) into ~/.vim/ftplugin/html_mymappings.vim, as it provides better separation and helps keeping your .vimrc short and understandable.
The ftdetect subdirectory is for filetype detection, i.e. inspecting file path / name / contents to determine the correct filetype. It doesn't apply here, as the filetype is html.

Vim In-File Commands

I'm after a means by which I can add additional commands to a text file via vim. For example, just as you can do something like this:
# vim:syntax=foo
I'd like to do something like:
# vim:option call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
Any ideas? I know I can write a syntax file, but this is not what I'm after for now.
Vim modeline syntax (see :help modeline) is not intended to specify commands
to execute during file opening. That is exactly what autocommands is for (see
:help autocommand). What you are trying to do should be an autocommand
similar the following.
autocmd FileType foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
or
autocmd BufReadPost *.foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
(Here instead of *.foo you can use any pattern that matches path or filename
(or both) of the target file.)
If the configuration you are setting up is local to some files or a project,
and you don't want to pollute your .vimrc with those autocmds, use
localvimrc plugin. It allows you to have a "local" .vimrc file next to
your target file or project folder. Script stored in that .lvimrc is
executed when you open files in the same directory where the "local" .vimrc
is, or in its subdirectories. Autocommands shown above (or any other
configurations) can be stored in a .lvimrc file local the project. For
details about localvimrc configuration see the homepage of the plugin.
This isn't an answer to your question, but I have also searched for Truth, and this question here is the closest one to it:
Vim: How to execute selected text as vim commands
It isn't automatic, but potentially only one keypress away it's close enough. :)
My ModelineCommands plugin extends Vim's built-in modelines to execute any Ex command(s) when a file is opened. A set of configurable validators examine the commands and can verify the correctness of an optional command digest, in order to prevent the execution of potentially malicious commands from unknown sources. (That's the main reason why Vim doesn't offer this feature!) This way, you could restrict the commands to only simple :let, or have the plugin query you to confirm execution of anything that isn't signed with your own secret key.

Resources