1

I tried executing command by putting below

fun! DeoEnter()
    if pumvisible()
        if vsnip#available(1)
           " wanna exec function here, like `exe 'normal <Plug>(vsnip-jump-next)'`
        endif

        call UltiSnips#ExpandSnippetOrJump()
        if g:ulti_expand_or_jump_res > 0
            return ""
        endif

        return "\<C-y>"
    endif

    return "\n"
endfun
ino <CR> <C-R>=DeoEnter()<CR>

, however, command actually never worked because it is normal mapping, but I wanna accomplish insert mapping.

I looked up document but it's written only normal mode. Is there an any good idea?

1
  • this worked! and this one is what I needed because I tried to exec directly. Thank you very much. Commented Apr 21, 2020 at 6:24

2 Answers 2

1

Since your function is returning keystrokes to execute in insert mode already (through the <C-r>=... mechanism), you can do the same for this one.

You can return a double quoted string and use backslashes to escape key sequences (same as you're already doing for "\<C-y>" later in that function.)

So you can use \<Plug> to enter the <Plug> "key". Even if it's not a real key, it behaves the same as one.

Since the <Plug>(vsnip-jump-next) needs to be executed in Normal mode, you can use <C-o> which allows you to execute a single Normal mode command, from Insert mode, and return to Insert mode when that command completes.

So this line will work over there:

return "\<C-o>\<Plug>(vsnip-jump-next)"

Putting it all together:

fun! DeoEnter()
    if pumvisible()
        if vsnip#available(1)
           return "\<C-o>\<Plug>(vsnip-jump-next)"
        endif

        call UltiSnips#ExpandSnippetOrJump()
        if g:ulti_expand_or_jump_res > 0
            return ""
        endif

        return "\<C-y>"
    endif

    return "\r"
endfun
ino <CR> <C-R>=DeoEnter()<CR>

I also changed the last line of your function from return "\n" to return "\r" since that's actually the code that the "Enter" key will produce. (I believe "\n" would have worked too, but "\r" is more correct in this spot.) You could have also used return "\<CR>", which is equivalent to "\r" but more explicit about the fact you're sending the code for the "Enter" key, that's even better.

Sign up to request clarification or add additional context in comments.

2 Comments

The answer is great, and the explaination is great as well. Thank you very much again for your answer.
@ryuta69 Glad I could help! While we're at it, I'd like to add that you might want to check out the Vi and Vim StackExchange website for questions on Vim, you're likely to find great answers for your Vim questions over there!
1

You could do something like:

ino <CR> :exe "a".DeoEnter()<CR>

This way, the result of the DeoEnter function will be entered in insert mode

3 Comments

I fail to see how this is different from ino <CR> <C-R>=DeoEnter()<CR>, where the result of the DeoEnter function is also entered in insert mode. I mean, they do slightly different things and it's quite possible that it changes behavior enough for an exe 'normal <Plug>...' command with a jump to succeed in the latter while it fails in the former. But "will be entered in insert mode" is not a valid explanation for why it works.
Very true @filbranden, and after testing a bit, I have to admit that I don't understand why my solution worked, while the one in the Original post didn't...
@Zorzi Sometimes there's a restriction for executing a normal mode command in a function. For example, that's the case with imap <expr>, you can't have the function run normal mode commands or execute motions... But I believe that's not the case with <C-R>=..., I think that one is totally allowed to do anything it wants to.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.