Fandom

Vim Tips Wiki

Show only lines in quickfix list for current buffer

Redirected from VimTip76

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 76 Printable Monobook Previous Next

created 2001 · complexity intermediate · version 7.0


After executing :make or :grep you can browse the list of errors/matches, and the appropriate source code locations, with commands like :cnext. :help quickfix

This tip folds away lines in the current buffer that have no errors (when using :make), or that do not match the search pattern (when using :grep). See here to fold lines in the quickfix list.

Fold away missesEdit

The following script can be used to fold away lines with no errors/matches.

Usage

  • Create file ~/.vim/plugin/foldmisses.vim (Unix) or $HOME/vimfiles/plugin/foldmisses.vim (Windows) containing the script below, then restart Vim.
  • Edit a file.
  • Enter a command like :vimgrep /regexp/ % to get a quickfix list.
  • Enter :FoldMisses to fold away lines in the current buffer that are not in the quickfix list.
  • Enter :FoldLMisses to fold away lines in the current buffer that are not in the buffer's location list.
  • Optional: :1FoldMisses will give 1 extra context line (fewer lines folded). You can set a default via let g:foldmisses_context = 1
  • As normal, za toggles a fold, and zR opens all folds.
if ! exists('g:foldmisses_context')
  let g:foldmisses_context = 0
endif

" Add manual fold from line1 to line2, inclusive.
function! s:Fold(line1, line2)
  if a:line1 < a:line2
    execute a:line1.','.a:line2.'fold'
  endif
endfunction

" Return list of line numbers for current buffer found in quickfix list.
function! s:GetHitLineNumbers(list)
  let result = []
  for d in a:list
    if d.valid && d.bufnr == bufnr('')
      call add(result, d.lnum)
    endif
  endfor
  return result
endfunction

function! s:FoldMisses(list, context)
  setlocal foldmethod=manual
  normal! zE
  let extra = a:context == 99999 ? g:foldmisses_context : a:context
  let last = 0
  for lnum in s:GetHitLineNumbers(a:list)
    let start = last==0 ? 1 : last+1+extra
    call s:Fold(start, lnum-1-extra)
    let last = lnum
  endfor
  call s:Fold(last+1+extra, line('$'))
endfunction

":[N]FoldMisses [N]     Show only the lines (and surrounding [N] lines
":[N]FoldLMisses [N]    of context) in the current buffer that appear
"                       in the quickfix / location list.
"                       Missed, error-free lines are folded away.
command! -bar -count=99999 FoldMisses call s:FoldMisses(getqflist(), <count>)
command! -bar -count=99999 FoldLMisses call s:FoldMisses(getloclist(0), <count>)

CommentsEdit

Also on Fandom

Random Wiki