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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
:set list --- to enable seeing the extra whitespace chars
:set nolist --- to disable seeing the extra whitespace chars
:set list --- to enable seeing the extra whitespace chars :set nolist --- to disable seeing the extra whitespace chars
:set list  --- to enable seeing the extra whitespace chars
:set nolist --- to disable seeing the extra whitespace chars

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. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vim --version
vim --version | grep python
vim --version vim --version | grep python
vim --version
vim --version | grep python

+ 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apt-get install vim-gnome<span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>
apt-get install vim-gnome<span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>
apt-get install vim-gnome<span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apt-get install pyflakes
# or install it with pip if you have that
pip install pyflakes
apt-get install pyflakes # or install it with pip if you have that pip install pyflakes
apt-get install pyflakes
# or install it with pip if you have that
pip install pyflakes

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

example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# pyflakes /root/scripts/orgl3/orgl3.py
/root/scripts/orgl3/orgl3.py:33: 'shutil' imported but unused
/root/scripts/orgl3/orgl3.py:86: local variable 't' is assigned to but never used
/root/scripts/orgl3/orgl3.py:93: undefined name 'time'
/root/scripts/orgl3/orgl3.py:93: undefined name 'f1'
# pyflakes /root/scripts/orgl3/orgl3.py /root/scripts/orgl3/orgl3.py:33: 'shutil' imported but unused /root/scripts/orgl3/orgl3.py:86: local variable 't' is assigned to but never used /root/scripts/orgl3/orgl3.py:93: undefined name 'time' /root/scripts/orgl3/orgl3.py:93: undefined name 'f1'
# pyflakes /root/scripts/orgl3/orgl3.py
/root/scripts/orgl3/orgl3.py:33: 'shutil' imported but unused
/root/scripts/orgl3/orgl3.py:86: local variable 't' is assigned to but never used
/root/scripts/orgl3/orgl3.py:93: undefined name 'time'
/root/scripts/orgl3/orgl3.py:93: undefined name 'f1'

To install vim plugin

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd ~
mkdir src
cd src
git clone http://github.com/kevinw/pyflakes-vim
cd pyflakes-vim/ftplugin
cd ~ mkdir src cd src git clone http://github.com/kevinw/pyflakes-vim cd pyflakes-vim/ftplugin
cd ~
mkdir src
cd src
git clone http://github.com/kevinw/pyflakes-vim
cd pyflakes-vim/ftplugin

See what files/dir are there:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
find
find
find

You should just see these enteries:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.
./python
./python/pyflakes.vim
./python/pyflakes
. ./python ./python/pyflakes.vim ./python/pyflakes
.
./python
./python/pyflakes.vim
./python/pyflakes

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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir -p ~/.vim/ftplugin/python/
cp -Rp * ~/.vim/ftplugin/python/
mkdir -p ~/.vim/ftplugin/python/ cp -Rp * ~/.vim/ftplugin/python/
mkdir -p ~/.vim/ftplugin/python/
cp -Rp * ~/.vim/ftplugin/python/

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vim /etc/vimrc/vimrc
vim /etc/vimrc/vimrc
vim /etc/vimrc/vimrc

Add these lines to bottom:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
" COMMENT START WITH DOUBLE QUOTES - ADDED BY ME FOR PLUGINS
filetype on
filetype plugin on
" COMMENT START WITH DOUBLE QUOTES - ADDED BY ME FOR PLUGINS filetype on filetype plugin on
" COMMENT START WITH DOUBLE QUOTES - ADDED BY ME FOR PLUGINS
filetype on
filetype plugin on

To use pyflakes in vim:

