Folding away based on a seach

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Folding away based on a seach

Samuel Wright
HI All,

I have a todo list of single line entries.
I'd like to fold everything away apart from a custom seach, say

:customsearch urgent

would fold away all lines that did not contain urgent.

Has this been done already in some way?

If not, can I search, get the line numbers with results on, and do the
math to fold away everything else?

Ie, if lines 3 and 8 contain the term,
mark 1-2, fold
mark 4-7 fold
mark 9-end fold

Thanks for your help
S
Reply | Threaded
Open this post in threaded view
|

Re: Folding away based on a seach

Tim Chase-2
> would fold away all lines that did not contain urgent.
>
> Has this been done already in some way?
>
> If not, can I search, get the line numbers with results on, and do the
> math to fold away everything else?

:set foldmethod=expr
:set foldexpr=getline(v:lnum)!~'urgent'


should do what you describe.  You can learn more at

        :help foldmethod
        :he foldexpr
        :he fold-expr

(this abuses the fact that boolean-true is non-zero and
boolean-false is zero which can be used as fold-levels)

Gotta love the succinctness of vim. :)

-tim



Reply | Threaded
Open this post in threaded view
|

Re: Folding away based on a seach

Noah Spurrier
In reply to this post by Samuel Wright
> HI All,
>
> I have a todo list of single line entries.
> I'd like to fold everything away apart from a custom seach, say
>
> :customsearch urgent
>
> would fold away all lines that did not contain urgent.
>
> Has this been done already in some way?
>
> If not, can I search, get the line numbers with results on, and do the
> math to fold away everything else?
>
> Ie, if lines 3 and 8 contain the term,
> mark 1-2, fold
> mark 4-7 fold
> mark 9-end fold
>
> Thanks for your help
> S
>

I have this map setup in my .vimrc, so \z will hide all lines that
don't contain the current search pattern.
    map <silent><leader>z :set foldexpr=getline(v:lnum)!~@/ foldlevel=0
foldcolumn=0 foldmethod=expr<CR>

So in your case, you would search for URGENT in the usual way
   /URGENT
then type \z to hide every line that doesn't have URGENT
   \z

It comes in handy quite often. This is about the only way that
I ever use folding in fact.

Yours,
Noah


Reply | Threaded
Open this post in threaded view
|

Re: Folding away based on a seach

Jean-Rene David-2
In reply to this post by Samuel Wright
* Samuel Wright [2007.01.11 06:15]:
> I have a todo list of single line entries.
> I'd like to fold everything away apart from a custom seach, say
>
> :customsearch urgent
>
> would fold away all lines that did not contain urgent.
>
> Has this been done already in some way?

Yes.

http://www.vim.org/scripts/script.php?script_id=158

--
JR