On 7/13/25 15:24, Joseph Wulf wrote: > In regular/normal vim editing, is there a method to collapse out of > view all lines NOT matching "/pattern"?
Joe, Vim folding can be used to hide lines from view (see `:help folding`). I've got a few functions and commands in my vimrc for this; `:FoldSearch pattern` shows lines matching `pattern` and folds away everything else. Here are my notes about using these commands: ``` Folding by regular expression *notes_folding_regex* To fold away lines that match a regex, use the custom command :Fold: > :Fold regex ; lines matching regex are hidden :Fold ; use last search pattern (@/) for regex Use the custom command :FoldSearch to search for lines matching a regex and fold away everything else: > :FoldSearch regex ; show lines matching regex, and fold away all else :FoldSearch ; use last search pattern (@/) for regex With :FoldSearch, increasing the 'foldlevel' to 1 will show one line of context around all matches. To fold away comments and blank lines, use this custom command: > :FoldComments ------------------------------------------------------------------------------ QuickFix entry folding *notes_quickfix_folding* To group lines of QuickFix output together by files or by directories, use one of the following commands when a QuickFix or Location List window is focused: *FoldQuickFixFiles* *FoldQuickFixDirs* :[count] FoldQuickFixFiles :[count] FoldQuickFixDirs When [count] is 0 (the default), the folds will start closed; when [count] is 1, the folds will start open. When a QuickFix or Location List window is opened, :1FoldQuickFixFiles is applied by :SetupQuickFix, such that the folds start open. Use standard Vim shortcut keys within the QuickFix or Location List window to change folding levels: zr Reduce amount of folding zR Reduce amount of folding fully (no folding remaining) zm More folding zM More folding (fully folded) zo Open a fold zc Close a fold In Gvim, you may also click on the symbols in the left margin of the QuickFix or Location List window to toggle individual folds. ``` Below are the functions and commands from vimrc. ```vim " ============================================================= " Folding " ============================================================= function! FoldShowExpr() let maxLevel = 2 let level = 0 while level < maxLevel if getline(v:lnum - level) =~ @/ break endif if level != 0 && (getline(v:lnum + level) =~ @/) break endif let level = level + 1 endwhile return level endfunction function! FoldHideExpr() return (getline(v:lnum) =~ @/) ? 1 : 0 endfunction function! FoldRegex(foldExprFunc, regex) if a:regex != "" let @/=a:regex call histadd("search", a:regex) endif let &l:foldexpr = a:foldExprFunc . '()' setlocal foldmethod=expr setlocal foldlevel=0 setlocal foldcolumn=0 setlocal foldminlines=0 setlocal foldenable " Return to manual folding now that folds have been applied. setlocal foldmethod=manual endfunction " Search (and "show") regex; fold everything else. command! -nargs=? FoldSearch call FoldRegex('FoldShowExpr', <q-args>) " Fold matching lines ("hide" the matches). command! -nargs=? Fold call FoldRegex('FoldHideExpr', <q-args>) " Fold away comment lines (including blank lines). " TODO: Extend for more than just shell comments. command! -nargs=? FoldComments Fold ^\s*#\|^\s*$ " 'foldexpr' for extracting folding information from QuickFix buffer. " pattern - used to extract portion of QuickFix path from line. function! FoldQuickFixPatternFoldExpr(pattern) let thisLine = getline(v:lnum) let nextLine = getline(v:lnum + 1) let thisKey = matchstr(thisLine, a:pattern) let nextKey = matchstr(nextLine, a:pattern) if thisKey != nextKey return '<1' else return '1' endif endfunction function! FoldQuickFixDirsFoldExpr() return FoldQuickFixPatternFoldExpr('\v^.*[/\\]') endfunction " Fold QuickFix window entries by directory. " level - initial foldlevel (0 => fold everything, 1 => expand all folds) function! FoldQuickFixDirs(level) let &l:foldlevel = a:level setlocal foldcolumn=1 setlocal foldmethod=expr setlocal foldexpr=FoldQuickFixDirsFoldExpr() endfunction command! -count=0 FoldQuickFixDirs call FoldQuickFixDirs(<count>) function! FoldQuickFixFilesFoldExpr() return FoldQuickFixPatternFoldExpr('\v^[^|]*') endfunction " Fold QuickFix window entries by filename. " level - initial foldlevel (0 => fold everything, 1 => expand all folds) function! FoldQuickFixFiles(level) let &l:foldlevel = a:level setlocal foldcolumn=1 setlocal foldmethod=expr setlocal foldexpr=FoldQuickFixFilesFoldExpr() setlocal foldenable endfunction command! -count=0 FoldQuickFixFiles call FoldQuickFixFiles(<count>) ``` Michael Henry -- -- You received this message from the "vim_use" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_use" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/vim_use/5b30f32c-c918-40e6-98a4-a9f30c0b7b94%40drmikehenry.com.