abstract
| - Folding based on indentation is the easiest and most intuitive way to hide sections of a file with minimal special formatting. However, I wanted a section of lines with the same indent to fold into a heading with less indent. This is not what the default behavior is, which folds into the first line with the same indent: Unfolded text: heading A line 1 line 2 line 3 heading B Default behavior (folded based on indent): heading A line 1 heading B What I wanted when folded: heading A heading B So, I put this in my vimrc to implement folding: setlocal foldmethod=expr setlocal foldexpr=(getline(v:lnum)=~'^$')?-1:((indent(v:lnum)'.indent(v:lnum+1)):indent(v:lnum)) set foldtext=getline(v:foldstart) set fillchars=fold:\ "(there's a space after that \) highlight Folded ctermfg=DarkGreen ctermbg=Black This will create folds based on a single-space indent (I wanted to be able to build a hierarchy many many levels deep, and using a tab for the fold indents very quickly pushed my text off the window, but I didn't want to mess with the tabstop). Blank lines are folded based on the surrounding indentation (they are not counted as a 0 indent line). I quickly got annoyed by the default key mappings for folding/unfolding sections, so I remapped Shift-Left/Shift-Right to close/open: nnoremap zo inoremap zo nnoremap zc inoremap zc " Shift-Up Shift-Down (incase Shift is held while browsing folds) nmap imap nmap imap But, I found that the default mappings for Alt-(Arrows) and Ctrl-(Arrows) sometimes caused Vim to do strange stuff, and I would occasionally hit those by accident, so I remapped those as well: " modified arrow keys do bad things by default " Ctrl-(Up, Down, Left, Right) noremap noremap! noremap noremap! noremap noremap! noremap noremap! " Alt-(Up, Down, Left, Right) noremap noremap! noremap noremap! noremap noremap! noremap noremap!
|