How can I create mappings that work only if the cursor is enclosed by certain characters?
As an example, I want to map
\alpha if the cursor is enclosed by
$$ or by
\[\] so as to make
This jar has an $ja$ engraved on it.
be remapped to
This jar has an $\alpha$ engraved on it.
This \alphar has an $\alpha$ engraved on it.
So, how can I ask vim to check if the cursor is enclosed by
An immediate complication that I can think of is the following:
The jar has an $ja$ engraved on it (note: $ja$ is a greek character).
That is, the check should work just like vim does for checking unclosed parentheses, where there may be several closed ones back too. (Or like LaTeX for checking mathmode). This would be done only on the current line, for scalability.
Here is a scheme of how I would do it for
$$ (if only I knew how to actually check these using vim syntax):
- Take position of cursor;
- Scan to the left for the number of
- Do the same for the right;
- If these are even, work as if in normal text (e.g.
- If these are odd, do the mappings (e.g.
\[\] I think it could be done in the same way as a check for balanced paratheses...
Since, this question is asked having LaTeX in mind, is there a shortcut when using vim-latex, vim-tex, or other TeX plugins for vim?