Created
September 19, 2011 16:02
-
-
Save jeff-r/1226832 to your computer and use it in GitHub Desktop.
Vim bindings to fold Ruby class and spec files.
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
" Folding for Ruby | |
" | |
" ,z -- Show only last search | |
" ,zz -- Show only "describe ..." and "it ..." lines in specs | |
" ,zd -- Show only "class ..." and "def ..." lines in Ruby files | |
" zR -- Remove all folds | |
" | |
" From http://vim.wikia.com/wiki/Folding_with_Regular_Expression | |
nnoremap ,z :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2<CR> | |
" Then variations on that, with different searches ... | |
" | |
" Fold spec files, displaying "describe ..." and "it ..." lines | |
function! FoldSpec() | |
let @/='\(describe.*do$\|it.*do$\)' | |
setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2 | |
endfunction | |
map ,zz :call FoldSpec()<CR> | |
" Fold Ruby, showing class and method definitions | |
function! FoldDefs() | |
let @/='\(module\ \|class\ \|has_many\ \|belongs_to\ \|_filter\ \|helper\ \|belongs_to\ \|def\ \|private\|protected\)' | |
setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2 | |
endfunction | |
map ,zd :call FoldDefs()<CR> | |
" Set the text that represents folded lines to a simple dash, showing no | |
" information. | |
" This way, when viewing folded specs and classes, there is minimal cruft on | |
" the screen to distract from the unfolded content. | |
set foldtext=MyFoldText() | |
function! MyFoldText() | |
return "-" | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I sure will, thanks for getting back to me.