ryuta69
ryuta69

Reputation: 147

How to exec Vim <Plug> command in function when insert mode?

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?

Upvotes: 1

Views: 1430

Answers (2)

filbranden
filbranden

Reputation: 8898

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.

Upvotes: 1

Zorzi
Zorzi

Reputation: 792

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

Upvotes: 1

Related Questions