Wikia

Vim Tips Wiki

Changes: Show only lines in quickfix list for current buffer

Edit

Back to page

(Minor tweaks.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 11: Line 11:
 
|category2=Folding
 
|category2=Folding
 
}}
 
}}
After executing <tt>:make</tt> or <tt>:grep</tt> you can browse the list of errors/matches, and the appropriate source code locations, with commands like <tt>:cnext</tt>. {{help|quickfix}}
+
After executing <code>:make</code> or <code>:grep</code> you can browse the list of errors/matches, and the appropriate source code locations, with commands like <code>:cnext</code>. {{help|quickfix}}
   
This tip [[Folding|folds away lines]] in the current buffer that have no errors (when using <tt>:make</tt>), or that do not match the search pattern (when using <tt>:grep</tt>). See [[VimTip801|here]] to fold lines in the quickfix list.
+
This tip [[Folding|folds away lines]] in the current buffer that have no errors (when using <code>:make</code>), or that do not match the search pattern (when using <code>:grep</code>). See [[VimTip801|here]] to fold lines in the quickfix list.
   
 
==Fold away misses==
 
==Fold away misses==
Line 19: Line 19:
   
 
'''Usage'''
 
'''Usage'''
*Create file <tt>~/.vim/plugin/foldmisses.vim</tt> (Unix) or <tt>$HOME/vimfiles/plugin/foldmisses.vim</tt> (Windows) containing the script below, then restart Vim.
+
*Create file <code>~/.vim/plugin/foldmisses.vim</code> (Unix) or <code>$HOME/vimfiles/plugin/foldmisses.vim</code> (Windows) containing the script below, then restart Vim.
 
*Edit a file.
 
*Edit a file.
*Enter a command like <tt>:vimgrep /regexp/ %</tt> to get a quickfix list.
+
*Enter a command like <code>:vimgrep /regexp/ %</code> to get a quickfix list.
*Enter <tt>:FoldMisses</tt> to fold away lines in the current buffer that are not in the quickfix list.
+
*Enter <code>:FoldMisses</code> to fold away lines in the current buffer that are not in the quickfix list.
*Enter <tt>:FoldLMisses</tt> to fold away lines in the current buffer that are not in the buffer's location list.
+
*Enter <code>:FoldLMisses</code> to fold away lines in the current buffer that are not in the buffer's location list.
*Optional: <tt>:1FoldMisses</tt> will give 1 extra context line (fewer lines folded). You can set a default via <tt>let g:foldmisses_context = 1</tt>
+
*Optional: <code>:1FoldMisses</code> will give 1 extra context line (fewer lines folded). You can set a default via <code>let g:foldmisses_context = 1</code>
*As normal, <tt>za</tt> toggles a fold, and <tt>zR</tt> opens all folds.
+
*As normal, <code>za</code> toggles a fold, and <code>zR</code> opens all folds.
   
 
<pre>
 
<pre>

Latest revision as of 05:12, July 13, 2012

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

Around Wikia's network

Random Wiki