I want to be able to set up the hot key \\
to be able to write and run python script from VIM without each time having to type in
:w
:! python3 file.py
What I have done so far is pasted the following into my vimrc file:
"{{{ The following is for sourcing command.vim whenever exists.
" Function to source only if the file command.vim exists
" https://devel.tech/snippets/n/vIIMz8vZ/load-vim-source-files-only-if-they-exist
"
function! SourceIfExists(file)
if filereadable(expand(a:file))
echom a:file . " is about to be sourced."
exe 'source' a:file
endif
endfunction
autocmd BufEnter * call SourceIfExists("command.vim")
" }}}
and then in the same directory that my python file is, I create a file named command.vim and paste the following inside that file:
noremap <leader><leader> :w <cr> :!python3 % &<cr>
Now this almost works perfectly except for the following issue. Suppose I want to run the following script, which I'll call file.py:
import numpy as np
x = np.linspace(0, 5, 6)
y = np.linspace(6, 10, 5)
print('{}\n{}'.format(x, y))
If I run this the standard way by using :! python3 file.py
then the output is the following:
[0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]
Press ENTER or type command to continue
But, if I use the \\
method to run the same script, I get the following output:
Press ENTER or type command to continue[0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]
and you can see that the output of the script using \\
is not formatted nicely like it is when using the standard command :! python3 file.py
.
Anyone know how to fix this?
nnoremap <buffer><leader><leader> :w<bar>!python3 %<cr>
?command.vim
. Just add your mappings inafter/ftplugin/python.vim
.matplotlib.pyplot
and runplt.show()
, then I can no longer edit the code until I close the plot that was created. Before when using thecommand.vim
method, I did not need to close the plot to keep editing the code.:nnoremap <buffer><leader><leader>! :w! \| !python3 % & <cr> \| :redraw!<cr>
. After running your script, the plot is shown and you will be able to go back to your text file, however, if you run your script again, a new plot will be created. In my opinion, a workflow based on :make is better as you have more control over which plot will be displayed and by "recompiling" your file, the initial plot will be updated. Having said that, this is not the usual iterative workflow that is common in other applications such as jupyter notebook.