1

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

3

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.

3
  • 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
    Commented Jan 8, 2022 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
    Commented Jan 8, 2022 at 16:59
  • changing | to <bar> worked perfect - thank you :)
    – mama
    Commented Jan 9, 2022 at 15:05
1

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
    tabnew
    norm "xp
endfunction

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

6
  • You may choose to do new or vnew instead of tabnew.
    – eyal karni
    Commented Jan 6, 2022 at 22:23
  • You may need to remove the silent.
    – eyal karni
    Commented Jan 6, 2022 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
    Commented Jan 6, 2022 at 22:42
  • I don't understand the :r !python FN command. should FN represent the file of the previous buffer?
    – mama
    Commented Jan 8, 2022 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
    Commented Jan 8, 2022 at 15:38

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.