Skip to content

Instantly share code, notes, and snippets.

@benley
Created July 9, 2013 00:24
Show Gist options
  • Save benley/5953657 to your computer and use it in GitHub Desktop.
Save benley/5953657 to your computer and use it in GitHub Desktop.
python vim niceties
" You most likely want these set in your vimrc:
filetype plugin indent on
" These three are used by the indent function below:
let s:maxoff = 50 " maximum number of lines to look backwards.
let pyindent_nested_paren="&sw*2"
let pyindent_open_paren="&sw*2"
" See http://google-styleguide.googlecode.com/svn/trunk/pyguide.html,
" which is very close to pep-8 in terms of indentation with a few additions.
" Google style defaults to two-space indents, but you can set shiftwidth=4
" as you prefer. This function doesn't mind either way.
function GetGooglePythonIndent(lnum)
" Indent inside parens.
" Align with the open paren unless it is at the end of the line.
" E.g.
" open_paren_not_at_EOL(foo, bar, bas,
" (200, 300,
" 400),
" buz)
" open_paren_at_EOL(
" 100, 200, 300, 400)
call cursor(a:lnum, 1)
let [par_line, par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
\ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
\ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
\ . " =~ '\\(Comment\\|String\\)$'")
if par_line > 0
call cursor(par_line, 1)
if par_col != col("$") - 1
return par_col
endif
endif
" Delegate the rest to the original function.
return GetPythonIndent(a:lnum)
endfunction
" Allow for highlighting columns beyond some threshold:
" (see the match OverLength statement below)
autocmd ColorScheme * highlight OverLength ctermfg=red guifg=red
" Angrily highlight trailing whitespace:
" (http://vim.wikia.com/wiki/Highlight_unwanted_spaces)
autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
autocmd ColorScheme * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd FileType python
\ setlocal indentexpr=GetGooglePythonIndent(v:lnum)
\ | setlocal omnifunc=pythoncomplete#Complete
\ | setlocal shiftwidth=4 expandtab
\ | match OverLength /\%>80v.\+/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment