Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use Python in Vim?

Tags:

python

vim

I waste a lot of time between Vim and Python. I find it too slow to manually copy-paste from Python to Vim and vice versa. A good broken example is:

%!python for i in xrange(25); print 6*i \n

How can you do such tweaks direcly in Vim? [Solved]

[Clarification] I need things to Vim, like printing sequences, arithmetics... - things I cannot do in Vim.

[?] Can someone elaborate this point: "your script can read from stdin to operate directly on the lines given (., %, ...)."

[Further Clarification]

If I want to print 'Hello' to lines 4,5, 6 and 7, what is wrong:

:4-7!python -c "print 'hello'"

The dot . modifies the current line. Can I print on multiple lines 7, 32 and 99:

:7,32,99!python -c "print 'hello'"

Clearly not working. How?

like image 224
Léo Léopold Hertz 준영 Avatar asked Feb 01 '09 21:02

Léo Léopold Hertz 준영


People also ask

Which python is Vim using?

Currently -python and -python3 seem to be the default for the Debian vim package. If you need vim support for scripting languages, install vim-nox , which is dedicated to them and therefore has (among other things) +python3 enabled.

Should you use Vim for python?

Vim is an awesome text editor that can be used as a Python IDE. For those who use Vim and want to program in Python, here are a few tips on how to do it easily. Vim is a highly extensible text editor.

How do I build Vim with python support?

You need to compile Vim yourself or get a prebuilt Vim package that was compiled with Python support. If you're on a Debian based system, the easiest way is to download the vim-gnome or vim-gtk package from apt ( apt install vim-gtk for instance). Other distros might have a similar package with python support built in.


1 Answers

In any of your vim windows, type something like this:

for x in range(1,10):     print '-> %d' % x 

Visually select both of those lines (V to start visual mode), and type the following:

:!python 

Because you pressed ':' in visual mode, that will end up looking like:

:'<,'>!python 

Hit enter and the selection is replaced by the output of the print statements. You could easily turn it into a mapping:

:vnoremap <f5> :!python<CR> 
like image 156
too much php Avatar answered Oct 09 '22 01:10

too much php