Created
August 14, 2013 20:30
-
-
Save alecthomas/6235244 to your computer and use it in GitHub Desktop.
Fold Python docstrings in VIM.
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
" | |
" Fold multi-line Python comments into one line. | |
" | |
" Also maps the "-" key to toggle expansion and <C-f> to toggle all folding. | |
" | |
setlocal foldmethod=syntax | |
setlocal foldtext=FoldText() | |
setlocal fillchars= | |
map <buffer> - za | |
map <buffer> <C-f> :call ToggleFold()<CR> | |
let b:folded = 1 | |
hi Folded gui=bold cterm=bold guifg=cyan ctermfg=cyan guibg=NONE ctermbg=NONE | |
function! ToggleFold() | |
if b:folded == 0 | |
exec "normal! zM" | |
let b:folded = 1 | |
else | |
exec "normal! zR" | |
let b:folded = 0 | |
endif | |
endfunction | |
function! s:Strip(string) | |
return substitute(a:string, '^[[:space:][:return:][:cntrl:]]\+\|[[:space:][:return:][:cntrl:]]\+$', '', '') | |
endfunction | |
" Extract the first line of a multi-line comment to use as the fold snippet | |
function! FoldText() | |
let l:snippet = getline(v:foldstart) | |
if len(s:Strip(l:snippet)) == 3 | |
let l:snippet = strpart(l:snippet, 1) . s:Strip(getline(v:foldstart + 1)) | |
endif | |
return '+' . l:snippet . ' ...' | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment