sponsor Vim development Vim logo go to HTTPS page Vim Book Ad

OnSyntaxChange : Generate events when moving onto / off a syntax group.

 script karma  Rating 1/1, Downloaded by 465    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin can set up custom User events similar to the InsertEnter and
InsertLeave events that fire when the cursor moves onto / off text that is
matched by a particular syntax group. You can use a custom :autocmd to
perform an action or change a Vim setting depending on the syntax under the

The plugin checks for changes to the syntax group under the cursor whenever
the cursor changes, and keeps track of the state using the event definition
name. Having many definitions or complex syntaxes may slow down cursor
movement noticeably, but the plugin takes care that only necessary hooks are
installed in the proper scope. So as long as you only have buffer-local
definitions, other buffers won't be affected at all.

The following is an overview; you'll find the details directly in the
implementation file .vim/autoload/OnSyntaxChange.vim.
To set up User events for a particular syntax group, invoke:

OnSyntaxChange#Install( name, syntaxItemPattern, isBufferLocal, mode )

You need to give your event definition a name; it is used to differentiate
different setups and is included in the event names that will be generated:

The syntaxItemPattern is a regular expression that matches the syntax group;
both the actual and effective names are considered (e.g. in Vimscript,
"vimLineComment" is linked to "Comment").

Events can be generated globally for all buffers, or just for a particular
buffer; use the latter to create events for particular filetypes (via an
:autocmd FileType or in a ~/.vim/ftplugin/{filetype}.vim script).

The mode specifies whether the syntax check should only be performed in
normal, insert, or any of both modes. For example:
    call OnSyntaxChange#Install('Comment', '^Comment$', 0, 'a')

To handle the generated events, define one or more|:autocmd| for the User
event, matching the event name, e.g.
    autocmd User SyntaxCommentEnterA unsilent echo "entered comment"
    autocmd User SyntaxCommentLeaveA unsilent echo "left comment"

1. Enable 'list' when inside string (e.g. to see the difference between <Tab>
   and <Space>).
This should only affect both modes, so mode is "a". Let's do this only for C
    autocmd Filetype c call OnSyntaxChange#Install('CString', 'String$', 1, 'a')
    autocmd Filetype c autocmd User SyntaxCStringEnterA setlocal list
    autocmd Filetype c autocmd User SyntaxCStringLeaveA setlocal nolist

2. To disable the AutoComplPop plugin (vimscript #1879) when inside a
   comment. (Inspired by http://stackoverflow.com/questions/10723499/)
Let's do this globally. Since the completion only works in insert mode, use
mode "i".
    call OnSyntaxChange#Install('Comment', '^Comment$', 0, 'i')
    autocmd User SyntaxCommentEnterI silent! AcpLock
    autocmd User SyntaxCommentLeaveI silent! AcpUnlock
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim OnSyntaxChange*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.

Rating scripts is only available on the HTTPS page

script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
OnSyntaxChange-1.01.vmb.gz 1.01 2013-01-21 7.0 Ingo Karkat Do not trigger modeline processing when triggering.
OnSyntaxChange.vba.gz 1.00 2012-05-25 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
SourceForge.net Logo