1

Consider the following snippet

context "math()"
snippet "([a-zA-Z])#" "Vectors" riA
\mathbf{`!p snip.rv=match.group(1)`} 
endsnippet

The expected behaviour is that if I write A# I get \mathbf{A} immediately, however, when I type that the cursor goes in front of # and then after a second it goes behind it and the snippet is executed.

Does anybody know how can I fix that?

5
  • It sounds like this is related to tex (or latex). Anyway, does this behavior still occur with all plugins disabled (if any)?
    – husB
    Commented Mar 30, 2022 at 2:29
  • Hi, do you know how can I temporarily disable the plugins to check? Commented Mar 30, 2022 at 9:56
  • @MathsWizzard depends on how you installed them, probably. vim -u DEFAULTS loads without any plugins; then you can manually load ultisnips with the :runtime commands. This sort of delay is commonly observed when there are mappings or abbreviations that start with the same prefix, so that's another place to investigate.
    – D. Ben Knoble
    Commented Mar 30, 2022 at 14:14
  • @MathsWizzard If you are using a plugin manager (eg. packer/vim-plug/pathogen), just comment out the necessary lines. Alternatively if you are using vim8's package management feature, move the plugins from start/ to opt/
    – husB
    Commented Mar 30, 2022 at 14:15
  • Some plugins can be disabled by setting a variable. For example, vimtex can be disabled with let g:vimtex_enabled = 0. Refer to each plugin's help page
    – husB
    Commented Mar 30, 2022 at 14:16

1 Answer 1

2

My hunch is that you are using vimtex with Ultisnips. While this answer assumes that the behavior comes from vimtex, I recommended to diagnose what exactly is causing this behavior.


This thread details how to find out the specific plugin, or the line in one's config, that causes such a behavior.

Alternatively, a delay in execution suggests that there is a mapping that interferes with it, as alluded by Ben in the comments. This can be investigated with :verbose imap <char>. (Note that imap <char> will list the mappings starting with <char>, and the use of :verbose will show where the mapping was created.)


Now, assuming the behavior is due to vimtex, :verbose imap # will return

i  #B          *@vimtex#imaps#wrap_math("#B", vimtex#imaps#style_math("mathbb"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119
i  #-          *@vimtex#imaps#wrap_math("#-", vimtex#imaps#style_math("overline"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119
i  #c          *@vimtex#imaps#wrap_math("#c", vimtex#imaps#style_math("mathcal"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119
i  #f          *@vimtex#imaps#wrap_math("#f", vimtex#imaps#style_math("mathfrak"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119
i  #b          *@vimtex#imaps#wrap_math("#b", vimtex#imaps#style_math("mathbf"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119
i  #/          *@vimtex#imaps#wrap_math("#/", vimtex#imaps#style_math("slashed"))
    Last set from ~/.vim/plugged/vimtex/autoload/vimtex/imaps.vim line 119

Hence, after # is pressed in insert mode, vim waits for a set time, as the next key may correspond to one of the maps listed above. (For example, the user may press # followed by b, which results in \mathbf{...}). This is why there is a delay after # is pressed (on its own), before # is actually inserted.


The output from :verbose imap also gives us a big clue how to resolve the issue. Searching within VimTeX's documentation :h vimtex for "imap", we see

INSERT MODE MAPPINGS *vimtex-imaps*

Some LaTeX commands are very common, and so it is both natural and convenient to have insert mode mappings/abbreviations for them. VimTeX therefore provides a list of such mappings that are enabled by default, see |g:vimtex_imaps_list|
...
It is of course possible to customize the list of mappings. First, one may specifically disable the entire imaps feature with |g:vimtex_imaps_enabled| or specific default mappings through |g:vimtex_imaps_disabled|. ...

Thus, we can remove these mappings with

  • either let g:vimtex_imaps_enabled = 0, which disables all imaps set by vimtex,
  • or let g:vimtex_imaps_disabled = ['B','-','c','f','b','/'] which disables only specific imaps.
3
  • You were exactly right! I am realy impressed with all of your vim/ulti snips knowledge!. I got it to work with the first of your suggestions, however, the second one doesn't seem to work. I added let g:vimtex_imaps_disabled = ['#'] to my .vimrc and it did not change the behaviour. Was I suppose to do that? Apologies for my incompetence with a lot of this... Commented Mar 31, 2022 at 19:59
  • I just realised that I was supposed to copy 'let g:vimtex_imaps_disabled = ['B','-','c','f','b','/']'. Thank you very much! You are amazing! My question has been answered. Commented Mar 31, 2022 at 20:00
  • @MathsWizzard Thank you for the kind words :) Well, many on this site surpass me, and I have benefited from their knowledge. Hopefully the pointers written here will be helpful when you encounter similar problems in future! Cheers!
    – husB
    Commented Apr 1, 2022 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.