Running Python Code in Vim

Running Python code in Vim

How about adding an autocmd to your ~/.vimrc-file, creating a mapping:

autocmd FileType python map <buffer> <F9> :w<CR>:exec '!python3' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F9> <esc>:w<CR>:exec '!python3' shellescape(@%, 1)<CR>

then you could press <F9> to execute the current buffer with python

Explanation:

  • autocmd: command that Vim will execute automatically on {event} (here: if you open a python file)
  • [i]map: creates a keyboard shortcut to <F9> in insert/normal mode
  • <buffer>: If multiple buffers/files are open: just use the active one
  • <esc>: leaving insert mode
  • :w<CR>: saves your file
  • !: runs the following command in your shell (try :!ls)
  • %: is replaced by the filename of your active buffer. But since it can contain things like whitespace and other "bad" stuff it is better practise not to write :python %, but use:
  • shellescape: escape the special characters. The 1 means with a backslash

TL;DR: The first line will work in normal mode and once you press <F9> it first saves your file and then run the file with python.
The second does the same thing, but leaves insert mode first

Running Python code in Vim without saving

You have already answered your own question:

:w !python

will run the file in python without saving it. Seriously, test it out yourself! make some changes, run :w !python and then after it runs, run :e!. It will revert all of your changes.

The reason this works is because :w does not mean save. It means write, and by default, it chooses to write the file to the currently selected file, which is equivalent to saving. In bash speak, it's like

cat myfile > myfile

But if you give an argument, it will write the file to that stream rather than saving. In this case, your writing it to python, so the file is not saved.


I wrote a much longer answer on this topic here.

Running python code interactively from vim

As @Doktor OSwaldo, @filbranden, amd @shaahiin suggested, vim-slime was a great choice (vim-ipython-cell has some additional scripts).

I downloaded the plugin using:

mkdir -p ~/.vim/pack/plugins/start
cd ~/.vim/pack/plugins/start
git clone https://github.com/jpalardy/vim-slime.git

Added the following to ~/.vimrc:

let g:slime_target = "tmux"
let g:slime_python_ipython = 1
filetype plugin on

Then used two tmux windows. One for vim, and the other for the iPython. using

cntrl-c + cntrl-c

commands could be send to the iPython session.

How to run python script inside vim?

command! -nargs=* Py !python % <args>

-nargs=* allows any number of arguments (0, 1, or many). <args> pastes command-line arguments into the command.

How to create a mapping to execute python code from Vim?

Interacting with command-line through vim, the much preferred way would be with exclamation.
Instead of:

:exec '!python' shellescape(@%, 1)<cr>

Try doing the easy way:

:update<bar>!python %<CR>

here in the above code:

update save file if modified (preferred) or can also use w.

<bar> pipe commands.

!python % will run the file as python filename.

So basically, put this in your .vimrc file, and that'll do it:

autocmd FileType python nnoremap <buffer> <F9> :update<bar>!python %<CR>

On Vim, error running simplest python code

It looks like you're running SpaceVim from you mentioning the SPC l r shortcut to run the current program.

Also, it looks like you're writing Python 3 code, from your comment indicating a simple print("Hello, World") is failing.

SpaceVim defaults to Python 2, so that's probably why you're getting a syntax error when you try to run your code.

Add the following line to the start of your script:

#!/usr/bin/python3

SpaceVim will recognize that line and use the Python interpreter referred by that line when running your script.



Related Topics



Leave a reply



Submit