I'm trying to execute simple commands in the vim terminal mode, but cannot get it to work. For example, I tried

nmap <F12> :let $VIM_DIR=expand('%:p:h')<CR>:terminal<CR>cd $VIM_DIR<CR>

to open a terminal in the directory of the current file, but the final <CR> is ignored. I also tried term_sendkeys("","\<CR>") but that didn't have any noticeable effect at all.

  • For me this works, but only if I map it to a function key below F10, very strange...
    – Bart
    Jan 2, 2019 at 10:46
  • @Bart for me it never works
    – Bananach
    Jan 2, 2019 at 13:56
  • Is it Vim or Neovim?
    – Matt
    Jul 11, 2020 at 15:58
  • @Matt this is Vim
    – Bananach
    Jul 11, 2020 at 19:30

1 Answer 1


I tested that this works. It assumes that the terminal is opened in buffer 2.
You can check which buffer the terminal is open in with :ls.

:let  $VIM_DIR=expand('%:p:h')
:call term_sendkeys(2, "cd $VIM_DIR\n")
:call term_sendkeys(2, "pwd\n")

Also your initial command worked for me.

nmap <F12> :let $VIM_DIR=expand('%:p:h')<CR>:terminal<CR>cd $VIM_DIR<CR>

It maybe that you are testing this mapping using keys that are already mapped. You can review the nmap mappings with:


Or a specific mapping with:

:verb map <F12>

verb (verbose) tells you where the mapping was defined (file and line).

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.