1. tednaleid
  2. vimrc

Source

vimrc / .vimrc

filetype off
call pathogen#runtime_append_all_bundles()
filetype plugin indent on

set nocompatible

set modelines=0

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

set encoding=utf-8
set scrolloff=3
set autoindent
set showmode
set showcmd
set hidden
set wildmenu
set wildmode=list:longest
set visualbell
set cursorline
set ttyfast
set ruler
set backspace=indent,eol,start
set laststatus=2
set number " change to relativenumber if you want relative to your position
set undofile


let mapleader = " "

nnoremap / /\v
vnoremap / /\v
set ignorecase
set smartcase
set gdefault
set incsearch
set showmatch
set hlsearch
"nnoremap <leader><space> :noh<cr>
nnoremap <tab> %
vnoremap <tab> %

set wrap
"set textwidth=79
set formatoptions=qrn1
set colorcolumn=85

set list
set listchars=tab:▸\ ,eol
nnoremap <up> <nop>
nnoremap <down> <nop>
nnoremap <left> <nop>
nnoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
nnoremap j gj
nnoremap k gk

inoremap <F1> <ESC>
nnoremap <F1> <ESC>
vnoremap <F1> <ESC>

inoremap jj <ESC>
inoremap jk <ESC>

" Examples:  
" din( - "Delete in next ()" 
" vin( - "Select in next ()" 
" cin( - "Change in next () (great for function calls)
vnoremap <silent> in( :<C-U>normal! f(vi(<cr> 
onoremap <silent> in( :<C-U>normal! f(vi(<cr>

"au FocusLost * :wa


" todo helpers
" move current line up/down
nnoremap <D-J> ddp 
nnoremap <D-K> ddkP
" mark current line with a '-' for not done, or a 'X' for done 
nnoremap <D-H> :s/^\([-X] \)\=/- /<cr>:noh<cr>
nnoremap <D-L> :s/^\([-X] \)\=/X /<cr>:noh<cr>

" markdown helpers
nnoremap <leader>1 yypVr=
nnoremap <leader>2 yypVr-
nnoremap <leader>3 I# <Esc>A #<Esc>
nnoremap <leader>4 I## <Esc>A ##<Esc>
nnoremap <leader>5 I### <Esc>A ###<Esc>
nnoremap <leader>6 I#### <Esc>A ####<Esc>

" working with split windows
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" open vertical window and switch to it
nnoremap <leader>v <C-w>v<C-w>l
nnoremap <D-d> <C-w>v<C-w>l
nnoremap <leader>s <C-w>s<C-w>j
nnoremap <D-D> <C-w>s<C-w>j

" open vimrc in split window
nnoremap <leader>vv <C-w><C-v><C-l>:e ~/.vimrc<cr>
nnoremap <leader>vg <C-w><C-v><C-l>:e ~/.gvimrc<cr>
nnoremap <leader>vm <C-w><C-v><C-l>:e ~/Dropbox/docs/markdown_notes<cr>
nnoremap <leader>vz <C-w><C-v><C-l>:e ~/shared-zshrc<cr>


nnoremap <leader><F5> :source $MYVIMRC<cr>:echo 'sourced ' . $MYVIMRC<cr>

" cd to directory of current file
nnoremap <leader>cd :lcd %:p:h<cr>:pwd<cr>

nnoremap <leader>nt :NERDTree<cr>
nnoremap <F2> :NERDTreeToggle<CR>
nnoremap <leader><F2> :NERDTreeFind<CR>

" ack
nnoremap <leader>a :Ack 
nnoremap <leader>aa :Ack --all 
nnoremap <leader>ag :Ack --groovy 

" from Janus: https://github.com/carlhuda/janus
" in MacVim, need to unmap existing D-F in gvimrc
nnoremap <D-F> :Ack<space>


" tab expansion in python
au BufRead,BufNewFile *.py set expandtab

" gundo
nnoremap U :GundoToggle<cr>
let g:gundo_debug = 1
let g:gundo_preview_bottom = 1


" easy motion leader change
"let g:EasyMotion_leader_key = '<Leader>m'

" command t remap to cmd-T
nnoremap <D-t> :CommandT<cr>

" Folding
set foldlevelstart=0
nnoremap <C-Space> za
vnoremap <C-Space> za
noremap <leader>ft Vatzf

"Directories for swp files
set backupdir=~/.vim/backup
set directory=~/.vim/backup

" Up/Down in quick fix windows
nnoremap <D-j> :cnext<cr>
nnoremap <D-k> :cprevious<cr>


" from http://vim.wikia.com/wiki/Indent_text_object
" use (motion)ai to select "an indent" use (motion)ii to select "inner
" indent", good for python
onoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR>
onoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR>
vnoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR><Esc>gv
vnoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR><Esc>gv

function! IndTxtObj(inner)
  let curline = line(".")
  let lastline = line("$")
  let i = indent(line(".")) - &shiftwidth * (v:count1 - 1)
  let i = i < 0 ? 0 : i
  if getline(".") !~ "^\\s*$"
    let p = line(".") - 1
    let nextblank = getline(p) =~ "^\\s*$"
    while p > 0 && ((i == 0 && !nextblank) || (i > 0 && ((indent(p) >= i && !(nextblank && a:inner)) || (nextblank && !a:inner))))
      -
      let p = line(".") - 1
      let nextblank = getline(p) =~ "^\\s*$"
    endwhile
    normal! 0V
    call cursor(curline, 0)
    let p = line(".") + 1
    let nextblank = getline(p) =~ "^\\s*$"
    while p <= lastline && ((i == 0 && !nextblank) || (i > 0 && ((indent(p) >= i && !(nextblank && a:inner)) || (nextblank && !a:inner))))
      +
      let p = line(".") + 1
      let nextblank = getline(p) =~ "^\\s*$"
    endwhile
    normal! $
  endif
endfunction

" Open markdown in marked.app editor
nnoremap <leader>m :silent !open -a Marked.app %:p<cr>


" FuzzyFinder
let g:fuzzy_ignore = "*.pyc;log/**;.svn/**;.git/**;.hg/**;pip-log.txt;*.gif;*.jpg;*.jpeg;*.png;**media/admin/**;**media/ckeditor/**;**media/filebrowser/**;**media/pages/**;**src/**;**build/**;**_build/**;**media/cache/**"
let g:fuzzy_matching_limit = 70

let g:grails_test_app = 'grails test-app'

" auto cd to root of hg/git project for current buffer's file
function! s:set_working_directory_for_project()
  let wd = expand("%:p:h")
  silent exe "cd " . wd

  let git_root = s:git_root()
  if git_root != ""
    silent exe "cd " . git_root
    return
  endif

  let hg_root = s:mercurial_root()
  if hg_root != "" 
    silent exe "cd " . hg_root
    return
  endif

endfunction

function! s:mercurial_root()
  let mercurial_root = system('hg root')

  if v:shell_error != 0
    return ""
  endif
  return mercurial_root
endfunction

function! s:git_root()
  let git_root = system('git rev-parse --show-toplevel')
  if v:shell_error != 0
    return ""
  endif
  return git_root
endfunction

au BufEnter * call s:set_working_directory_for_project()

function! s:copy_groovy_buffer_to_temp(first, last)
  " groovy/java scripts can't start with a # and tempname's normally do
  let src = substitute(tempname(), "[^\/]*$", "vim_&.groovy", "") 
  
  " put current buffer's content in a temp file
  silent exe ": " . a:first . "," . a:last . "w " . src

  return src
endfunction

function! s:select_new_temp_buffer()
  let temp_file = tempname()

  " open the preview window to the temp file
  silent exe ":pedit! " . temp_file

  " select the temp buffer as active 
  wincmd P

  " set options for temp buffer
  setlocal buftype=nofile
  setlocal noswapfile
  setlocal syntax=none
  setlocal bufhidden=delete

  return temp_file
endfunction

function! Groovy_eval_vsplit() range
  let temp_source = s:copy_groovy_buffer_to_temp(a:firstline, a:lastline)
  let temp_file = s:select_new_temp_buffer()
  
  " replace current buffer with groovy's output
  silent execute ":%! groovy " . temp_source . " 2>&1 "

  wincmd p " change back to the source buffer
endfunction

au BufNewFile,BufRead *.groovy vmap <silent> <F7> :call Groovy_eval_vsplit()<CR>
au BufNewFile,BufRead *.groovy nmap <silent> <F7> mzggVG<F7>`z
au BufNewFile,BufRead *.groovy imap <silent> <F7> <Esc><F7>a
au BufNewFile,BufRead *.groovy map <silent> <S-F7> :wincmd P<CR>:q<CR>
au BufNewFile,BufRead *.groovy imap <silent> <S-F7> <Esc><S-F7>a


" set these up as environment variables on your system, or override
" per session by using ':let g:grails_user = foo'
let g:grails_user = $DEFAULT_GRAILS_USER
let g:grails_password = $DEFAULT_GRAILS_PASSWORD
let g:grails_base_url = $DEFAULT_GRAILS_BASE_URL

function! Grails_eval_vsplit() range
  let temp_source = s:copy_groovy_buffer_to_temp(a:firstline, a:lastline)
  let temp_file = s:select_new_temp_buffer()
  
  if strlen(g:grails_user) > 0
      " replace current buffer with grails' output
      silent execute ":%! postCode.groovy -u " . g:grails_user . " -p " . g:grails_password . " -b " . g:grails_base_url . " " . temp_source . " 2>&1 "
  else 
      silent execute ":%! postCode.groovy -b " . g:grails_base_url . " " . temp_source . " 2>&1 "
  endif

  wincmd p " change back to the source buffer
endfunction

au BufNewFile,BufRead *.groovy vmap <silent> <F8> :call Grails_eval_vsplit()<CR>
au BufNewFile,BufRead *.groovy nmap <silent> <F8> mzggVG<F8>`z
au BufNewFile,BufRead *.groovy imap <silent> <F8> <Esc><F8>a
au BufNewFile,BufRead *.groovy map <silent> <S-F8> :wincmd P<CR>:q<CR>
au BufNewFile,BufRead *.groovy imap <silent> <S-F8> <Esc><S-F8>a