Skip to content

Instantly share code, notes, and snippets.

@g0xA52A2A
Last active August 23, 2020 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save g0xA52A2A/d59600bde83dd4f7353aef8011315f2f to your computer and use it in GitHub Desktop.
Save g0xA52A2A/d59600bde83dd4f7353aef8011315f2f to your computer and use it in GitHub Desktop.

A simplified variant of vim-addon-qf-layout.

  • Format location like grep -H
  • Align start of contents from all files
  • Sperate location and contents with |
function! QuickFixFormat()
  let qflist = map(getqflist(),
    \ "extend(v:val, {'filename' : bufname(v:val.bufnr)})")

  let prefix_len = max(map(copy(qflist),
    \ "strchars(v:val.filename . ':' . v:val.lnum)"))

  let fmt = '%-' . prefix_len . 's' . '%s'

  setlocal modifiable

  call setline('1', map(qflist,
    \ "printf(fmt, v:val.filename . ':' . v:val.lnum, ' | ' . v:val.text)"))

  setlocal nomodifiable nomodified
endfunction

augroup QuickFixFormat
  autocmd!
  autocmd BufReadPost quickfix
    \ if !getwininfo(win_getid())[0]['loclist']
    \ |   call QuickFixFormat
    \ | endif
augroup END

As an update Vim 8.2.0869 introduces the quickfixtextfunc option which can be set to a function intended to format the contents of the quickfix window. However this function is called for each entry rather than the entire list. As such it is useless for aligning text other than on predetermined fixed boundaries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment