Difference between revisions of "Vim"

From Noah.org
Jump to navigationJump to search
m
Line 5: Line 5:
 
     [http://www.noah.org/engineering/dotfiles/vimrc .vimrc]
 
     [http://www.noah.org/engineering/dotfiles/vimrc .vimrc]
  
I tried to balance compatability and familiarity with  
+
I tried to balance vi compatability and familiarity with  
standard 'vi' with adding time-saving features.
+
with new time-saving features.
 
I didn't want to make my vim so different that  
 
I didn't want to make my vim so different that  
I'd go crazy when I had to go back
+
I would go crazy when I had to go back
to a machine without all my customizations.
+
to a machine without my customizations.
  
 
== Folding ==
 
== Folding ==

Revision as of 07:36, 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. 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:

" 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>