views:

101

answers:

1

I've seen magical Vim commands before that you could add to your .vimrc to have folds created upon opening a particular type of file. I remember having such code that would create the folds, upon opening the file, at every Ruby method and class. Then, with one command, I could collapse all those method folds. Does anyone know how to do this with inline views in PL/SQL? Say I have the following SQL:

SELECT blah,
       teh_max
FROM (
       SELECT blah,
              MAX(bar) AS teh_max
       FROM (
              SELECT blah,
                     bar
              FROM foo
            )
       GROUP BY blah
     )
ORDER BY blah

I would like folds to be created when I open this in Vim so that I can go to a FROM ( line, hit zc in command mode, and have the inline view starting at that line be collapsed. It would be nice to collapse all the folds with one command, too.

+3  A: 

Folding based on syntax is activated by setting foldmethod to syntax:

" for all windows
set foldmethod=syntax
" for the current window
setlocal foldmethod=syntax

The folding must then be specified within the syntax definition, which is done by providing the fold argument to regions which should increase the fold level. To quote the documentation:

The "fold" argument makes the fold level increase by one for this item.
Example: 
   :syn region myFold start="{" end="}" transparent fold
   :syn sync fromstart
   :set foldmethod=syntax
This will make each {} block form one fold.

So you'll have to go into the syntax files for whatever filetypes you care about, and add the fold argument to the appropriate regions, or potentially add in your own regions. In your case, it looks like it's fairly similar to the C/C++ syntax's fold-by-braces, except with parentheses.

The default syntax files are generally kept in /usr/share/vim/vimXX/syntax on Linux (and presumably <vim-directory>\vimXX\syntax on windows?) where XX is the version number without the period (e.g. 72). These may be overridden system-wide by files in /usr/share/vim/vimfiles/syntax or per-user by files in ~/.vim/syntax.

Jefromi
I tried adding folds where the start and end are just parentheses but that doesn't work because so much other stuff in SQL uses parentheses, e.g., `MAX(bar)`. Setting `start="FROM ("` didn't work either; it didn't recognize a fold there.
Sarah Vessels