Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Vim for Python

I really like the Emacs editor for Python because of its smart tabbing for instance if I have something like this

def foo():     if bar:          blah          [b]eep 

and I press tab on the cursor (which is on the b of beep), it will not insert a new tab causing a syntax error but it would toggle through the possible levels that beep can be on. Is there any way of getting this effect on Vim?

like image 985
Doboy Avatar asked Feb 07 '12 08:02

Doboy


People also ask

Can you use VIM for python?

Using Vim as a Python IDEThe python-mode project is a Vim plugin with syntax highlighting, breakpoints, PEP8 linting, code completion and many other features you'd expect from an integrated development environment.

How do I create a python file in Vim?

Write Your Python Script To write in the vim editor, press i to switch to insert mode. Write the best python script in the world. Press esc to leave the editing mode. Write the command :wq to save and quite the vim editor ( w for write and q for quit ).

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.


2 Answers

In general, vim is a very powerful regular language editor (macros extend this but we'll ignore that for now). This is because vim's a thin layer on top of ed, and ed isn't much more than a line editor that speaks regex. Emacs has the advantage of being built on top of ELisp; lending it the ability to easily parse complex grammars and perform indentation tricks like the one you shared above.

To be honest, I've never been able to dive into the depths of emacs because it is simply delightful meditating within my vim cave. With that said, let's jump in.

Getting Started

Janus

For beginners, I highly recommend installing the readymade Janus plugin (fwiw, the name hails from a Star Trek episode featuring Janus Vim). If you want a quick shortcut to a vim IDE it's your best bang for your buck.

I've never used it much, but I've seen others use it happily and my current setup is borrowed heavily from an old Janus build.

Vim Pathogen

Otherwise, do some exploring on your own! I'd highly recommend installing vim pathogen if you want to see the universe of vim plugins.

It's a package manager of sorts. Once you install it, you can git clone packages to your ~/.vim/bundle directory and they're auto-installed. No more plugin installation, maintenance, or uninstall headaches!

You can run the following script from the GitHub page to install pathogen:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \ curl -so ~/.vim/autoload/pathogen.vim \     https://raw.github.com/tpope/vim-pathogen/HEAD/autoload/pathogen.vim 

Helpful Links

Here are some links on extending vim I've found and enjoyed:

  • Turning Vim Into A Modern Python IDE
  • Vim As Python IDE
  • OS X And Python (osx specific)
  • Learn Vimscript The Hard Way (great book if you want to learn vimscript)
like image 160
mvanveen Avatar answered Sep 17 '22 14:09

mvanveen


For those arriving around summer 2013, I believe some of this thread is outdated.

I followed this howto which recommends Vundle over Pathogen. After one days use I found installing plugins trivial.

The klen/python-mode plugin deserves special mention. It provides pyflakes and pylint amongst other features.

I have just started using Valloric/YouCompleteMe and I love it. It has C-lang auto-complete and python also works great thanks to jedi integration. It may well replace jedi-vim as per this discussion /davidhalter/jedi-vim/issues/119

Finally browsing the /carlhuda/janus plugins supplied is a good guide to useful scripts you might not know you are looking for such as NerdTree, vim-fugitive, syntastic, powerline, ack.vim, snipmate...

All the above '{}/{}' are found on github you can find them easily with Google.

like image 44
Greg Avatar answered Sep 19 '22 14:09

Greg