Edit a python file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vim test.py
vim test.py
vim test.py

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
:scriptnames --- see scripts
:function --- see function list
:command --- see command list
:verbose set --- see all of the set options (including omnifunc, so can know what intellisense module is loaded)
:verbose set history? --- reveals value of history and where set
:function --- list functions
:func SearchCompl --- List particular function
:help <plugin name> --- put in plugin name for <plugin name> and you will get help on plug in (in help type :q to get out out of extra window)
:scriptnames --- see scripts :function --- see function list :command --- see command list :verbose set --- see all of the set options (including omnifunc, so can know what intellisense module is loaded) :verbose set history? --- reveals value of history and where set :function --- list functions :func SearchCompl --- List particular function :help <plugin name> --- put in plugin name for <plugin name> and you will get help on plug in (in help type :q to get out out of extra window)
:scriptnames --- see scripts
:function --- see function list
:command --- see command list
:verbose set --- see all of the set options (including omnifunc, so can know what intellisense module is loaded)
:verbose set history? --- reveals value of history and where set
:function --- list functions
:func SearchCompl --- List particular function
:help <plugin name> --- put in plugin name for <plugin name> and you will get help on plug in (in help type :q to get out out of extra window)

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
set omnifunc=syntaxcomplete#Complete
set omnifunc=syntaxcomplete#Complete
set omnifunc=syntaxcomplete#Complete

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd /usr/share/vim/vim73/colors
wget https://raw.github.com/garybernhardt/dotfiles/master/.vim/colors/grb256.vim
wget http://www.vim.org/scripts/download_script.php?src_id=4128 -O guardian.vim
wget https://raw.github.com/Lokaltog/vim-distinguished/develop/colors/distinguished.vim
wget http://www.vim.org/scripts/download_script.php?src_id=12456 -O github.vim
wget https://raw.github.com/nanotech/jellybeans.vim/master/colors/jellybeans.vim
wget https://raw.github.com/ryanb/dotfiles/master/vim/colors/railscasts.vim
wget http://www.vim.org/scripts/download_script.php?src_id=10496 -O twilight.vim
wget https://raw.github.com/tpope/vim-vividchalk/master/colors/vividchalk.vim
wget http://www.vim.org/scripts/download_script.php?src_id=817 -O candy.vim
cd /usr/share/vim/vim73/colors wget https://raw.github.com/garybernhardt/dotfiles/master/.vim/colors/grb256.vim wget http://www.vim.org/scripts/download_script.php?src_id=4128 -O guardian.vim wget https://raw.github.com/Lokaltog/vim-distinguished/develop/colors/distinguished.vim wget http://www.vim.org/scripts/download_script.php?src_id=12456 -O github.vim wget https://raw.github.com/nanotech/jellybeans.vim/master/colors/jellybeans.vim wget https://raw.github.com/ryanb/dotfiles/master/vim/colors/railscasts.vim wget http://www.vim.org/scripts/download_script.php?src_id=10496 -O twilight.vim wget https://raw.github.com/tpope/vim-vividchalk/master/colors/vividchalk.vim wget http://www.vim.org/scripts/download_script.php?src_id=817 -O candy.vim
cd /usr/share/vim/vim73/colors

wget https://raw.github.com/garybernhardt/dotfiles/master/.vim/colors/grb256.vim
wget http://www.vim.org/scripts/download_script.php?src_id=4128 -O guardian.vim
wget https://raw.github.com/Lokaltog/vim-distinguished/develop/colors/distinguished.vim
wget http://www.vim.org/scripts/download_script.php?src_id=12456 -O github.vim
wget https://raw.github.com/nanotech/jellybeans.vim/master/colors/jellybeans.vim
wget https://raw.github.com/ryanb/dotfiles/master/vim/colors/railscasts.vim
wget http://www.vim.org/scripts/download_script.php?src_id=10496 -O twilight.vim
wget https://raw.github.com/tpope/vim-vividchalk/master/colors/vividchalk.vim
wget http://www.vim.org/scripts/download_script.php?src_id=817 -O candy.vim

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
dos2unix *
dos2unix *
dos2unix *

