Conditional imap

8 views
Skip to first unread message

gitterrost4

unread,
Jun 28, 2010, 5:46:19 AM6/28/10
to vim...@googlegroups.com

Hello,

I am using vim mostly for LaTeX-editing. now I would like to imap the
sequence "(<Tab>" to "\left(".

The Problem being that "\left(" is only valid in Math-Mode.

So I would like to first check (by regex) if I am in a Math-environment
(e.g count the number of "$"-signs) and if so, imap the above mentioned.

Is there any way of doing that?

--
View this message in context: http://old.nabble.com/Conditional-imap-tp29008528p29008528.html
Sent from the Vim - General mailing list archive at Nabble.com.

Christian Brabandt

unread,
Jun 28, 2010, 5:57:53 AM6/28/10
to vim...@googlegroups.com
On Mon, June 28, 2010 11:46 am, gitterrost4 wrote:
> I am using vim mostly for LaTeX-editing. now I would like to imap the
> sequence "(<Tab>" to "\left(".
>
> The Problem being that "\left(" is only valid in Math-Mode.
>
> So I would like to first check (by regex) if I am in a Math-environment
> (e.g count the number of "$"-signs) and if so, imap the above mentioned.
>
> Is there any way of doing that?

You can use map-expressions, as explained in :h map-expression

Excuse my LaTeX skills, I haven't used math-mode in a while, but something
like this should work:

imap <expr> <Tab> synIDattr(synID(line('.'), col('.'), 1), "name")=~
"texMath" ? "\left(" : "\t"

(one line)

This obviously requires a working Syntax highlighting (e.g. :syntax on)
and I am not sure, if the name "texMath" matches all different TeX Math
environments.

regards,
Christian

gitterrost4

unread,
Jun 29, 2010, 3:22:24 AM6/29/10
to vim...@googlegroups.com

I did what you told me to (i dont want to use "(<Tab>" but just "(" to remap
to "\left("). When I enter the command while i opended the Document
everything works fine. But when I put the command in my .vimrc (or the
tex.vim respectively) it stops working.

The map is present, it just won't replace the "(".

Any Idea?

> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
>

--
View this message in context: http://old.nabble.com/Conditional-imap-tp29008528p29020858.html

Christian Brabandt

unread,
Jun 29, 2010, 6:01:22 AM6/29/10
to vim...@googlegroups.com
On Tue, June 29, 2010 9:22 am, gitterrost4 wrote:
> I did what you told me to (i dont want to use "(<Tab>" but just "(" to
> remap
> to "\left("). When I enter the command while i opended the Document
> everything works fine. But when I put the command in my .vimrc (or the
> tex.vim respectively) it stops working.

So when you enter a "(" in insert mode, vim enters the "(" or what are
you expecting? The mapping might not be active or vim doen't recognize
the math-mode.

