5/16/2013 - 3:05 AM

Vim Configuration Redux

Vim Configuration Redux

" This minimal vimrc is intended to be easily deployed to machines where my
" dotfiles are not already installed, while maintaining a basic feature set
" for usability.

" These directories must exist... @{
set backupdir=$HOME/.vim/backup " backup files location
set directory=$HOME/.vim/swap " swap files location
set tags=./tags,$HOME/.vim/tags " you probably want to add more to these later.
" }@

colorscheme desert 

" Performance @{
set nocompatible

" Swap timer: milliseconds and bytes to wait idle before writing swap
set updatetime=30000 updatecount=100

" Buffer handling
set switchbuf=useopen           "swb:   Jumps to first window or tab that contains specified buffer instead of duplicating an open window
set hidden                      "hid:   allows opening a new buffer in place of an existing one without first saving the existing one

" }@ end Performance

" Misc options & key mappings @{
" NOTE: lots of other key mappings are positioned in this file in their
" relevant groups.

" Use this to override keymapping elsewhere if needed
" let mapleader=","

" File buffer navigation
nmap <Leader>n :next<CR>
nmap <Leader>p :prev<CR>

" Easy buffer selection
nmap <Leader>b :ls<CR>:b<Space>

" toggle readonly
nmap <Leader>ro :set invreadonly<CR>

noremap <Leader>tex :tabe %:h<CR>

" Spelling options (and a shortcut to disable it)
" set spell
set spelllang=en
nmap <Leader>Spell :set spell!<CR>

" toggle synchronous scrolling of windows
nmap <Leader>scrollbind :set scrollbind!<CR>

" toggles paste mode
nmap <Leader>Paste :set paste!<CR>

" toggles auto-changedir in Ex mode?
nmap <Leader>CD :set invacd<CR>

" }@

" Line wrapping & non-printing chars @{

" toggle non-printing characters
nmap <Leader>List :set list!<CR>

set nowrap " no wrapping of lines
nmap <Leader>Wrap :set nowrap!<CR>

" Display non-wrapping line-continues 
set listchars+=precedes:<,extends:<

" }@

" Window features (statusline, splits, etc) @{

set ruler
set rulerformat=%30(%Y\ %B\ %=\ %l,%c%V\ %P%)

" put new windows below and right by default
set splitbelow splitright

" Highlight the current line & column position
" set cursorline cursorcolumn

set number " enable line numbering, and a toggle shortcut
nmap <silent> <Leader>Number :set number!<CR>
nmap <silent> <Leader>n :set number!<CR>

" }@

" Autoindent @{

" toggle auto-indent
set autoindent smartindent
set tabstop=2 shiftwidth=2 expandtab " spaces for tabs, indentation, and avoid real tabs

nmap <Leader>Indent :set autoindent!<CR>
nmap <Leader>Tab :set expandtab!<CR>

" }@

" Search features @{

" For tag navigation... (e.g. in help files)
" map <silent><C-Left> <C-T>
" map <silent><C-Right> <C-]>

" Search settings
set incsearch " incremental search - actively find matches while typing
set tagbsearch " use binary search for tags (:ta) - performance improvement

" Show matching parens, brackets, etc (for 5 seconds)
set showmatch matchtime=5

" }@

" GUI and Mouse Options @{

if has("gui_running") " See :help guioptions
  set guioptions-=T " Disable toolbar in GUI mode 
  set guioptions+=aA " Enable autoselect mode; integrate VIMs buffers with OS paste system, etc

if has('transparency')
  set transparency=15

" Mouse support always-on in macvim
set mouse=a
set mousemodel=popup_setpos

" }@

" Code folding configuration @{
" reference:
set foldenable
nmap <Leader>F set foldenable!<CR>

set foldcolumn=4
set foldmethod=syntax

" In normal mode, Spacebar toggles a fold, zi toggles all folding, zM closes all folds
nnoremap  <silent>  <space> :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<cr>

" Automatic save of folds, so that you dont have to type everytime
" :mkview to save and :loadview to restore folds
autocmd BufWinLeave * silent! mkview
autocmd BufWinEnter * silent! loadview
" }@

" FileType & autocomplete handling @{

" Enable per-file formatting and the like
set modeline

filetype plugin indent on

if has('syntax')
  syntax on

" Setup color/style for completion menu @{
hi Pmenu cterm=none ctermfg=black ctermbg=DarkGreen
hi PmenuSel cterm=none ctermfg=Green ctermbg=black
hi PmenuSbar cterm=bold ctermfg=none ctermbg=Green
hi PmenuThumb cterm=bold ctermfg=Blue ctermbg=black
" }@

" set wildmenu
" set wildignore+=*.o,*.obj,*.~,.lo,.so,.pyc,.swp,.bak
" set,.a,.bak,.swp,.pyc

set completeopt=menuone,longest
" set completeopt=menuone,longest

set pumheight=12

" enable autocomplete
set omnifunc=syntaxcomplete#Complete

