I am using this in my vimrc, to get output of current script printed on the screen when I click enter.

autocmd FileType python map <buffer> <CR> :w<CR>:!python3 %<CR>

How can I get it to open in a new buffer where I can edit the output?

2 Answers 2


Use a :terminal:

" in ~/.vim/after/ftplugin/python.vim

nnoremap <buffer> <CR> :write <bar> terminal python3 %<CR>

" update b:undo_ftplugin

When the command is finished, you can :set modifiable (:set ma) and edit.

  • It didn't work for me but I changed it to autocmd FileType python map <buffer> <CR> :w<CR> :terminal python % <CR> and now it works - Thank you very much!!
    – mama
    Jan 8 at 15:33
  • @mama wdym didnt work? You do need at least :filetype plugin on for ftplugins to work. See my edit about the bar though
    – D. Ben Knoble
    Jan 8 at 16:59
  • changing | to <bar> worked perfect - thank you :)
    – mama
    Jan 9 at 15:05

You may run

autocmd FileType python map <buffer> <CR> :w<CR>:call Exec('!python3 '.expand('%'))<CR>

And use:

function! Exec(cmd)
    redir @x
    exec printf('silent %s',a:cmd)
    redir END
    norm "xp

It writes the output of a command in a new tab.

  • You may choose to do new or vnew instead of tabnew.
    – eyal karni
    Jan 6 at 22:23
  • You may need to remove the silent.
    – eyal karni
    Jan 6 at 22:25
  • 1
    This can also be achieved by first opening a buffer and doing :r !python FN . But I had this ready..
    – eyal karni
    Jan 6 at 22:42
  • I don't understand the :r !python FN command. should FN represent the file of the previous buffer?
    – mama
    Jan 8 at 15:23
  • I really like the idea of getting the output in vnew it reminds me of something like emacs "something mode" where you always get the output of your code right next to the code. 2 problems is that it would be nicer if the output came in the right side instead of the left. (probably that's an easy fix) The other is that it will always spawn a new buffer. It would be nice if it could reuse the old one. Maybe thats a future question I will ask about. Thanks for the answer.
    – mama
    Jan 8 at 15:38

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.