Closed pluskid closed 8 years ago
Thanks for the nice words!
At the moment, I do not plan to add support for the mentioned functionality. The reason is that I've found this functionality is better kept in more dedicated plugins such as neosnippet or Ultisnips. Both of these plugins implement a high quality snippet engine. Obviously, the mentioned functionality does not require very advanced mechanisms, but at the same time, the functionality is very well suited for snippets. For instance, a snippet for \frac{]{]
can easily be defined such that e.g. frac
or '/
inserts \frac{]{]
and leaves the cursor in the first set of braces. A keyboard shortcut then be used to jump directly to the second braces and then out of the frac
command.
There already exists collection of snippets for LaTeX, e.g. vim-snippets, which provides snippets for several commands and environments. However, it does not provide snippets for many of the things you ask.
Personally, I find snippets to solve the functionality you ask for.
Indeed, Ultisnips is amazing! These are my favourites:
#extends tex snippets
snippet au "autocite"
\autocite[${1}]{${2}}${0}
endsnippet
snippet aus "autocites"
\autocites[${1}]{${2}}${0}
endsnippet
snippet tc "textcite"
\textcite[${1}]{${2}}${0}
endsnippet
snippet tcs "textcites"
\textcites[${1}]{${2}}${0}
endsnippet
@lervag @nasenatmer Thanks for the hint. I agree that it is better to be handled by a snippet plugin. Or I think there should be something built on top of one of the snippet plugin. To my knowledge, those snippet plugins typically need a trigger key (TAB for example) to expand a snippet, but for the math symbols and related very frequently used TeX commands, I still love the behavior of Emacs / Sublime Text more. For example, typing (
gives you (|)
, and if you type (
again, it gives you \left( | \right)
. Not to say `a giving you alpha. No tab is needed here. I guess those should be relatively easy to built on top of a snippet plugin, but need some extra wrapper to make it work more efficiently in the context of LaTeX.
Anyway, thanks again for this nice plugin.
@pluskid Thanks for the kind words!
Regarding the point about the parantheses: This can be at least partially solved with something like delimitMate. I've added the command tsd
in order to easily toggle between (..)
and \left(..\right)
(and similar for other bracket types).
You have a good point about needing a trigger for snippets. I think you're desired feature could be relevant as an improved math mode. I think I could implement this by use of either abbreviations or insert mode maps. An option is obviously necessary to be able to turn on/off this feature. In addition, I think it is relevant to choose the "leader" key for these mappings. You're mentioning `, which might be a sensible default, but I think the optimal setting will depend on the keyboard layout/language.
However, in order to implement this, I would very much appreciate a list of mappings that should be implemented. If you could either provide one or send me a link to an existing list of such mappings, that would be appreciated.
@lervag Wow, that would be really great! Please have a look at this page for a TeX plugin for Sublime Text: https://github.com/randy3k/Latex-Plus/wiki/User%20Manual#4-math-commands
In the math commands section, there are several very handy stuff
...
gives you \ldots
_
is just _
, but if you type it twice, it gives you _{|}
, allowing you to put multiple symbols in the subscript. The same for ^
((
for \left( | \right)
and so on for other bracket, \left\{ | \right\}
for exampleThe leader key ` (denote by foo from now on as the github markdown is interpreting it) is good for me because that is rarely used.
But I guess the user will be happy if that is configurable. In case the user would like to type the key itself, it might just map foo foo
(type twice) to it, or it seems both VIM and Emacs has the ability to tell the event that two keys are typed consecutively within a short time. So if people type foo a
very quickly, he will get \alpha
, but otherwise, it will not be expanded.
Actually, I found a VIM plugin that does similar things, it is called auctex.vim
. It has not been updated for about a year, and it seems mostly containing some key bindings. This page http://www.math.rochester.edu/people/faculty/cmlr/Latex/auctex-vim-keybindings contains a list of their symbol maps, which is very similar to the sublime text one. It would be nice to have those symbol maps incorporated into the new vimtex plugin.
BTW: thanks for the pointer for delimitMate!
typing is just , but if you type it twice, it gives you _{|}, allowing you to put multiple symbols in the subscript.
@pluskid you can achieve your desired behavior using Ultisnips' Anon Snippets:
inoremap <buffer> <silent> __ __<C-R>=UltiSnips#Anon('_\{${1:${VISUAL}}\}',
\ '__', '', 'i')<cr>
You can find more about anonymous snippets with :h UltiSnips#Anon
. Hope it helps.
@petobens Thanks! I tried it and it works as expected. It also works nicely with some other examples. I will collect some defined mappings in this gist when I get more familiar with the UltiSnips syntax.
@pluskid glad it helped. :)
I've started to look at this. I think it makes sense to define the advanced mappings (such as __
, ^^
, ((
) through UltiSnips, in particular since I don't want to implement the functionality to skip out of the parantheses myself. The more straightforward abbreviations I will implement myself, but I want to find a way to make them context sensitive. That is, I think many or most of the mappings only make sense inside math environments, and so I want them only to be relevant there. I've opened a question on Stackexchange to get input on how to do this.
I've opened a branch for working on this feature. I don't have much time lately, so things are going slowly. However, feel free to test and comment the development in the new branch.
@lervag Thanks! Trying the new branch! It seems working nicely. Some minor issues:
backquote
-a map does not work with delimitMate since the latter overwrite the backquote
key.backquote
-a, but only expanded when I start typing something else after typing backquote
-a.I think you are not trying the latest version. I'm sorry it's taking so long, but I have very much on my plate right now. I hope to get some more work done soon.
I do not know if this could be useful. To get this functionality I use a naive approach: in ./vim/after/syntax I put a tex.vim file containing a list of maps:
" In insert mode use ';' to insert commands,
" for instance ';i' inserts inline maths \(...\)
"
let mapleader = ';'
set notimeout
inoremap <buffer> <Leader><Leader> <Leader>
inoremap <buffer> <Leader>a \alpha
inoremap <buffer> <Leader>b \beta
.
.
.
inoremap <buffer> <Leader>I \int_{}^{}<Esc>F}i
inoremap <buffer> <Leader>S \sum_{}^{}<Esc>F}i
inoremap <buffer> <Leader>/ \frac{}{}<Esc>F}i
inoremap <buffer> <Leader>P \frac{\partial }{\partial }<Esc>i
I extensively use these maps in editing tex files.
Hi @verga. Thanks for your input. I'm working on something I think will become a good solution. It will create maps similar to the ones you describe with an optional leader key.
Great! I did not mention it in my comment, but I use your plugin every day and I find your approach interesting, useful, and intelligent. My skills in vim are more than limited, even if, as other people, I use it since my thesis in the 80'(!). Thank you.
2015-06-17 18:46 GMT+02:00 Karl Yngve Lervåg notifications@github.com:
Hi @verga https://github.com/verga. Thanks for your input. I'm working on something I think will become a good solution. It will create maps similar to the ones you describe with an optional leader key.
— Reply to this email directly or view it on GitHub https://github.com/lervag/vimtex/issues/172#issuecomment-112873783.
Thank you @verga for the kind words! I'm very happy that my plugin is used and appreciated by fellow researchers and vim/LaTeX users!
I've now updated the math-mappings branch. I would be happy if anyone could test it @pluskid @nasenatmer @petobens. As for now, the following mappings (see below) are created. Here math = 1
indicates that the mapping is only valid in math mode, and leader = 1
indicates that the mapping takes a leader key, by default the backtick ```.
Note that the advanced mappings depend on Ultisnips. At the moment, no workaround is used. This is on the todolist before merging into master.
let s:mappings = {
\ 'list' : [
\ ['...', '\dots'],
\ ['<m-i>', '\item '],
\ ],
\}
let s:mappings_math = {
\ 'math' : 1,
\ 'list' : [
\ ['__', '_\{$1\}'],
\ ['^^', '^\{$1\}'],
\ ['((', '\left($1\right)'],
\ ['[[', '\left[$1\right]'],
\ ['{{', '\left\{$1\right\}'],
\ ['exp', '\exp\left($1\right)'],
\ ['cos', '\cos'],
\ ['sin', '\sin'],
\ ['tan', '\tan'],
\ ['log', '\log'],
\ ['in', '\in'],
\ ['to', '\to'],
\ ['lim', '\lim_{$1}'],
\ ['qj', '\downarrow'],
\ ['ql', '\leftarrow'],
\ ['qh', '\rightarrow'],
\ ['qk', '\uparrow'],
\ ['qJ', '\Downarrow'],
\ ['qL', '\Leftarrow'],
\ ['qH', '\Rightarrow'],
\ ['qK', '\Uparrow'],
\ ]
\}
let s:mappings_math_leader = {
\ 'leader' : 1,
\ 'math' : 1,
\ 'list' : [
\ ['a', '\alpha'],
\ ['b', '\beta'],
\ ['c', '\chi'],
\ ['d', '\delta'],
\ ['e', '\varepsilon'],
\ ['f', '\varphi'],
\ ['g', '\gamma'],
\ ['h', '\eta'],
\ ['k', '\kappa'],
\ ['l', '\lambda'],
\ ['m', '\mu'],
\ ['n', '\nu'],
\ ['o', '\omega'],
\ ['p', '\pi'],
\ ['q', '\theta'],
\ ['r', '\rho'],
\ ['s', '\sigma'],
\ ['t', '\tau'],
\ ['u', '\upsilon'],
\ ['z', '\zeta'],
\ ['D', '\Delta'],
\ ['F', '\Phi'],
\ ['G', '\Gamma'],
\ ['L', '\Lambda'],
\ ['N', '\nabla'],
\ ['O', '\Omega'],
\ ['Q', '\Theta'],
\ ['R', '\varrho'],
\ ['U', '\Upsilon'],
\ ['X', '\Xi'],
\ ['Y', '\Psi'],
\ ['i', '\int_{$1}^{$2}'],
\ ['S', '\sum_{$1}^{$2}'],
\ ['/', '\frac{$1}{$2}'],
\ ['0', '\emptyset'],
\ ['6', '\partial'],
\ ['8', '\infty'],
\ ['=', '\equiv'],
\ ['\', '\setminus'],
\ ['.', '\cdot'],
\ ['*', '\times'],
\ ['<', '\leq'],
\ ['>', '\geq'],
\ ['~', '\tilde{$1}'],
\ ['^', '\hat{$1}'],
\ [';', '\dot{$1}'],
\ ['_', '\bar{$1}'],
\ ]
\}
Thanks for the invitation. Math mode? Seems crazy to turn around numbers from a social scientist perspective but I'll do my best :D
backquote
works as a leader (although it is a dead key on my keyboard), which is really good because many people in Germany use it like this and this means they still can use this as a leader key.…
as a separate key on my third level, but anyways, ...
works, too.m-i
mapping? What am I supposed to press here?backquote-^
, because ^
is a deadkey on my keyboard again, which means I either have to press it twice to get one ^
or press ^-<space>
to get one ^
. Interestingly, though, ^^
works (by pressing it four times…)Looks very promising to mee!
Jakob Hetzelein wrote:
Thanks for the invitation. Math mode? Seems crazy to turn around numbers from a social scientist perspective but I'll do my best :D
Just a short comment: To set the leader for the new mappings, use g:vimtex_mappings_leader
.
I would be very happy to get a different name than math mode
. The point is to separate this from the other mappings, because this entire feature is an insert mode mapping feature. I think I might want to separate it as a new module/file as well. My idea is that mapping.vim
takes care of all general purpose mappings, and that this new feature takes care of utility mappings in insert mode for faster typing of LaTeX.
@lervag Thanks for the updating! It is great! Just a few commenting:
$|
, where |
indicate the cursor, then all the mappings works fine. But for the case when you already have a closing dollar for the math mode, $|$
, the mappings with a back-quote leader (e.g. \alpha
) works, but the other mappings does not work.\frac{}{}
and then expand a ^{}
within the frac
, then you lose the ability to properly jump out the snippet. But I guess this is rather an issue with UltiSnips.First: I've renamed the work to snippets.vim
. I think that is the most sensible name, and in the doc I plan to mention Ultisnips and neosnippet as good tools for writing more complex snippets. Thus we no longer call it math mode, and social scientists may rest easy (@nasenatmer).
@pluskid
$|$
and type for instance $__|$
I get $_{|}$
.@nasenatmer
<m-i>
is Meta-i or Alt-i. I've found this to be a nice mapping for quickly writing itemized or enumerated lists in LaTex.
I would be very happy if I could get some feedback on which snippets should be predefined and which should be up to the user to define.
I've added an option g:vimtex_snippets_custom
which should be a list of snippet dictionaries similar to the ones defined in vimtex#snippets#init_script()
, e.g.
\ 'miscellaneous' : {
\ 'list' : [
\ ['...', '\dots'],
\ ['<m-i>', '\item '],
\ ],
\ },
If the dictionary has the key leader
set to nonzero, the mappings will use the leader key. If the dictionary has the key math
, the mapping will only be valid inside math environments.
Just update to HEAD but it seems <m-i>
mapping doesn't work now anymore. Pressing Alt-i
shortly switches to NORMAL mode but then returns to INSERT mode.
First thing I notice when using this branch is that when I'm typing normal text and type a letter that starts one of the mappings (such as e
xp, c
os, s
in or t
), vim
waits for a few milliseconds for the next keypress to ensure it's not a mapping I'm going to type until it moves the cursor on. This feels very laggy and is rather strange. Will there be a way to disable certain mapping dictionaries?
Additionally, I'm not using math mode but this happens in normal environments. Finishing one of the above mentioned mappings doesn't actuate them (i.e. pressing cos
doesn't return \cos
). But they do work in math mode.
That is strange. <m-i>
works fine here.
And yes, the mappings will lead to a wait on things like e
xp and similar, however, you can continue to type with no log.
You can disable set of mappings through e.g. g:vimtex_snippets_math = 0
and g:vimtex_snippets_math_leader = 0
. This will be polished as I continue to work on it.
An alternative approach is to instead let all/most mappings use the desired leader, and then use abbreviations for the simple mappings. Abbreviations don't allow function calls such as the one I'm using to detect the math mode, but they would remove the lag.
@lervag OK, it's a bit strange. Now $|$
typing __
works for me. But for example, in the case of $|
, typing sin
gives me $\sin|
, and typing sin
again, gives me $\sin\sin|
. But for $|$
, typing two sin
only gives me $\sinsin|$
-- one slash is missing.
Just want to mention that I've not forgotten about this issue. New issues are prioritized, and there's been little time to continue the work this summer. I hope to get some progress during August.
Ok, so I am back in business. I've been thinking about this for some time. I'm beginning to think that I should still avoid features that are better achieved with other plugins. So, I have a new proposal for how this should work:
imap.vim
instead of snippets.vim
, since the point of this is to add insert mode mappings (or abbreviations).What do you guys think?
I agree. And some documentation for adding more advanced shortcut is definitely useful.
Ok, I need to think more about this. I might be changing my mind again. The mappings actually seem to be quite useful, and it seems like it is almost ready as it is.
However, the leader key thing is not quite there. I'm thinking that perhaps I could try to use an autocommand that detects if we're in math mode or not, and when in math mode, the mappings should be created. That way, the mappings are not present outside of math mode, which means the leader key is free (unless it is used for other non-mathmode-mappings).
At least I've begun to look at this again. I hope that I can find a good solution, because I think this feature can become quite good and useful.
I made some updates and fixed the problem you mentioned earlier, @pluskid.
Some minor updates:
<leader>li
I actually think the code might be ready for merging into master. I'll allow some time for feedback first.
Note, I will of course add documentation before merging. As for now, there is no documentation.
@nasenatmer @pluskid @verga @petobens (I'm sorry if you feel "dragged" into the discussion!)
I've been testing it a little bit today, and I've found that (some of) the math mappings that don't use a leader may need some tweaking. For instance, the mapping in -> \in
makes it impossible to write begin
.
I think a solution is to let this and similar mappings instead require the leader.
hi, i came across this by accident. not a vimtex user (yet). however, in what seems to be an example of convergent evolution i have started to use a similar workflow. latex-box plus ultisnips. i made the rule for myself that any snippet which has an argument (such as commands, item, begin/end pair, equation, figure, align etc.) i do using ultisnips ( for {...} i use followed by ultisnips trigger) and all the zero-argument ones as simple abbreviations.
about the proposed abbreviations. they look good except for some things
i copied a list of abbreviations from auctex.vim and modified them slightly. fwiw,
au FileType tex inoremap <buffer> `a \alpha
au FileType tex inoremap <buffer> `b \beta
au FileType tex inoremap <buffer> `c \chi
au FileType tex inoremap <buffer> `d \delta
au FileType tex inoremap <buffer> `e \epsilon
au FileType tex inoremap <buffer> `ve \varepsilon
au FileType tex inoremap <buffer> `f \phi
au FileType tex inoremap <buffer> `vf \varphi
au FileType tex inoremap <buffer> `g \gamma
au FileType tex inoremap <buffer> `h \eta
"au FileType tex inoremap <buffer> `i \int_{}^{}<Esc>F}i
au FileType tex inoremap <buffer> `k \kappa
au FileType tex inoremap <buffer> `l \lambda
au FileType tex inoremap <buffer> `ll \ell
au FileType tex inoremap <buffer> `m \mu
au FileType tex inoremap <buffer> `n \nu
au FileType tex inoremap <buffer> `o \omega
au FileType tex inoremap <buffer> `p \pi
au FileType tex inoremap <buffer> `q \theta
au FileType tex inoremap <buffer> `vq \vartheta
au FileType tex inoremap <buffer> `r \rho
au FileType tex inoremap <buffer> `s \sigma
au FileType tex inoremap <buffer> `t \tau
au FileType tex inoremap <buffer> `x \xi
au FileType tex inoremap <buffer> `y \psi
au FileType tex inoremap <buffer> `z \zeta
au FileType tex inoremap <buffer> `D \Delta
"au FileType tex inoremap <buffer> `I \int_{}^{}<Esc>F}i
au FileType tex inoremap <buffer> `F \Phi
au FileType tex inoremap <buffer> `G \Gamma
au FileType tex inoremap <buffer> `L \Lambda
au FileType tex inoremap <buffer> `N \nabla
au FileType tex inoremap <buffer> `O \Omega
au FileType tex inoremap <buffer> `Q \Theta
au FileType tex inoremap <buffer> `S \Sigma
au FileType tex inoremap <buffer> `X \Xi
au FileType tex inoremap <buffer> `Y \Psi
au FileType tex inoremap <buffer> `0 \emptyset
au FileType tex inoremap <buffer> `1 \left
au FileType tex inoremap <buffer> `2 \right
au FileType tex inoremap <buffer> `3 \big
au FileType tex inoremap <buffer> `4 \Big
au FileType tex inoremap <buffer> `5 \bigg
au FileType tex inoremap <buffer> `6 \partial
au FileType tex inoremap <buffer> `8 \infty
"au FileType tex inoremap <buffer> `/ \frac{}{}<esc>F}i
au FileType tex inoremap <buffer> `@ \circ
au FileType tex inoremap <buffer> `\| \Big\|
au FileType tex inoremap <buffer> `= \equiv
au FileType tex inoremap <buffer> `* \times
au FileType tex inoremap <buffer> `( \subset
au FileType tex inoremap <buffer> `) \supset
au FileType tex inoremap <buffer> `< \langle
au FileType tex inoremap <buffer> `> \rangle
au FileType tex inoremap <buffer> `\\ \nonumber\\
au FileType tex inoremap <buffer> `: \dots
some example snippets i use
snippet $ "inline math" w
\(${1:${VISUAL}}\)
endsnippet
snippet "eq(uation)?(\*)?" "Equation(*)" br
`!p s = match.group(2) or ""
`\begin{equation`!p snip.rv = s`}
\label{eq:$1}
$2
\end{equation`!p snip.rv = s`}
$0
endsnippet
snippet "(/|frac)" "Fraction" wr
\frac{${1:${VISUAL:}}}{${2:}}$0
endsnippet
snippet int "Integral with limits" w
\int_{$1}^{$2} $0
endsnippet
snippet "(S|sum)" "Sum" wr
\sum{$1}^{$2}$0
endsnippet
snippet "\\?(left|[Bb]igg?l)([[({|]|\\\w+|\\{)" "lr delims" wr
`!p
m1, m2 = [match.group(i) for i in (1,2)]
if m2=='{': m2='\\{'
def mir(s):
mird = {
'left':'right', 'l':'r', '\\langle':'\\rangle',
'[':']', '(':')', '\\{':'\\}', '|':'|'}
try:
return mird[s]
except KeyError:
return re.sub('l', 'r', s)
mm1, mm2 = [mir(m) for m in (m1,m2)]
`\\`!p snip.rv = m1 + m2` ${1:${VISUAL}} \\`!p snip.rv = mm1 + mm2
` $0
endsnippet
snippet ^ "super" i
^{$1}
endsnippet
snippet _ "sub" i
_{$1}
endsnippet
another point of comparison that may be useful are the standard digraphs for greek characters. you get them with <c-k> roman_character *
. they are listed in digraph.txt in the vim help. ideally, these would use the same mapping between roman and greek as auctex, but they don't always. for instance, w maps to omega and o to omikron.
they follow a standard RFC1345 apparently, but some mappings are quite weird, such as c to xi and x to chi (this seems swapped to me). since they contain the full greek alphabet including characters that look the same as roman characters, it seems the mappings are directed towards general use, not only math.
Hi, I would just like to start by saying this plugin is fantastic. I really like the relative light weight of the package and the features in my point of view are bang on. I was excited to see that the math functions were being implemented.
I am having issues with the snippet expansion imap functions. Both the math leader and plain text in math mode expansions work if there is no snippets in them for example <math-leader> a
gives \alpha
, however <math-leader> /
or <math-leader> ~
does not give the \fraction{}{} or \hat{}. It just types <math-leader> \
or <math-leader> ~
respectively.
Are there any suggestions anyone could make as to why this is. I can see that no one else seems to be having this issue.
@sciff90 Thanks! I'm happy you like it! Do you have UltiSnips installed? The snippet functions require UltiSnips to work.
I feel bad that I still haven't been able to continue work on this feature. I have some ideas that I want to try out that might improve things. I hope to get the time to work on this soon.
Yes I use snippets all the time. I use Siver/ultisnips. That's why I was confused. My other latex snippets work, they are supplied by honza/vim-snippets.
there would probably be room for a collection of extended latex snippets for use with ultisnips. not sure if this should be inside vimtex though.
@nilsbecker First, thanks for all the valuable feedback! I'm sorry this has taken so long. So, regarding the comment about the greek character mappings, I agree that they should follow the standard. I'm fixing this for the cases you mention, but I will first focus on completing the feature now.
@nilsbecker You had several comments. Regarding snippets: I don't want to add snippets to vimtex in general. The point of this feature is not to add snippets in general, but to add abbreviations. However, I found I could use the Ultisnips functionality to create more advanced abbreviations. Note, though, that I still think of this as abbreviations, because the point is that the mappings are expanded directly, and not through the snippet expand key. I've found that this is very nice for some LaTeX commands. The bottom line is that I will not add snippets, but I will use Ultisnips (or perhaps another engine) to add advanced abbreviations (or imaps).
@sciff90 I am not really sure I understand your problem. There are no real snippets here. I use Ultisnips for the anonymous function that expands the snippet directly. E.g. if you type something like ((
you get \left( | \right)
where |
indicates the cursor. This is a snippet that was expanded directly, and you may move the cursor out of the parantheses with the snippet jump key. In any case, I have decided to complete this feature now and to merge it into master. I hope to be able to finish it within a week or two.
Finally: I've opened a new issue (#251) to focus the work load. Here I've listed the remaining tasks before merging. If you have comments specifically on those tasks or if you think I've forgotten something, please don't hesitate to comment.
Note also that I closed this issue, as the new one (#251) takes over.
@lervag thanks for your answer.
regarding the 'standard' question, i don't know really what's best. auctex has one de-facto standard way of defining abbreviations that is familiar to users of legacy vim tex plugins and of emacs. the digraphs use another, actually-standard way (RFC soandso).
it seems the actual standard would be preferable. that may be. however, it's not specifically tailored to math, and contains mappings to greek letters that are never used (like omikron) because the look alike latin ones. not sure what the best solution is.
regarding abbreviations for common functions: i'm not sure expanding sin to \sin is so useful. it only saves one keystroke and will delay visual feedback when typing. i personally would want to disable that.
regarding using ultisnips for actual abbreviations: that sounds like a good solution!
@nilsbecker Please see #251 for the continued discussion. However, I just want to mention that I will very much appreciate feedback on what the default abbreviations/mappings should be.
Thanks for this nice plugin! Just wondering, is there any plan to add support of quickly inserting math symbols? For example, plugins for Emacs and Sublime Text all have a similar ability to, say, type `a to insert \alpha,
type `/ to insert \frac{}{}, etc. It would be nice to have this feature because it makes typing math much faster.