Hi ,
I want to fold set of lines after search as follows,
The mouse can also be used to open and close folds by following steps:
Click on a '+' to open the closed fold at this row.
Click on any other non-blank character to close the open fold at this row
I want to search click and collapse all matching lines.
The mouse can also be used to open and close folds by followingsteps:
+--
There is a method to collapse the patterns which are not matching in vim , after searching a pattern we can collapse non matching patterns by "\z" key .
nnoremap \z :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2<CR>
Is there any option to do the opposite? Just find a pattern and collapse?
I got an answer to this question from reddit user on reddit vim forum.
https://www.reddit.com/r/vim/comments/91qz90/search_a_pattern_and_fold_the_matching_lines_in/
function! FoldSearchPattern() abort
if !exists('w:foldpatterns')
let w:foldpatterns=[]
setlocal foldmethod=expr foldlevel=0 foldcolumn=2
endif
if index(w:foldpatterns, @/) == -1
call add(w:foldpatterns, @/)
setlocal foldexpr=SetFolds(v:lnum)
endif
endfunction
function! SetFolds(lnum) abort
for pattern in w:foldpatterns
if getline(a:lnum) =~ pattern
if getline(a:lnum + 1) !~ pattern
return 's1'
else
return 1
endif
endif
endfor
endfunction
nnoremap \z :call FoldSearchPattern()<CR>
Hope it is helpful.