ultisnips
ultisnips copied to clipboard
Cannot jump to next placeholder when expanding snippet from an autocmd
I'm trying to make a command that expands a snippet when you open a file, the command i came up with is:
autocmd BufNewFile * execute "normal! imysnippet\<C-r>=UltiSnips#ExpandSnippet()\<CR>"
and the snippet im using for testing is
snippet mysnippet
#
${1}
#
${2}
#
${3}
endsnippet
Expected behavior:
press 2 (or whatever mapping is in g:UltiSnipsJumpForwardTrigger) to go to the next placeholder
Actual behavior: nothing happens
Steps to reproduce
- install the Ultisnips plugin
- create an autocommand that expands a snippet on BufNewFile
- define a snippet for the file you want, in this case "mysnippet", with multiple placeholder jumps
- create a new file, the snippet should now have expanded
- press
2to go to the next placeholder
My minimal neovim config
call plug#begin('~/.vim_plugged')
Plug '/src/UltiSnips'
Plug 'honza/vim-snippets'
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsListSnippets="9"
let g:UltiSnipsJumpForwardTrigger="2"
let g:UltiSnipsJumpBackwardTrigger="1"
let g:UltiSnipsEditSplit="vertical"
call plug#end()
autocmd BufNewFile * execute "normal! imysnippet\<C-r>=UltiSnips#ExpandSnippet()\<CR>"
- Operating System: OS: GNU/Linux Solus 4.0
- Vim Version: Vim version: Neovim v0.5.0-nightly
- UltiSnips Version: Ultisnips version: 3.2
- Python inside Vim: python3: 3.7.7 python: 2.7.18
- Docker repo/vimrc: https://github.com/Akuseru1/ultisnips/tree/reproduce_problem
@Akuseru1 ExpandSnippet doesn't work from :normal, see https://github.com/junegunn/fzf.vim/issues/796#issuecomment-738448709 and https://github.com/junegunn/fzf.vim/pull/1196
Been fighting with this whole day yesterday. :-(
See also https://github.com/SirVer/ultisnips/commit/1ca82f76f7a4e7e9beac77f74bda99457bbc8286#r44803007
@Akuseru1 ExpandSnippet doesn't work from
:normal, see junegunn/fzf.vim#796 (comment) and junegunn/fzf.vim#1196 Been fighting with this whole day yesterday. :-(
That would explain the weird behaviors I've been getting, @SirVer any chance of this being fixed?
@Akuseru1 unlikely, UltiSnips contains a bunch of heuristics that depend on keys being entered, normal delays when Vim is running certain buffer drains and that messes with UltiSnips state tracking. So this is very hard to fix unfortunately.
I'm wondering if perhaps using mode(1) instead of mode() at https://github.com/SirVer/ultisnips/blob/8554371b57c8989cf73f73f288c456fb3f2a3a3a/pythonx/UltiSnips/vim_helper.py#L187
might make it safe to trigger the expansion by using call UltiSnips#ExpandSnippet() which would make it easier to make bindings such as this. I'll probably follow @SirVer's suggestion in https://github.com/SirVer/ultisnips/commit/1ca82f76f7a4e7e9beac77f74bda99457bbc8286#commitcomment-44833981 and try if it breaks any tests...
Anyway, to achieve what @Akuseru1 wants, it would probably suffice to use startinsert/feedkeys in his mapping.
I didn't get to trying to fix the mode() issue and running the tests yet, but in the meantime I discovered that vim 8.2.1978 added <Cmd> that can be used instead of <C-\><C-O> in insert mode mappings and then mode() no longer causes any problems with UltiSnips. This is mostly irrelevant to this issue, just vaguely related.
What is relevant, though, is that I also found that invoking UltiSnips#ExpandSnippet() after inserting the snippet name in a script sometimes causes glitches, because UltiSnips needs to update its state after typing, and that doesn't happen after normal! imysnippet or even after startinsert and feedkeys("mysnippet"). The solution is to also invoke UltiSnips#CursorMoved(), like here: https://github.com/junegunn/fzf.vim/pull/1196/commits/9bc5b6570c2fc7df67a2c71d22c9917e2b003879
So if you encounter any glitches, give this a try!