How can I automatically evaluate a script and put the results in an existing window after saving to Vim?

My ideal scenario would be for Vim to split into two windows - first containing the script (python) I'm working on now, and the other is the result of evaluating this script. This is what I have so far:

:autocmd BufWritePost *.py redir @p | execute 'silent w !python -' | redir END 

When the script is saved, the contents of the script are transferred to the python command by protocol, the output of this command is stored in the p register. What is the best way to get p into a new / empty buffer displayed in another window?

Some things I tried are blast | normal! "pp | bfirst blast | normal! "pp | bfirst blast | normal! "pp | bfirst (blast: new / empty buffer, bfirst: buffer containing the python script), but that seems to leave me in the" output "buffer and for some reason I lose syntax highlighting and need to flip between buffers so that bring them back in. I would really like to do it all in its place and not generate a temp dump file, where I connect the output of the script and prefer to avoid using any other external tools to "view" the python script file and do something, when it changes.

+4
source share
1 answer

My approach was to use the preview window. This allows you to always move to the desired window no matter how many windows you have. It also allows you to β€œignore” the fact that the preview window is open when you want to exit vim (you can just do :q , not :qa ).

 autocmd BufWritePost *.py call OutputWindow("python") autocmd BufWritePost *.rb call OutputWindow("ruby") function OutputWindow(executable) let filename=expand('%:p') " move to preview window and create one if it doesn't " yet exist silent! wincmd P if ! &previewwindow " use 'new' instead of 'vnew' for a horizontal split vnew set previewwindow endif execute "%d|silent 0r!" . a:executable . " " . filename wincmd p endfunction 

There may be a less sure way to accomplish this, but it works very well as it is.

+3
source

Source: https://habr.com/ru/post/1397719/


All Articles