1

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 '18 at 17:31
1

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>
0

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 %
endfunction

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

New contributor
Eazy Snatch is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.