Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim as a python ide

Tags:

python

vim

Python support is integrated in the latest versions of emacs. For example C-c C-z provides me an interpreter and C-c C-c automatically interprets the file I'm working on without moving to the other buffer. (Although there are some downsides to emacs's approach)

Is this possible in vim, or as easy done as in emacs with just two keystrokes? I know I can evaluate an expression with :python, but it is not exactly what I desire.

like image 689
osolmaz Avatar asked Jan 25 '13 18:01

osolmaz


1 Answers

To execute the current file in python, you can use the command :!python %. You can bind this to a keyboard shortcut by editing your vimrc. For example, adding nnoremap \ll :!python %<cr> to your vimrc will execute the current file in python when you type \ll in normal mode. (* see footnote for more details).

The vim-ipython plugin lets you open an ipython window in vim. You may also be interested in tmux, which allows you to split your terminal in two, vertically (so you can have a shell and vim running in parallel).

There are plenty of plugins that can turn vim into a really good python IDE. "pyflakes", which automatically highlights syntax errors, is a particular favorite of mine.

This blog post describes vim plugins for python extensively:

http://sontek.net/blog/detail/turning-vim-into-a-modern-python-ide


(*) As an aside, you may want to make this command specific to python files (it doesn't really make sense to execute C++ source in a python interpreter). This can be done either by putting it in a specific python.vim file in your .vim/ftplugin directory, or by writing

autocmd FileType python nnoremap \ll :!python %<cr>

in your .vimrc. That way, you can rebind the \ll keyboard shortcut to different actions for different types of file.

As a second aside, if you just want to execute a section of the current file, select the relevant lines in visual mode (SHIFT+v) and type the :!python % command. The lines selected will get piped to the python interpreter!

like image 73
Pascal Bugnion Avatar answered Oct 28 '22 18:10

Pascal Bugnion