0

I'm new to VIM and am using this command to save and run a Python script:

:w !python

However, I can't move up and down to read the output. The only option I am given is to press enter or enter a command. I tried to yank(:%y+) everything, but the actual code is yanked and not the output. I would prefer to be able to read all the output displayed in VIM and even better would be opening a new tab with the output and being able to search and read through all of it.

2
  • I think you have to save using :w and then go back and :!python exec.py because it doesnt save when I try it. And when I do that I get proper output displayed in vim, all of it. Commented Aug 14, 2013 at 18:48
  • Try :read !python /path/to/myscript.py Commented Jun 20, 2022 at 14:33

2 Answers 2

2

You can use redirection just like you normally would to write the output of python running the script into a different file. For instance:

:w !python > temp

And then

:tabnew temp

Not sure if there's a way to write the output directly to another buffer or not.

Another option is to save your script to a file (say "script.py") and then switch to your other buffer where you want to see the output, and then filter it like:

:%!python script.py

It will replace the entire contents of the buffer with the output of your script.

Sign up to request clarification or add additional context in comments.

5 Comments

Works great! Any way to make it into one line? The second one (%!python script.py) gives me the same issues. Also, what is the % for? I used : instead.
I don't think there is a way to do it in one line. Normally, you could separate multiple commands with a |, but the vim help says that this isn't possible for the write ! command because it sees the bar as part of it's arguments. But once you open temp in a new tab, you can just leave it open and reload it when it changes.
The % at the start of the last code snippet was sort of a mistake, I forgot to incldue the :. The % just means to filter the entire buffer, as opposed to a specific range. It may not be necessary if you just do :!, but it does work.
Getting back to doing it without two commands: if you leave the output file (temp) open in another buffer, you can set the autoread option on that buffer and it will automatically reload it any time it changes.
I tried to do :set autoread on the buffer, didn't work though.
0

Place this function in your vimrc. It will open a window with the captured output (for any command, not just python).

function! Redir(cmd)
  for win in range(1, winnr('$'))
    if getwinvar(win, 'scratch')
      execute win . 'windo close'
    endif
  endfor
  if a:cmd =~ '^!'
    let output = system(matchstr(a:cmd, '^!\zs.*'))
  else
    redir => output
    execute a:cmd
    redir END
  endif
  botright vnew
  let w:scratch = 1
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, split(output, "\n"))
endfunction
"`:Redir` followed by either shell or vim command
command! -nargs=+ -complete=command Redir silent call Redir(<q-args>)

Also in do :help python in vim. There are also plugins for python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.