Search code examples
vimag

Vim, Ag and Quickfix window, limit output width and height


Is there a way to limit :Ag output so it always takes one line and doesn't blow up the quickfix window?

At the moment it looks like this and it's awful. I can't see filenames, everything is super slow and just sucks:

Update For the record, I scrolled Quickfix window a bit to illustrate the point better. And while it is usable via :cn :cp, I would like to be able to quickly glance over the results with j k.


Solution

  • Looking over the man page, there does not seem to be any way to limit the output built into Ag itself.

    Is there another way of limiting the line length? Actually, you do have the built in "cut" command in Linux, e.g. using it on the shell:

    ag --column foo | cut -c 1-80
    

    Limit all lines to 80.

    Now we have to make ag.vim execute our specially crafted command, for which the g:agprg exists. So the first thing I thought of is this:

    let g:agprg='ag --column \| cut -c 1-80'  " doesn't work
    

    The problem with this is that the ag.vim plugin just appends extra arguments to the end, and thus you end up executing something like ag --column | cut -c 1-80 something-i-searched-for. Is there a way to directly "insert" the arguments before the |?

    One trick is to use a temporary shell function, like this:

    f() { ag --column "$@" | cut -c 1-80 }; f something-i-search-for
    

    Unfortunately, we still can't use this. ag.vim checks whether or not the first word is an actual command. So it complains that no executable by the name of "f()" exists. So my final solution:

    let g:agprg='true ; f(){ ag --column "$@" \| cut -c 1-80 }; f'
    

    As true always exists and doesn't do anything except return true, this works!

    To your actual screenwidth instead of 80, you could use:

    let g:agprg='true ; f(){ ag --column "$@" \| cut -c 1-'.(&columns - 6).' }; f'
    

    I added the magic - 6 here to account for the extra characters Vim itself adds.