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.
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
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
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
This should obviously be
:imap <expr> <buffer> ( �
regards,
Christian
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
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
--
>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