PYTHON VIM
###########
###########

Here are my recommendations to programming with vim for python

Whats covered:
* pyflakes – to catch errors (only when in normal mode, not while typing, so you have to keep pressing escape)
* intellisense – see available methods and variables – need to have a good compiled version of vim (vim-gnome does that job, so apt get it – all this is in instructions)
* also some colorschemes to set the mood
* also how to see indents
* also my vimrc file (first off note there is a global one in /etc/vim/vimrc that affects every user and then there is the per user ones in home dir ~/.vimrc)

See Indents or Tabs

Vim has a cool built in way to see indents, since python doesnt like indents and prefers 4 spaces to indents, you can see indents with this

They show up as ^I (which looks off color if you have syntax on and some colorscheme) , so erasing a ^I erases a tab (4 or 8 spaces worth depending on your tab size, in mine its 4)

Note $ represents end of line

INTELLISENSE support

See what vim is compiled with –version (+ means compiled to have it, and – means not compiled to have it), the default vim (apt-get install vim) is not compiled for python intellisense support. 

+ supported (will have “intellisense”)
– not supported (will have “intellisense”)

make sure +python (meaning its supported for version +2)
-python3 is okay to have (you still get “intellisense”) (so not having python3 support is okay if all your using is just typical python 2)
you can still get good useability (probably even if using python3)

if you want +python3 and regular +python you will need to recompile vim (or find an apt-get of vim that has all of the options compiled – vim-gnome has python but not python3)

If missing +python or +python3 then do this:

your config in /etc/vim/vimrc will still be there
your plugins in ~/.vim/ will still be there
Also i dont have gui i work all from terminal(ssh) and this works (its not like a its gui/gnome only vim)

pyflakes – program and vim plugin install and use

To install

just running pyflakes alone can see errors (vim plugin utilizes this):

example:

To install vim plugin

See what files/dir are there:

You should just see these enteries:

Let copy them to plugins directory (you can see that there are other generic ones so all users can have it if you type :scriptnames in vim, but per user install is recommended either way):

To enabled plugins need to edit vimrc (also each user has one ~/.vimrc, my root user doesnt, but just edit the global one, so every user gets this – so every user gets plugins enabled essentially – even if they dont have pyflakes, these next “filetype” options in vimrc will not be bad to have)

Add these lines to bottom:

To use pyflakes in vim:

Edit a python file

Type :cc ENTER to see next error and see number of errors

When not in insert mode (hit ESCAPE to ensure that)
Type :cc ENTER
That will show first error, and cycle thru them, repeat :cc ENTER
:cc 1 — goes to first error
:cc 2 — goes to second error
:cc # — goes to # error
When you type :cc you see the number of errors, so you know what number to type (count starts at 1)

NOTE: this is where pyflakes.vim is, notice that there are ziped version – dont download those, as they include the pyflakes python module (which we already apt-getted) http://www.vim.org/scripts/script.php?script_id=2441
TO SEE WHAT PLUGINS LOADED

when viewing any help it will open a new section in vim and you wont be in your file, so type this to get out of help and get back to your file

:q (if you open help exit out of it like this)

Also the other options open like a “subshell”/process where its not quiet a section, but it show you output, you can get back to your work by pressing q or enter. arrow keys and space bar to navigate thru output

INTELLISENSE – how to use

vim somefile.py

In insert mode, while typing, just type below

CONTROL-X CONTROL-O to see pop up of variables and functions
CONTROL-N and CONTROL-P to navigate thru options

IF INTELLISENSE NOT WORKING STILL

I believe I had to put this to work (in my vimrc – see below – i have this)

Try putting this into your vimrc file, for me it worked without it

More colorschemes for vim

The default darkblue and default are just not good enough for my taste with python (the red color of pyflakes errors covers up the text too much, anyhow so try these out). Even after trying these, I ended up sticking with something that comes in every install “elflord” (see my vimrc config below)

fix files that might have ^M at end (CR+LF from windows/dos line endings, instead of just having LF at end of each line which linux needs)

To use in vim:

NOTE: some of those downloaded colorschemes didnt work for me at all, and some didnt work only in ssh/terminal

My VIMRC (global one)

Im not showing you the comment (using the greps )

NOTE ANOTHER TRICK – force loading intellisense

I didnt need to use this – as my intellisense worked but if yours doesnt

This is to force set your omnifunctions, mine just work automatincally (thats with install of vim-gnome)

Add these

 

##########################################
CHANGE KEY IN INTELLISENSE – didnt do this
##########################################

http://belchak.com/2011/01/31/code-completion-for-python-and-django-in-vim/

USEFULL KEY REMAP:
Put this in /etc/vim/vimrc at the bottom

 <C-p> – Shows a list of all local symbols. This is good if you do not have a tags file associated with the file you are editing.

<C-space> – Shows a list of all available symbols. You need to set up a tags file, which is outside the scope of this blog post

<C-x><C-o> – The original keystroke sequence that we re-mapped C-space to.

<Tab> – The all-powerful tab!

Leave a Reply

Your email address will not be published. Required fields are marked *