I would like to know how I could trigger a certain action when I'm within a certain environment when writing code/ latex using vim. For instance, suppose I'm writing tex and inside the align environment, then I'd like every equals sign I enter to be converted to &=.
What I'm thinking of now is rather cumbersome. This is pseudo code for what is in my vimrc. Here's what it looks like
Shortcut for environment opens start and end tag for align environment (Using imap)
The above action also initiates a counter variable, sets it to 1
If counter is 1, every equals sign I enter should be entered as an &=.
Shortcut to force counter to zero when I move out of the environment. (Using imap)
I would like to know if there is an easier way to get around this. This is just an example. The main idea here is for vim to have environment-awareness.
Looking forward to your suggestions.