Created
October 10, 2014 15:34
-
-
Save natekandler/3c8e0c48e71182b9a5b1 to your computer and use it in GitHub Desktop.
vimrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Sampled from Gary Bernhardt's .vimrc file | |
" vim:set ts=2 sts=2 sw=2 expandtab: | |
execute pathogen#infect() | |
syntax on | |
filetype plugin indent on | |
set shortmess=a | |
set cmdheight=2 | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" BASIC EDITING CONFIGURATION | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
set nocompatible | |
" allow unsaved background buffers and remember marks/undo for them | |
set hidden | |
" remember more commands and search history | |
set nu | |
set history=10000 | |
set expandtab | |
set tabstop=2 | |
set shiftwidth=2 | |
set softtabstop=2 | |
set autoindent | |
set laststatus=2 | |
set showmatch | |
set incsearch | |
set hlsearch | |
" make searches case-sensitive only if they contain upper-case characters | |
set ignorecase smartcase | |
" highlight current line | |
set cursorline | |
set cmdheight=2 | |
set switchbuf=useopen | |
set numberwidth=5 | |
set showtabline=2 | |
set winwidth=79 | |
set shell=bash | |
" Prevent Vim from clobbering the scrollback buffer. See | |
" http://www.shallowsky.com/linux/noaltscreen.html | |
set t_ti= t_te= | |
" keep more context when scrolling off the end of a buffer | |
set scrolloff=3 | |
" Store temporary files in a central spot | |
set backup | |
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp | |
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp | |
" allow backspacing over everything in insert mode | |
set backspace=indent,eol,start | |
" display incomplete commands | |
set showcmd | |
" Enable highlighting for syntax | |
syntax on | |
" 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 | |
" use emacs-style tab completion when selecting files, etc | |
set wildmode=longest,list | |
" make tab completion for files/buffers act like bash | |
set wildmenu | |
let mapleader="," | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" CUSTOM AUTOCMDS | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
augroup vimrcEx | |
" Clear all autocmds in the group | |
autocmd! | |
autocmd FileType text setlocal textwidth=78 | |
" Jump to last cursor position unless it's invalid or in an event handler | |
autocmd BufReadPost * | |
\ if line("'\"") > 0 && line("'\"") <= line("$") | | |
\ exe "normal g`\"" | | |
\ endif | |
"for ruby, autoindent with two spaces, always expand tabs | |
autocmd FileType ruby,haml,eruby,yaml,html,javascript,sass,cucumber set ai sw=2 sts=2 et | |
autocmd FileType python set sw=4 sts=4 et | |
autocmd! BufRead,BufNewFile *.sass setfiletype sass | |
autocmd BufRead *.mkd set ai formatoptions=tcroqn2 comments=n:> | |
autocmd BufRead *.markdown set ai formatoptions=tcroqn2 comments=n:> | |
" Indent p tags | |
autocmd FileType html,eruby if g:html_indent_tags !~ '\\|p\>' | let g:html_indent_tags .= '\|p\|li\|dt\|dd' | endif | |
" Don't syntax highlight markdown because it's often wrong | |
autocmd! FileType mkd setlocal syn=off | |
augroup END | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" COLOR | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
syntax enable | |
let g:solarized_termcolors=256 | |
"set t_Co=256 | |
"set background=dark | |
colorscheme solarized | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" STATUS LINE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
:set statusline=%<%f\ (%{&ft})\ %-4(%m%)%=%-19(%3l,%02c%03V%) | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" MISC KEY MAPS | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
map <leader>y "*y | |
" Move around splits with <c-hjkl> | |
nnoremap <c-j> <c-w>j | |
nnoremap <c-k> <c-w>k | |
nnoremap <c-h> <c-w>h | |
nnoremap <c-l> <c-w>l | |
" Insert a hash rocket with <c-l> | |
imap <c-l> <space>=><space> | |
" Can't be bothered to understand ESC vs <c-c> in insert mode | |
imap <c-c> <esc> | |
" Clear the search buffer when hitting return | |
:nnoremap <CR> :nohlsearch<cr> | |
nnoremap <leader><leader> <c-^> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" ARROW KEYS ARE UNACCEPTABLE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
map <Left> :echo "no!"<cr> | |
map <Right> :echo "no!"<cr> | |
map <Up> :echo "no!"<cr> | |
map <Down> :echo "no!"<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" MULTIPURPOSE TAB KEY | |
" Indent if we're at the beginning of a line. Else, do completion. | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
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> | |
inoremap <s-tab> <c-n> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" OPEN FILES IN DIRECTORY OF CURRENT FILE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
cnoremap %% <C-R>=expand('%:h').'/'<cr> | |
map <leader>e :edit %% | |
map <leader>v :view %% | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" RENAME CURRENT FILE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! RenameFile() | |
let old_name = expand('%') | |
let new_name = input('New file name: ', expand('%')) | |
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> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" PROMOTE VARIABLE TO RSPEC LET | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! PromoteToLet() | |
:normal! dd | |
" :exec '?^\s*it\>' | |
:normal! P | |
:.s/\(\w\+\) = \(.*\)$/let(:\1) { \2 }/ | |
:normal == | |
endfunction | |
:command! PromoteToLet :call PromoteToLet() | |
:map <leader>p :PromoteToLet<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" EXTRACT VARIABLE (SKETCHY) | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! ExtractVariable() | |
let name = input("Variable name: ") | |
if name == '' | |
return | |
endif | |
" Enter visual mode (not sure why this is needed since we're already in | |
" visual mode anyway) | |
normal! gv | |
" Replace selected text with the variable name | |
exec "normal c" . name | |
" Define the variable on the line above | |
exec "normal! O" . name . " = " | |
" Paste the original selected text to be the variable value | |
normal! $p | |
endfunction | |
vnoremap <leader>rv :call ExtractVariable()<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" INLINE VARIABLE (SKETCHY) | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! InlineVariable() | |
" Copy the variable under the cursor into the 'a' register | |
:let l:tmp_a = @a | |
:normal "ayiw | |
" Delete variable and equals sign | |
:normal 2daW | |
" Delete the expression into the 'b' register | |
:let l:tmp_b = @b | |
:normal "bd$ | |
" Delete the remnants of the line | |
:normal dd | |
" Go to the end of the previous line so we can start our search for the | |
" usage of the variable to replace. Doing '0' instead of 'k$' doesn't | |
" work; I'm not sure why. | |
normal k$ | |
" Find the next occurence of the variable | |
exec '/\<' . @a . '\>' | |
" Replace that occurence with the text we yanked | |
exec ':.s/\<' . @a . '\>/' . @b | |
:let @a = l:tmp_a | |
:let @b = l:tmp_b | |
endfunction | |
nnoremap <leader>ri :call InlineVariable()<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" MAPS TO JUMP TO SPECIFIC COMMAND-T TARGETS AND FILES | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
map <leader>gr :topleft :split config/routes.rb<cr> | |
function! ShowRoutes() | |
" Requires 'scratch' plugin | |
:topleft 100 :split __Routes__ | |
" Make sure Vim doesn't write __Routes__ as a file | |
:set buftype=nofile | |
" Delete everything | |
:normal 1GdG | |
" Put routes output in buffer | |
:0r! rake -s routes | |
" Size window to number of lines (1 plus rake output length) | |
:exec ":normal " . line("$") . _ " | |
" Move cursor to bottom | |
:normal 1GG | |
" Delete empty trailing line | |
:normal dd | |
endfunction | |
map <leader>gR :call ShowRoutes()<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> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" SWITCH BETWEEN TEST AND PRODUCTION CODE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! OpenTestAlternate() | |
let new_file = AlternateForCurrentFile() | |
exec ':e ' . new_file | |
endfunction | |
function! AlternateForCurrentFile() | |
let current_file = expand("%") | |
let new_file = current_file | |
let in_spec = match(current_file, '^spec/') != -1 | |
let going_to_spec = !in_spec | |
let in_app = match(current_file, '\<controllers\>') != -1 || match(current_file, '\<models\>') != -1 || match(current_file, '\<views\>') != -1 | |
if going_to_spec | |
if in_app | |
let new_file = substitute(new_file, '^app/', '', '') | |
end | |
let new_file = substitute(new_file, '\.rb$', '_spec.rb', '') | |
let new_file = 'spec/' . new_file | |
else | |
let new_file = substitute(new_file, '_spec\.rb$', '.rb', '') | |
let new_file = substitute(new_file, '^spec/', '', '') | |
if in_app | |
let new_file = 'app/' . new_file | |
end | |
endif | |
return new_file | |
endfunction | |
nnoremap <leader>. :call OpenTestAlternate()<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" RUNNING TESTS | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! RunTests(filename) | |
" Write the file and run tests for the given filename | |
:w | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo | |
if match(a:filename, '\.feature$') != -1 | |
exec ":!script/features " . a:filename | |
else | |
if filereadable("script/test") | |
exec ":!script/test " . a:filename | |
elseif filereadable("config/spring.rb") | |
exec ":!bundle exec spring rspec --color " . a:filename | |
elseif filereadable("Gemfile") | |
exec ":!bundle exec rspec --color " . a:filename | |
else | |
exec ":!rspec --color " . a:filename | |
end | |
end | |
endfunction | |
function! SetTestFile() | |
" Set the spec file that tests will be run for. | |
let t:grb_test_file=@% | |
endfunction | |
function! RunTestFile(...) | |
if a:0 | |
let command_suffix = a:1 | |
else | |
let command_suffix = "" | |
endif | |
" Run the tests for the previously-marked file. | |
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\)$') != -1 | |
if in_test_file | |
call SetTestFile() | |
elseif !exists("t:grb_test_file") | |
return | |
end | |
call RunTests(t:grb_test_file . command_suffix) | |
endfunction | |
function! RunNearestTest() | |
let spec_line_number = line('.') | |
call RunTestFile(":" . spec_line_number . " -b") | |
endfunction | |
map <leader>t :call RunTestFile()<cr> | |
map <leader>T :call RunNearestTest()<cr> | |
map <leader>a :call RunTests('')<cr> | |
map <leader>c :w\|:!script/features<cr> | |
map <leader>w :w\|:!script/features --profile wip<cr> | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" Md5 COMMAND | |
" Show the MD5 of the current buffer | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
command! -range Md5 :echo system('echo '.shellescape(join(getline(<line1>, <line2>), '\n')) . '| md5') | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" OpenChangedFiles COMMAND | |
" Open a split for each dirty file in git | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! OpenChangedFiles() | |
only " Close all windows, unless they're modified | |
let status = system('git status -s | grep "^ \?\(M\|A\)" | cut -d " " -f 3') | |
let filenames = split(status, "\n") | |
exec "edit " . filenames[0] | |
for filename in filenames[1:] | |
exec "sp " . filename | |
endfor | |
endfunction | |
command! OpenChangedFiles :call OpenChangedFiles() | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" InsertTime COMMAND | |
" Insert the current time | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
command! InsertTime :normal a<c-r>=strftime('%F %H:%M:%S.0 %z')<cr> | |
" Ryan Fischer -custom entries | |
map <leader>r :NERDTreeToggle<CR> | |
set wildignore=vendor/assets/stylesheets/metronic/*,vendor/assets/images/metronic/*,tmp/* | |
let g:indent_guides_start_level = 2 | |
let g:indent_guides_guide_size = 1 | |
"Nate Kandler entries | |
au BufRead,BufNewFile *.hamlc set ft=haml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment