How to read and/or save Python output in VIM

佐手、 提交于 2020-01-14 05:49:20

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18239751/how-to-read-and-or-save-python-output-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!