Vim

From Noah.org
Revision as of 07:54, 6 November 2006 by Root (talk | contribs) (→‎Folding)
Jump to navigationJump to search

My .vimrc

This is my super-terrific .vimrc file.

   .vimrc

I tried to balance vi compatability and familiarity with with new time-saving features. I didn't want to make my vim so different that I would go crazy when I had to go back to a machine without my customizations.

Folding

Most folding is complicated and bothersome. These notes describe easy Vim folding.

My favorite trick is to fold on the current /search/ pattern. This way I can see only the lines that contain what I'm searching for. This is handy even for regular text documents, not just code.

When I fold code, I just want show class, method, and function names. In other words, I just want an index view of my code. I don't want to fold on every nested statement, so I only need one level of folding.

I add a map so that I can quickly set the search pattern to find all classes and functions. The following search pattern works for both PHP and Python:

   map zff :/^\s*class\s\\|^\s*function\s\\|^\s*def\s/<CR>

I combine this with the search folding trick so that I can quickly fold code without the bother of syntax files or nested folding regions.

Finally, I add a map so that the SPACE key will toggle the hide/show state of a fold region under the cursor.

To use these tips, in normal mode type "zff" to highlight all classes and functions. Then type "\z" to fold. Now the document looks like an index. Press SPACE to view a class or function.

Put the following in your .vimrc file:

" folding using /search/ pattern
"     \z
" This folds every line that does not contain the search pattern.
" see vimtip #282 and vimtip #108
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1
map <silent><leader>z :set foldmethod=expr foldlevel=0 foldcolumn=1<CR>
" think mnemonic: "function fold"
map zff :/^\s*class\s\\|^\s*function\s\\|^\s*def\s/<CR> 
nnoremap <silent><space> :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<CR>