Skip to content

Instantly share code, notes, and snippets.

@ys
Created August 1, 2013 14:25
Show Gist options
  • Save ys/6131850 to your computer and use it in GitHub Desktop.
Save ys/6131850 to your computer and use it in GitHub Desktop.
" Begin .vimrc
"
set nocompatible
"
" ================
" Ruby stuff
" ================
if filereadable(expand("~/.vimrc.bundles"))
source ~/.vimrc.bundles
endif
syntax on " Enable syntax highlighting
filetype plugin indent on " Enable filetype-specific indenting and plugins
augroup myfiletypes
" Clear old autocmds in group
autocmd!
" autoindent with two spaces, always expand tabs
autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
"octopress
autocmd BufNewFile,BufRead *.markdown,*.textile set filetype=octopress
augroup END
let mapleader = ","
let g:mapleader = ","
nmap <Leader>bi :source ~/.vimrc<cr>:BundleInstall<cr>
map <Leader>co ggVG"*y
map <Leader>a :A<CR>
map <leader>gv :CommandTFlush<cr>\|:CommandT app/views<cr>
map <leader>gc :CommandTFlush<cr>\|:CommandT app/controllers<cr>
map <leader>gm :CommandTFlush<cr>\|:CommandT app/models<cr>
map <leader>gh :CommandTFlush<cr>\|:CommandT app/helpers<cr>
map <leader>gl :CommandTFlush<cr>\|:CommandT lib<cr>
map <leader>gp :CommandTFlush<cr>\|:CommandT public<cr>
map <leader>gs :CommandTFlush<cr>\|:CommandT public/stylesheets/sass<cr>
map <leader>gf :CommandTFlush<cr>\|:CommandT features<cr>
map <leader>gg :topleft 100 :split Gemfile<cr>
map <leader>gt :CommandTFlush<cr>\|:CommandTTag<cr>
map <leader>f :CommandTFlush<cr>\|:CommandT<cr>
map <leader>F :CommandTFlush<cr>\|:CommandT %%<cr>
""Quick vim regex to convert hashrocket (=>) 1.8 to colon syntax (:) 1.9:
nmap <leader>nh :%s/\v:(\w+) \=\>/\1:/g<cr>
map <Leader>o :call RunCurrentLineInTest()<CR>
map <Leader>p :set paste<CR>o<esc>"*]p:set nopaste<cr>
map <Leader>t :call RunCurrentTest()<CR>
map <Leader>tf :call RunForemanCurrentTest()<CR>
map <Leader>tl :call RunForemanCurrentLineInTest()<CR>
map <Leader>tt :call RunCurrentLineInTest()<CR>
map <Leader>w <C-w>w
map <Leader>x :exec getline(".")<cr>
nnoremap <leader><leader> <c-^>
" Edit another file in the same directory as the current file
" uses expression to extract path from current file's path
map <Leader>e :e <C-R>=expand("%:p:h") . '/'<CR>
map <Leader>s :split <C-R>=expand("%:p:h") . '/'<CR>
map <Leader>v :vnew <C-R>=expand("%:p:h") . '/'<CR>
map <C-h> :nohl<cr>
imap <C-l> :<Space>
map <C-s> <esc>:w<CR>
imap <C-s> <esc>:w<CR>
map <C-t> <esc>:tabnew<CR>
map <C-x> <C-w>c
map <C-n> :cn<CR>
map <C-p> :cp<CR>
nmap <C-)> <C-]>
inoremap jj <ESC>
" Get off my lawn
nmap <Left> <<
nmap <Right> >>
vmap <Left> <gv
vmap <Right> >gv
nmap <Up> [e
nmap <Down> ]e
vmap <Up> [egv
vmap <Down> ]egv
" Emacs-like beginning and end of line.
imap <c-e> <c-o>$
imap <c-a> <c-o>^
set backspace=indent,eol,start " allow backspacing over everything in insert mode
set history=500 " keep 500 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set autoindent
set showmatch
set nowrap
set backupdir=~/.vim/_backup/ " where to put backup files.
set directory=~/.vim/_temp/ " where to put swap files.
set autoread
set wmh=0
set viminfo+=!
set guioptions-=T
set guifont=Triskweline_10:h10
set et
set sw=2
set smarttab
set noincsearch
set ignorecase smartcase
set laststatus=2 " Always show status line.
set number
"set gdefault " assume the /g flag on :s substitutions to replace all matches in a line
set autoindent " always set autoindenting on
set shell=zsh
set shellcmdflag=-ci
set colorcolumn=80
set guifont=Bitstream\ Vera\ Sans\ Mono:h14
"let g:NERDTreeWinPos = "right"
set guioptions-=T " Removes top toolbar
set guioptions-=r " Removes right hand scroll bar
set go-=L " Removes left hand scroll bar
set showbreak=↪
set winwidth=84
" We have to have a winheight bigger than we want to set winminheight. But if
" we set winheight to be huge before winminheight, the winminheight set will
" fail.
set winheight=5
set winminheight=5
set winheight=999
set mouse=nicr
" Set the tag file search order
set tags=./tags;
" Use Ack instead of grep
set grepprg=ack
" Gist options
let g:gist_detect_filetype = 1
let g:gist_open_browser_after_post = 1
" Make the omnicomplete text readable
:highlight PmenuSel ctermfg=black
" Fuzzy finder: ignore stuff that can't be opened, and generated files
let g:fuzzy_ignore = "*.png;*.PNG;*.JPG;*.jpg;*.GIF;*.gif;vendor/**;coverage/**;tmp/**;rdoc/**"
" Ag instead of Ack
let g:ackprg = 'ag --nogroup --nocolor --column'
" Highlight the status line
highlight StatusLine ctermfg=blue ctermbg=yellow
set shiftround " When at 3 spaces and I hit >>, go to 4, not 5.
set nofoldenable " Say no to code folding...
command! Q q " Bind :Q to :q
command! Qall qall
" Disable Ex mode
map Q <Nop>
" Disable K looking stuff up
map K <Nop>
" When loading text files, wrap them and don't split up words.
au BufNewFile,BufRead *.txt setlocal wrap
au BufNewFile,BufRead *.txt setlocal lbr
" Checkthis tests
"
function! RunForemanCurrentTest()
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1
if in_test_file
call SetTestFile()
if match(expand('%'), '\.feature$') != -1
call SetTestRunner("!foreman run -e .env.test cucumber")
exec g:bjo_test_runner g:bjo_test_file
elseif match(expand('%'), '_spec\.rb$') != -1
call SetTestRunner("!foreman run -e .env.test rspec")
exec g:bjo_test_runner g:bjo_test_file
else
call SetTestRunner("!foreman run -e .env.test ruby -Itest")
exec g:bjo_test_runner g:bjo_test_file
endif
else
exec g:bjo_test_runner g:bjo_test_file
endif
endfunction
function! RunForemanCurrentLineInTest()
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1
if in_test_file
call SetTestFileWithLine()
end
exec "!foreman run -e .env.test rspec" g:bjo_test_file . ":" . g:bjo_test_file_line
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Test-running stuff
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RunCurrentTest()
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1
if in_test_file
call SetTestFile()
if match(expand('%'), '\.feature$') != -1
call SetTestRunner("!cucumber")
exec g:bjo_test_runner g:bjo_test_file
elseif match(expand('%'), '_spec\.rb$') != -1
call SetTestRunner("!rspec")
exec g:bjo_test_runner g:bjo_test_file
else
call SetTestRunner("!ruby -Itest")
exec g:bjo_test_runner g:bjo_test_file
endif
else
exec g:bjo_test_runner g:bjo_test_file
endif
endfunction
function! SetTestRunner(runner)
let g:bjo_test_runner=a:runner
endfunction
function! RunCurrentLineInTest()
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\|_test.rb\)$') != -1
if in_test_file
call SetTestFileWithLine()
end
exec "!rspec" g:bjo_test_file . ":" . g:bjo_test_file_line
endfunction
function! SetTestFile()
let g:bjo_test_file=@%
endfunction
function! SetTestFileWithLine()
let g:bjo_test_file=@%
let g:bjo_test_file_line=line(".")
endfunction
function! CorrectTestRunner()
if match(expand('%'), '\.feature$') != -1
return "cucumber"
elseif match(expand('%'), '_spec\.rb$') != -1
return "rspec"
else
return "ruby"
endif
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Tab completion
" will insert tab at beginning of line,
" will use completion if not at beginning
set wildmode=list:longest,list:full
set complete=.,w,t
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
inoremap <Tab> <c-r>=InsertTabWrapper()<cr>
" Let's be reasonable, shall we?
nmap k gk
nmap j gj
let g:CommandTMaxHeight=50
let g:CommandTMatchWindowAtTop=1
" Don't wait so long for the next keypress (particularly in ambigious Leader
" situations.
set timeoutlen=500
" Remove trailing whitespace on save for ruby files.
"au BufWritePre *.rb :%s/\s\+$//e
function! OpenFactoryFile()
if filereadable("test/factories.rb")
execute ":sp test/factories.rb"
else
execute ":sp spec/factories.rb"
end
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RENAME CURRENT FILE (thanks Gary Bernhardt)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RenameFile()
let old_name = expand('%')
let new_name = input('New file name: ', expand('%'), 'file')
if new_name != '' && new_name != old_name
exec ':saveas ' . new_name
exec ':silent !rm ' . old_name
redraw!
endif
endfunction
map <leader>n :call RenameFile()<cr>
" ========================================================================
" End of things set by me.
" ========================================================================
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" autocmd BufReadPost *
" \ if line("'\"") > 0 && line("'\"") <= line("$") |
" \ exe "normal g`\"" |
" \ endif
" augroup END
endif " has("autocmd")
autocmd User Rails let b:surround_{char2nr('-')} = "<% \r %>" " displays <% %> correctly
:set cpoptions+=$ " puts a $ marker for the end of words/lines in cw/c$ commands
function! <SID>StripTrailingWhitespaces()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
%s/\s\+$//e
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
"show trailing spaces
:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$/
set background=dark
color solarized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment