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
: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.
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:
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
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:
# 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
cd ~ mkdir src cd src git clone http://github.com/kevinw/pyflakes-vim cd pyflakes-vim/ftplugin
See what files/dir are there:
find
You should just see these enteries:
. ./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):
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)
vim /etc/vimrc/vimrc
Add these lines to bottom:
" COMMENT START WITH DOUBLE QUOTES - ADDED BY ME FOR PLUGINS filetype on filetype plugin on
To use pyflakes in vim:
Edit a python file
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
: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
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)
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)
dos2unix *
To use in vim:
: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 )
# 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)
vim /etc/vim/vimrc
Add these
" 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
vim /etc/vim/vimrc
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!