""" Default autocomplete methods
" C-X C-L   match similar lines
" C-X C-F   match file names
" C-X C-K   match from dictionary
" C-X C-S   match spelling suggestions
" C-X C-T   match from thesaurus
" C-X C-O   match via Omnicomplete
" C-X C-]   match via tag database
" C-X C-U   match based on user 'completefunc' if defined (or syntax file)
" C-X C-I   match via keywords in include files (see also 'include' and 'path')
" C-X C-N   match keywords in current file
" C-X C-D   match macros & definitions

let g:completion_select_first=0 " config
let g:completion_all_keystrokes=0 " config
let g:completion_active=0 " state

" What keys to use in full automcompletion
let s:keyMapping = split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$&>', '.\zs')

function! s:getTokenClassName(pos)
  return synIDattr(synID(line('.'), a:pos - 1, 1), 'name')

" Completion inspired by SuperTab
function! SuperCompleter(quicker_mode, minchars, basechar)

  let quickmode = (a:quicker_mode == 1)
  let trailer = g:completion_select_first == 1 ? "\<C-N>" : ''
  let currentline = getline('.') " the line content
  let currentcol = col('.') " position within the line
  let line_precede = strpart(currentline, 0, currentcol +1)
  let word_precede = matchstr(line_precede, "[^ \t]*$") 

  if(strlen(word_precede) > a:minchars)
    if(max([ stridx(word_precede, '/'), stridx(word_precede, '\\') ]) >= 0)
      return "\<C-X>\<C-F>" . trailer " Try completion as filename
      return "\<C-X>\<C-I>" . trailer " Try completion as keyword from current & included files (i.e. #include)
    elseif(exists('&omnifunc') && &omnifunc != '')
      return "\<C-X>\<C-O>" . trailer " Try completion via Omnifunc
    elseif(exists('&completefunc') && &completefunc != '')
      return "\<C-X>\<C-U>" . trailer " Try completion via user-mode completion
      return "\<C-X>\<C-D>" . trailer " Try keyword completion from this file
  return a:basechar

" Use the menu for *everything*
function! s:AllKeystrokesComplete()
  for key in s:keyMapping
    execute printf('inoremap <silent> %s %s<C-r>=SuperCompleter(0,3,"")<CR><C-N><C-P>', key, key)

function! s:RevokeAllKeystrokes()
  for key in s:keyMapping
    execute printf('iunmap %s', key)

function! SetupCompletionKeys()
  let g:completion_keys_active=1
  " Tab key (with Shift) will step through menu options
  inoremap <expr> <Tab> pumvisible() ? "\<C-N>" : "\<Tab>"
  inoremap <expr> <S-Tab> pumvisible() ? "\<C-P>" : "\<S-Tab>"

  " Enter will use the selected item if one is selected in the menu, when shown,
  " and behave normally (insert new line) when the menu is not shown.
  inoremap <silent> <expr> <CR> pumvisible() ? "\<C-Y>" : "<CR>"

  " Leader-Tab will activate auto-complete in regular mode (file or
  " omni-complete options)
  inoremap <expr> <Leader><Tab> SuperCompleter(0,1,'')

  " Leader-Shift-Tab will activate Definition/Macro completion
  inoremap <expr> <Leader><S-Tab> SuperCompleter(1,1,'')

    call s:AllKeystrokesComplete()


function! RevokeCompletionKeys()
  let g:completion_keys_active=0
  iunmap <Tab>
  iunmap <S-Tab>
  iunmap <CR>
  iunmap <Leader><Tab>
  iunmap <Leader><S-Tab>
    call s:RevokeAllKeystrokes()

call SetupCompletionKeys()

" Shortcut to toggle completion keys (Leader-C in normal mode)
nnoremap <expr> <Leader>C  (":call " . (g:completion_active == 1 ? "RevokeCompletionKeys" : "SetupCompletionKeys") . "()<CR>")

set dictionary+=/usr/share/dict/words

" For BASH and similar shell languages:
" :help bash
" let g:is_sh = 1
" let g:is_bash = 1
" let g:is_posix = 1
" let g:is_kornshell = 1
" let g:sh_fold_enabled = {0:none,1:function,2:heredoc,4:if/do/for} (or a sum of them)
let g:sh_fold_enabled=8
let g:sh_minlines=500
let g:sh_maxlines=1000

" FileType-specific settings
autocmd filetype python setlocal noet foldenable foldmethod=indent
autocmd filetype javascript setlocal foldenable foldmethod=indent
autocmd filetype html setlocal foldenable foldmethod=indent

autocmd FileType sql set omnifunc=sqlcomplete#Complete
autocmd FileType ruby set omnifunc=rubycomplete#Complete
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
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

" let the enter key take me to navigate help files
autocmd FileType help nmap <buffer> <CR> <C-]>
autocmd FileType help set foldcolumn=0 nonumber "no foldcolumn for help files

" Makefiles should permit tabs.
autocmd FileType make setlocal noexpandtab

" Configuration files should permit hex colors
autocmd FileType conf
    \  syn match confColor  "#\x\{2,6}" transparent |
    \  hi def link confColor  String

" We want consistent spacing in Python, and not tabs.
autocmd FileType python setlocal ai sw=4 ts=4 sta et

" And an easy way to check syntax...
autocmd FileType python set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd FileType python set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m

" }@

" Automation routines @{

" Shortcut to print PDF with highlights/colors
command! -nargs=* Hardcopy call DoMyPrint('<args>')
function! DoMyPrint(args)
  let colorsave=g:colors_name
  color print
  exec 'hardcopy '.a:args
  exec 'color '.colorsave

" }@

" .vimrc modeline
" vim: set ft=vim foldcolumn=3 foldenable foldmethod=marker foldmarker=@{,}@