.vimrc for Python

"My .vimrc file (Shan Jing)
" Part 1. Python Vim settings
" Part 2. Misc Settings
" Part 3. Global Format Settings

" <F1> opens bash in a horizonal split window
" <F2> opens bash in a vertical split window
" <F3> opens ipython in a horizonal split window
" <F4> opens ipython in a vertical split window
" <F5> debug/run current python file in a shell
" <c-,> opens a shell in a buffer/work with tabnew
" <c-w>t<c-w>H swap windows shapes H->V
" <c-w>t<c-w>K swap windows shapes V->H
" :map or :map! to show current mappings
" :nmap - Display normal mode maps
" :imap - Display insert mode maps
" :vmap - Display visual and select mode maps
" :smap - Display select mode maps
" :xmap - Display visual mode maps
" :cmap - Display command-line mode maps
" :omap - Display operator pending mode maps

"1. Python vim settings
set tabstop=4 " numbers of spaces of tab character
set shiftwidth=4 " numbers of spaces to (auto)indent
set smarttab
set expandtab " tabs are converted to spaces,use only when required
set autoindent
set smartindent

if has("autocmd")
" clear au, note: au = autocmd
" Restore cursor position
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

filetype plugin on
set ofu=syntaxcomplete#Complete
au FileType python set omnifunc=pythoncomplete#
au FileType javascript set omnifunc=javascriptcomplete#CompleteJS
au FileType html set omnifunc=htmlcomplete#CompleteTags
au FileType css set omnifunc=csscomplete#CompleteCSS
au FileType xml set omnifunc=xmlcomplete#CompleteTags
au FileType php set omnifunc=phpcomplete#CompletePHP
au FileType c set omnifunc=ccomplete#Complete

"nmap <buffer> <F5> :wmwG:r!python %<cr>`.
au BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
au BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
au BufRead *.py nmap <F5> :!python %<cr>
au BufRead *.py set nowrap
au BufRead *.py set go+=b
au BufRead *.py set tabstop=4
"Python settings: auto indent the next line after these key words:
au BufRead *.py set smartindent cinwords=def,if,elif,else,for,while,try,except,finall

" Automatically chmod +x Python, Perl and Shell scripts
au BufWritePost *.py !chmod +x %
au BufWritePost *.sh !chmod +x %
au BufWritePost *.pl !chmod +x %

"remove all training space when editing
"au BufWritePre *.py :%s/\s\+$//e

"3. Other global setttings
"3a. A Python Calculator
"3b. ConqueShell Mappings
"3c. vim 7+ tabs settings
"3d. color scheme settings
"3e. general coding settings for c,cpp,php,java, etc
"3f. global format settings

"A Python Calculator for python aware vim
"usage : command mode: Calc x*y
:command! -nargs=+ Calc :py print <args>
:py from math import *

" ConqueShell Mappings
nmap <F1> :ConqueTermSplit bash<cr>
nmap <F2> :ConqueTermVSplit bash<cr>
nmap <F3> :ConqueTermSplit ipython<cr>
nmap <F4> :ConqueTermVSplit ipython<cr>
nmap <F6> :ConqueTerm bash -o vi<cr>
let g:ConqueTerm_EscKey = '<c-k>'

"map <F9> :previous<cr> " map F9 to open previous buffer
"map <F10> :next<cr> " map F10 to open next buffer

" turn off highlighted search
map <silent> <c-h> :silent noh<cr>

"editting and sourcing .vimrc
map ,e :vsp ~/.vimrc<cr> " edit my .vimrc file
map ,u :source ~/.vimrc<cr> " update vim setting

"insert the directory name of the current buffer
imap <2> <c-r>=expand('%:p:h')<cr>

"For vim 7.0+ tabs limit (default is 10)
set tabpagemax=15
"open a new tab
map ,t :tabnew<cr>

"command mode mappings
"insert the directory name of the current buffer at command mode
"e.g., :!ls <2> will do ls dir at current cursor
cmap <F2> <c-r>=expand('%:p:h')<cr>

"color scheme setting
if has("gui_running")
" See ~/.gvimrc
set guifont=Monospace\ 10 " use this font
set lines=50 " height = 50 lines
set columns=100 " width = 100 columns
set background=light " adapt colors for background
set selectmode=mouse,key,cmd
set keymodel=
colorscheme elflord " use this color scheme
set background=dark " adapt colors for background

if has("autocmd")
" Filetypes (au = autocmd)
au FileType helpfile set nonumber " no line numbers when viewing help
au FileType helpfile nnoremap <buffer><cr> <c-]> " Enter selects subject
au FileType helpfile nnoremap <buffer><bs> <c-t> " Backspace to go back

" When using mutt, text width=72
au FileType mail,tex set textwidth=72
au FileType cpp,c,java,sh,pl,php,asp set autoindent
au FileType cpp,c,java,sh,pl,php,asp set smartindent
au FileType cpp,c,java,sh,pl,php,asp set cindent
"au BufRead mutt*[0-9] set tw=72

" File formats
au BufNewFile,BufRead *.pls set syntax=dosini
au BufNewFile,BufRead modprobe.conf set syntax=modconf
"remove all training space when editing
au BufWritePre *.py :%s/\s\+$//e

"Global format settings
set nocompatible " use vim defaults
set ls=2 " allways show status line
set scrolloff=3 " keep 3 lines when scrolling
set showcmd " display incomplete commands
set hlsearch " highlight searches
set incsearch " do incremental searching
set ruler " show the cursor position all the time
set visualbell t_vb= " turn off error beep/flash
set novisualbell " turn off visual bell
set nobackup " do not keep a backup file
set number " show line numbers
set ignorecase " ignore case when searching
"set noignorecase " don't ignore case
set title " show title in console title bar
set ttyfast " smoother changes
"set ttyscroll=0 " turn off scrolling, didn't work well with PuTTY
set modeline " last lines in document sets vim mode
set modelines=3 " number lines checked for modelines
set shortmess=atI " Abbreviate messages
set nostartofline " don't jump to first character when paging
set whichwrap=b,s,h,l,<,>,[,] " move freely between files

syntax on