In my vimrc I have a key mapping for switching buffers that also automatically resizes the splits by calling a function:

nnoremap <silent> <M-h> <c-w>h:call Splitresize()<CR>

I also would like to automatically switch to insert mode when entering a terminal buffer, so I wrote:

autocmd BufEnter term:* startinsert

The problem is that when use <M-h> to change to a terminal buffer, the autocmd is executed before the function call, so that :call Splitresize() is printed in my terminal.

Is there a way to ensure that the function is called before the autocmd is executed?


1 Answer 1


Use <cmd> pseudo key to call a function without involving command-line mode (:h <cmd>)

For example:

func! Splitresize()
        echom "Resize!!!"

nnoremap <silent> <C-h> <c-w>h<cmd>call Splitresize()<CR>
nnoremap <silent> <C-l> <c-w>l<cmd>call Splitresize()<CR>

augroup test | au!
        autocmd! BufEnter term://* startinsert
augroup END

enter image description here

  • 1
    Is that two opportunities in one day you've had to use <cmd> in an answer? Pretty good for a command I don't recall seeing mentioned before. :)
    – B Layer
    May 25, 2021 at 12:55
  • 1
    @BLayer, indeed :) Recent vim also has it.
    – Maxim Kim
    May 25, 2021 at 12:55
  • Oh, awesome! I was just going to comment that I hope they port it there. Looks super handy.
    – B Layer
    May 25, 2021 at 13:06

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.