Reputation: 8072
I am writing Python code using Vim, and every time I want to run my code, I type this inside Vim:
:w !python
This gets frustrating, so I was looking for a quicker method to run Python code inside Vim. Executing Python scripts from a terminal maybe? I am using Linux.
Upvotes: 163
Views: 215149
Reputation: 778
:!./%
run the current Python script, this will work only with the Shebang
You can also run it with arguments like that:
:!./% ARG1 ARG2
Upvotes: 1
Reputation: 21
I use this. It automatically saves the program and runs it into the shell. Comes back to vim after the program is run.
Works by pressing <F5>
in insert mode in vim.
Add the following code snippet in your vimrc file.
imap <F5> <Esc>:w<CR>:!python % [filename.py]; <CR>
I have just combined the two solutions previously provided to this question and it worked for me.
Thanks!
Upvotes: 2
Reputation: 1973
I recommend to use Vim-Slime plugin for "by-block" sending python code from vim to python console.
https://github.com/jpalardy/vim-slime
Upvotes: 2
Reputation: 161
Have you tried just using:
:!python nameofyourfile.py
example: https://www.youtube.com/watch?v=mjUxEX4OssM
It could just be your syntax, or it could be that you are missing pluggin or packages others have used. Are you using just plain terminal, or something else like MobaXterm? The above code works for running a python script using terminal inside vim (the output will show up inside the vim session).
Upvotes: 1
Reputation: 21
cd
vim .vimrc
nmap <F4> <Esc>:w<CR>:!clear;python %<CR>
ZZ
The next time you want to run your code, just press F4
in "normal mode". If you want to run your code in "insert mode", replace nmap
with imap
.
Upvotes: 2
Reputation: 195029
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 backslashTL;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
Upvotes: 227
Reputation: 2544
Think about using shebang line, so you will be able to use it with still any language, not only Python.
Adding shebang:
Add this to first line of your script:
#!/usr/bin/env python3
or this, if you are using Python 2:
#!/usr/bin/env python2
Vim keymap:
Add this to your ~/.vimrc
:
nmap <F7> :w<cr>:!clear;"%:p"<cr>
Make file executable:
Type in Vim:
:!chmod +x %
or in terminal:
chmod +x script_name.py
Explanation:
When F7 is pressed in normal mode, Vim will try to execute current file as bash script. Then bash interpreter will see shebang line and understand, that this file should be passed to Python (or any other programm if needed).
Also you will be able to run your script from terminal using it's name:
./script_name.py
instead of this way (that will work too):
python3 script_name.py
Upvotes: 5
Reputation: 4031
Plugin: jupyter-vim
So you can send lines (<leader>E
), visual selection (<leader>e
) to a running jupyter-client
(the replacement of ipython
)
I prefer to separate editor and interpreter (each one in its shell). Imagine you send a bad input reading command ...
Upvotes: 11
Reputation: 338
You can extends for any language with 1 keybinding with augroup command, for example:
augroup rungroup
autocmd!
autocmd BufRead,BufNewFile *.go nnoremap <F5> :exec '!go run' shellescape(@%, 1)<cr>
autocmd BufRead,BufNewFile *.py nnoremap <F5> :exec '!python' shellescape(@%, 1)<cr>
augroup END
Upvotes: 1
Reputation: 419
" run current python file to new buffer
function! RunPython()
let s:current_file = expand("%")
enew|silent execute ".!python " . shellescape(s:current_file, 1)
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
setlocal nobuflisted
endfunction
autocmd FileType python nnoremap <Leader>c :call RunPython()<CR>
Upvotes: 0
Reputation: 41
The accepted answer works for me (on Linux), but I wanted this command to also save the buffer before running, so I modified it slightly:
nnoremap <buffer> <F9> :w <bar> :exec '!python' shellescape(@%, 1)<cr>
The :w <bar>
saves the buffer THEN runs the code in it.
Upvotes: 3
Reputation: 613
You can use skywind3000/asyncrun.vim as well. It is similar to what @FocusedWolf has listed.
Upvotes: 2
Reputation: 503
Building on the previous answers, if you like to see the code while looking at its' output you could find :ter
(:terminal
) command useful.
autocmd Filetype python nnoremap <buffer> <F5> :w<CR>:ter python2 "%"<CR>
autocmd Filetype python nnoremap <buffer> <F6> :w<CR>:vert ter python3 "%"<CR>
Using vert
in the second line runs the code in vertical split instead of horizontal.
The negative of it is that if you don't close the split-window where the code ran you will have many splits after multiple runs (which doesn't happen in original python IDLE where the same output window is reused).
(I keep these lines inside /home/user/.vimrc
)
Upvotes: 17
Reputation: 10326
Just go to normal mode by pressing <esc>
and type:
! clear; python %
Step by step explanation:
!
allows you to run a terminal command
clear
will empty your terminal screen
;
ends the first command, allowing you to introduce a second command
python
will use python to run your script (it could be replaced withruby
for example)
%
concats the current filename, passing it as a parameter to thepython
command
Upvotes: 76
Reputation: 4364
Instead of putting the command mapping in your .vimrc
, put the mapping in your ~/.vim/ftplugin/python.vim
file (Windows $HOME\vimfiles\ftplugin\python.vim
). If you don't have this file or directories, just make them. This way the key is only mapped when you open a .py
file or any file with filetype=python
, since you'll only be running this command on Python scripts.
For the actual mapping, I like to be able to edit in Vim while the script runs. Going off of @cazyas' answer, I have the following in my ftplugin\python.vim
(Windows):
noremap <F5> <Esc>:w<CR>:!START /B python %<CR>
This will run the current Python script in the background. For Linux just change it to this:
noremap <F5> <Esc>:w<CR>:!python % &<CR>
Upvotes: 1
Reputation: 515
I have this in my .vimrc file:
imap <F5> <Esc>:w<CR>:!clear;python %<CR>
When I'm done editing a Python script, I just press <F5>
. The script is saved and then executed in a blank screen.
Upvotes: 39
Reputation: 20614
If you don't want to see ":exec python file.py
" printed each time, use this:
nnoremap <F9> :echo system('python2 "' . expand('%') . '"')<cr>
nnoremap <F10> :echo system('python3 "' . expand('%') . '"')<cr>
It didn't mess up my powerline / vim-airline statusbar either.
Upvotes: 5
Reputation: 49
I have this on my .vimrc:
"map <F9> :w<CR>:!python %<CR>"
which saves the current buffer and execute the code with presing only Esc + F9
Upvotes: 4
Reputation: 27
Put your cursor in the code somewhere. Right click and choose one of the "Select" choices to highlight your code. Then press Ctrl : and you will see the new prompt '<, >'
Now type !python and see if that works.
I just spend days trying to figure out the same problem!!! I used the coding:
s='My name'
print (s)
After I pulled out all my hair, I finally got it right!
Upvotes: 1
Reputation: 31
This .vimrc mapping needs Conque Shell, but it replicates Geany (and other X editors') behaviour:
Window closes automatically on exit
:let dummy = conque_term#subprocess('gnome-terminal -e "bash -c \"python ' . expand("%") . '; answer=\\\"z\\\"; while [ $answer != \\\"q\\\" ]; do printf \\\"\nexited with code $?, press (q) to quit: \\\"; read -n 1 answer; done; \" "')
Upvotes: 0
Reputation: 2732
For generic use (run python/haskell/ruby/C++... from vim based on the filetype
), there's a nice plugin called vim-quickrun. It supports many programming languages by default. It is easily configurable, too, so one can define preferred behaviours for any filetype if needed. The github repo does not have a fancy readme, but it is well documented with the doc file.
Upvotes: 3
Reputation: 944
If you want to quickly jump back through your :w
commands, a cool thing is to type :w
and then press your up arrow. It will only cycle through commands that start with w
.
Upvotes: 1
Reputation: 8072
A simple method would be to type :
while in normal mode, and then press the up arrow key on the keyboard and press Enter. This will repeat the last typed commands on VIM.
Upvotes: 1
Reputation: 12747
Keep in mind that you're able to repeat the last used command with @:
, so that's all you'd need to repeat are those two character.
Or you could save the string w !python
into one of the registers (like "a
for example) and then hit :<C-R>a<CR>
to insert the contents of register a
into the commandline and run it.
Or you can do what I do and map <leader>z
to :!python %<CR>
to run the current file.
Upvotes: 8