Python seems to keep growing on me. By no means am I a Python master, nor am I a vim guru but I do prefer to use vim over a gui editor. Sure there are some niceties with using a gui but getting comfortable using a tool that is almost always at your disposal has something to be said for it. Adding a few things to your vim environment will make writing python much more pleasurable.
First there is the python indent plugin. At the time of this writing the current version is 0.3. There is one annoying thing about it. When commenting with hashes it un-indents. Luckily
should be changed to.
Here is the full updated version of the script. Vim Python Indent 0.4_alpha1
Download http://www.vim.org/scripts/script.php?script_id=790 at the time of this writing the current version is 2.6.3. and place it in your .vim /syntax/ directory (if its not there you should create it). This will add awesome highlighting to your python files. But we can make it better! We can add some simple error checking for things like missing colons. These are the additions that I have added which I found provided by sontek aka John M. Anderson. Don’t forget to add
syntax on to your .vimrc.
syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display syn match pythonError "^\s*for\s.*[^:]$" display syn match pythonError "^\s*except\s*$" display syn match pythonError "^\s*finally\s*$" display syn match pythonError "^\s*try\s*$" display syn match pythonError "^\s*else\s*$" display syn match pythonError "^\s*else\s*[^:].*" display syn match pythonError "^\s*if\s.*[^\:]$" display syn match pythonError "^\s*except\s.*[^\:]$" display syn match pythonError "[;]$" display syn keyword pythonError do
We can take that a step further with the following additions to your .vimrc.
" Syntax checking entire file " Usage: :make (check file) " :clist (view list of errors) " :cn, :cp (move around list of errors) autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\" autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
This will allow you to check the syntax ofyour entire file by typing :make. you can the get a list of errors with :clist and move between the errors with :cn and :cp.
Execute block of code
Sometimes you want to check or execute a block of code. Drop the python execute block vim script into your .vim/ftplugin/ directory. With it you can visually select a block of code and execute it with Ctrl+h.
So you are finding errors in your code and want to trace them? Drop this python ipdb vim script into your .vim/ftplugin/directory. You can set break points with F7 and clear them with shift-F7. If your not familiar with ipdb you should look it up.
While we’re at it lets add an easy way to run your current script.
Download the bexec plugin open it with vim using the full path to your vimball and then type :so % to source and install the vimball. Now setup some maps for it.
nmap <silent> <unique> <F8> :call Bexec()<cr> vmap <silent> <unique> <F8> :call BexecVisual()<cr> map <silent> <unique <F9> :call BexecCloseOut()<cr>
in your .vimrc will cause F8 to run the file you currently have open.
Who doesn’t love tabs? You can have them in vim too ya know! Add these lines to your .vimrc
map \t :tabnew<cr> map \n :tabnext<cr> map \p :tabprevious<cr> map \c :tabclose<cr>
With these additions you can open a new tab with \t move right with \n and move left with \p. To close the tab simply enter \c.
Edit File Menu
You can add a handy menu when opening files with :e. Its called the wildmenu. Add the following lines to your .vimrc. As a side note it will also give completion options for vim commands.
set wildmenu set wildignore+=*.pyc,*.zip,*.gz,*.bz,*.tar,*.jpg,*.png,*.gif,*.avi,*.wmv,*.ogg,*.mp3,*.mov
The vim snippy plugin provides textmate like code snippets. Its pretty darn cool. Installing is hard. Download the latest version from here. Be sure to download both snippy_plugin.vba and snippy_bundles.vba as the plugin and snippets are now seperated. Create a directory .vim/after/ftplugin and put both .vba files in there. I installed them by opening them with vim using the full path to the .vba files and then typing :source % to source the file. The vim ball script will install and you will be ready to go. Open a .py file and type def
Extra functional goodness like block comment
Operating on blocks of code is handy. Being able to comment out an entire section with a few keystrokes saves much time. This vim script provides that functionality. I download it and save it to .vim/ftplugin/python_fn.vim. Then you can visually select a block of code with ]v and comment it out with ]c and move its indentation level with ]< and ]> as well as a few other things.
Adding the following to .vim/ftplugin/python_pep8.vim will help you with tab space and other such worries with pep8.
setlocal tabstop=4 setlocal softtabstop=4 setlocal shiftwidth=4 setlocal textwidth=79 setlocal smarttab setlocal expandtab setlocal smartindent
One last note
Make sure indent and plugins are turned on in your .vimrc
filetype plugin indent on