I know there'a probably a plugin for that, but since Neovim has a built-in terminal I wanna use it. I just want to bind a key that runs the current file in Python 3. I tried using @% and %, but the shell does not recognize these variables. I think the best way would be to export the current file path as an environment variable, and make use it on the terminal. How can I do that?

  • 3
    do :term % or :term python % not work?
    – Mass
    Mar 4, 2018 at 17:31

2 Answers 2


I am myself pretty new to vim in general, but I guess you can easily do this. Here is my version of it, which will create a horizontal split, create a terminal buffer and then run the current python file in it and then you can create a another mapping which can close the terminal buffer for you.

nnoremap <C-R> :sp <CR> :term python % <CR>
nnoremap <C-W> :bd!<CR>
  • 1
    I thought that <C-R> and <C-W> respectively differentiates from <C-r> and <C-w> by a Shift key, but they don't. This solution, without modification, shadows some crucial key bindings. Aug 12, 2020 at 10:45
  • @UtkanGezer can you please clarify what you mean by this? Are there system key bindings (for <C-R> and <C-W>) that you are referring to? If not, then wouldn't this solution (suggested by @peterh) be okay?
    – Vishal
    Mar 25 at 22:09
  • 1
    @Vishal I might have said that because <C-r> is the default key binding to redo what has been undone, and it seems (according to my old comment) that <C-R> refers to the same key combination, therefore shadowing that default and crucial function when rebound. It's been a long time, and I don't remember what exactly my motivation was back then, but this might be it. Mar 28 at 9:07

The best solution for me:

" Create a function to open a neovim terminal in a small split window and run python 
function! Termpy()
  exec winheight(0)/4."split" | terminal python3 %

Then use the solution above to map it to your prefered key combination:

" Press CTRL+R to run python script into separate term window 
nnoremap <C-R> :call Termpy() <CR>

Other cool things you can do is:

Use a command like :Pyrun

command Pyrun execute ":w<CR>:vert ter python3 "%"<CR>"

Use leader + p to save and run the script

map <leader>p :w<CR>:!python3 %<CR>

python2 and python3 are different,so change it for your projects or you can create different functions


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.