Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a user defined event in Vim

Tags:

vim

vim-plugin

I use a couple of auto commands to do highlighting of extraneous whitespace in my vim setup on InsertLeave and BufReadPost events. I recently started using a plugin to highlight indentation as well (https://github.com/nathanaelkane/vim-indent-guides)

The issue is that if there is an empty line with indentation, it gets highlighted by the indent-guides plugin but not by my auto commands. What I would like to do is add a custom event to the plugin so that when it is done highlighting I can set my autocommands to trigger and overwrite that highlighting in the cases where it should.

For example, this is the type of flow I would like (or at least something similar):

indent-guides plugin activates
indent-guides plugin highlights all indentation
indent-guides plugin triggers custom event signaling it is done
indent-guides plugin exits
auto command whitespace highlighter is triggered by indent-guides completion event

Here are the auto commands I am using for whitespace highlight:

autocmd InsertEnter * syn clear EOLWS | syn match EOLWS excludenl /\s\+\%#\@!$/
autocmd InsertLeave,BufReadPost * syn clear EOLWS | syn match EOLWS excludenl /\s\+$/

EDIT:

I have solved this issue another way (by editing a different plugin). This still doesn't answer this specific question, so I won't be posting that as a solution.

My solution to the problem as a plugin: https://github.com/ntpeters/vim-better-whitespace

like image 961
PseudoPsyche Avatar asked Feb 03 '14 18:02

PseudoPsyche


1 Answers

VIM already has support for triggering both native as well as custom user events. Since your question is regarding custom events, I'd like to give a small example to demonstrate how you can do the same.

This is how you would define a custom autocmd event :

autocmd User MyCustomEvent call my_custom_function()

This is how you would trigger your defined custom autocmd event :

doautocmd User MyCustomEvent

Now you can use doautocmd in a similar fashion to trigger both custom autocmd events like mentioned above and native vim events so depending on your use case you need to evaluate which of the two you need. The advantage of custom User autocmds are that you can rest asure no one else would trigger them whereas for native vim autocmds, they could be triggered by anybody.

like image 78
Dhruva Sagar Avatar answered Sep 22 '22 10:09

Dhruva Sagar