Please show us the output of:
:verbose imap (
You can do this, by issuing:
:redir @+
:verbose imap (
:redir end
and pasting your clipboard into your mail. If this doesn't output the
mapping that was given, it might be, that some plugin (maybe Latexsuite
?) overrides your mapping.

If you want that mapping only for latex documents, I suggest, that you
put it into a filetype specific file. Create a file
~/.vim/ftplugin/tex.vim (This is for Unix, create Directories that don't
exist yet, for Windows, you can use $HOME/vimfiles/ftplugin/tex.vim) and
in that file you write your mapping. Note, that you probably want to
change the map to be buffer-local, so that in only applies to TeX-files.

To do that change the map to something like this:
imap ( <expr> <buffer> �

If you want to make sure, that no plugin overrides the mapping, you
could place the file tex.vim also below ~/.vim/after/ftplugin/ for Unix
and $HOME/vimfiles/after/ftplugin/ for Windows. That way, it will be
applied last overriding any setting that might have been applied by a
global or filetype plugin.

It might also be, that the math-mode is not correctly
recognized. To find out, what the name of the syntax highlighing for the
math mode is, put the cursor on the math-mode and issue:
:echo synIDattr(synID(line('.'),col('.'),1), "name")

This should output something like texMathSomething. In case it is
something different, you need to adjust your mapping.

Oh and please don't top post. It is general list consensus to trim
quotes and reply inline.

regards,
Christian

Christian Brabandt

unread,
Jun 29, 2010, 6:04:10 AM6/29/10
to vim...@googlegroups.com
On Tue, June 29, 2010 12:01 pm, Christian Brabandt wrote:
> To do that change the map to something like this:
> imap ( <expr> <buffer> �

This should obviously be
:imap <expr> <buffer> ( �

regards,
Christian

gitterrost4

unread,
Jun 29, 2010, 7:04:16 AM6/29/10
to vim...@googlegroups.com

Indeed it was latex-suite overriding the map from my tex.vim file. I fixed
this by appending the line

exe 'source '.fnameescape('~/.vim/ftplugin/tex.vim')

to the file $VIM/ftplugin/latex-suite/main.vim

I do not know, if this could be solved better, but now it works...

I also had to adapt the matching, because your suggestion would only match
if the cursor was between math characters, but not if the cursor was at the
end of the line or after a math delimiter. It now boiled down to:

inoremap <expr> ( synIDattr(synID(line('.'), col('.')-1, 1),
"name")=~"texMath" ? "\\left(" : synIDattr(synID(line('.'), col('.'), 1),
"name")=~ "texMath" ? "\\left(" : synIDattr(synID(line('.'), col('.')-1, 1),
"name")=~"Delimiter" ? synIDattr(synID(line('.'), col('.')-2, 1),
"name")=~"Delimiter" ? synIDattr(synID(line('.'), col('.')-3, 1),
"name")!~"texMath" ? "\\left(" : "(" : synIDattr(synID(line('.'),
col('.')-2, 1), "name")!~"texMath" ? "\\left(" : "(" : "("


Thanks for helping :)
--
View this message in context: http://old.nabble.com/Conditional-imap-tp29008528p29022437.html

Christian Brabandt

unread,
Jun 29, 2010, 2:15:57 PM6/29/10
to vim...@googlegroups.com
Hi gitterrost4!

On Di, 29 Jun 2010, gitterrost4 wrote:

> Indeed it was latex-suite overriding the map from my tex.vim file. I fixed
> this by appending the line
>
> exe 'source '.fnameescape('~/.vim/ftplugin/tex.vim')
>
> to the file $VIM/ftplugin/latex-suite/main.vim
>
> I do not know, if this could be solved better, but now it works...
>
> I also had to adapt the matching, because your suggestion would only match
> if the cursor was between math characters, but not if the cursor was at the
> end of the line or after a math delimiter. It now boiled down to:
>
> inoremap <expr> ( synIDattr(synID(line('.'), col('.')-1, 1),
> "name")=~"texMath" ? "\\left(" : synIDattr(synID(line('.'), col('.'), 1),
> "name")=~ "texMath" ? "\\left(" : synIDattr(synID(line('.'), col('.')-1, 1),
> "name")=~"Delimiter" ? synIDattr(synID(line('.'), col('.')-2, 1),
> "name")=~"Delimiter" ? synIDattr(synID(line('.'), col('.')-3, 1),
> "name")!~"texMath" ? "\\left(" : "(" : synIDattr(synID(line('.'),
> col('.')-2, 1), "name")!~"texMath" ? "\\left(" : "(" : "("

Ok, I admit, I don't understand, why you need to check for 3 columns in
front of the cursor. But hey, if it works, I won't complain ;)

regards,
Christian
--

gitterrost4

unread,
Jun 29, 2010, 3:55:11 PM6/29/10
to vim...@googlegroups.com

>Ok, I admit, I don't understand, why you need to check for 3 columns in
>front of the cursor. But hey, if it works, I won't complain ;)

Well in the \[ or the $$ environment, I need to check, if there is a
delimiter one space in front of the cursor, a delimiter two before cursor
and a non-math environment before that. Thet's why I need to check 3 columns
before the cursor.
--
View this message in context: http://old.nabble.com/Conditional-imap-tp29008528p29027684.html

Ben Fritz

unread,
Jun 30, 2010, 12:19:34 PM6/30/10
to vim_use


On Jun 29, 6:04 am, gitterrost4 <gitterro...@gmx.de> wrote:
> Indeed it was latex-suite overriding the map from my tex.vim file. I fixed
> this by appending the line
>
> exe 'source '.fnameescape('~/.vim/ftplugin/tex.vim')
>
> to the file $VIM/ftplugin/latex-suite/main.vim
>

This could certainly be done better!

Just put your mapping in ~/.vim/after/ftplugin/latex-suite.vim, and
don't add any extra "source" logic to anything.
Reply all
Reply to author
Forward
0 new messages