Difference between revisions of "Vim"

From Noah.org
Jump to navigationJump to search
Line 13: Line 13:
 
== Folding ==
 
== Folding ==
  
Most folding is complicated and bothersome.
+
Most folding is complicated and bothersome. These notes describe easy Vim folding.
When I use code folding, I just want show class, method, and function names. That is, I just want a high-level index view of my code.
 
I don't want every single nested statement folded. I only need one level of folding. I use very simple folding
 
based on vimtip #282 and #108. First, I fold based
 
on the current /search/ pattern. This is handy even for
 
non-code documents. I can do a search and then
 
fold everything so that I only see the lines with that pattern.
 
Second, I add a map so that I can quickly set the search pattern
 
to find all classes and functions. This generic search pattern
 
will work for both PHP and Python. I'd like to extend it for other languages. Finally, I add a map so that the SPACE key will toggle the hide/show state of a fold region under the cursor.
 
So, in normal mode type "zff" to highlight all classes and functions.
 
Then type "\z" (you type the backslash key, then the z key)
 
to fold everything else. Now the document looks like an index. Press SPACE to view a class or function.
 
  
To do all this I put the following in my .vimrc file:
+
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:
 
<pre>
 
<pre>
 
" folding using /search/ pattern
 
" folding using /search/ pattern
" \z
+
"     \z
 
" This folds every line that does not contain the search pattern.
 
" This folds every line that does not contain the search pattern.
 
" see vimtip #282 and vimtip #108
 
" see vimtip #282 and vimtip #108

Revision as of 07:54, 6 November 2006

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>