To use in vim:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
:syntax on --- make sure you have color highlighting (make sure this is set in you /etc/vim/vimrc file so its default)
:colorscheme name ---- to set to colorscheme called "name" (there isnt one called name this is to show a point)
:colorscheme default ---- to set to colorscheme called "default"
:colorscheme CONTROL-D --- to see full list
:colorscheme TAB --- for tab completiong
:colorscheme d TAB --- for tab completion using d as start letter
:syntax on --- make sure you have color highlighting (make sure this is set in you /etc/vim/vimrc file so its default) :colorscheme name ---- to set to colorscheme called "name" (there isnt one called name this is to show a point) :colorscheme default ---- to set to colorscheme called "default" :colorscheme CONTROL-D --- to see full list :colorscheme TAB --- for tab completiong :colorscheme d TAB --- for tab completion using d as start letter
:syntax on --- make sure you have color highlighting (make sure this is set in you /etc/vim/vimrc file so its default)
:colorscheme name ---- to set to colorscheme called "name" (there isnt one called name this is to show a point)
:colorscheme default ---- to set to colorscheme called "default"
:colorscheme CONTROL-D --- to see full list
:colorscheme TAB --- for tab completiong
:colorscheme d TAB --- for tab completion using d as start letter

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 )

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/vim/vimrc | grep -v ^\" | grep .
runtime! debian.vim
syntax on
set incsearch " Incremental search
set autowrite " Automatically save before commands like :next and :make
set autoindent
set smartindent
set vb
set showmatch
set showmode
set ic
set ts=4
set sw=4
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
" MY MODIFICATIONS TO GET PYFLAKES TO LOOK GOOD (elflord) AND WORK (filetypes) AND INTELLISENSE TO WORK (omnifunc)
colorscheme elflord
filetype on
filetype plugin on
set omnifunc=syntaxcomplete#Complete
# cat /etc/vim/vimrc | grep -v ^\" | grep . runtime! debian.vim syntax on set incsearch " Incremental search set autowrite " Automatically save before commands like :next and :make set autoindent set smartindent set vb set showmatch set showmode set ic set ts=4 set sw=4 if filereadable("/etc/vim/vimrc.local") source /etc/vim/vimrc.local endif " MY MODIFICATIONS TO GET PYFLAKES TO LOOK GOOD (elflord) AND WORK (filetypes) AND INTELLISENSE TO WORK (omnifunc) colorscheme elflord filetype on filetype plugin on set omnifunc=syntaxcomplete#Complete
# cat /etc/vim/vimrc | grep -v ^\" | grep .
runtime! debian.vim
syntax on
set incsearch " Incremental search
set autowrite " Automatically save before commands like :next and :make
set autoindent
set smartindent
set vb
set showmatch
set showmode
set ic
set ts=4
set sw=4
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
" MY MODIFICATIONS TO GET PYFLAKES TO LOOK GOOD (elflord) AND WORK (filetypes) AND INTELLISENSE TO WORK (omnifunc)
colorscheme elflord
filetype on
filetype plugin on
set omnifunc=syntaxcomplete#Complete

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vim /etc/vim/vimrc
vim /etc/vim/vimrc
vim /etc/vim/vimrc

Add these

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
" your probably already have this next one, if you dont, put it in, its mandatory for plugins
filetype plugin on
" this next part you will not need (only if your omnifunctions are not set properly)
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
" your probably already have this next one, if you dont, put it in, its mandatory for plugins filetype plugin on " this next part you will not need (only if your omnifunctions are not set properly) autocmd FileType python set omnifunc=pythoncomplete#Complete autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS autocmd FileType html set omnifunc=htmlcomplete#CompleteTags autocmd FileType css set omnifunc=csscomplete#CompleteCSS
" your probably already have this next one, if you dont, put it in, its mandatory for plugins
filetype plugin on
" this next part you will not need (only if your omnifunctions are not set properly)
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS

 

##########################################
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vim /etc/vim/vimrc
vim /etc/vim/vimrc
vim /etc/vim/vimrc
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
inoremap <C-space> <C-x><C-o><span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>
inoremap <C-space> <C-x><C-o><span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>
 inoremap <C-space> <C-x><C-o><span style="background-color: #ffffff; font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5;"> </span>

 <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 *