2

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.

Community
  • 1
  • 1
firedev
  • 20,898
  • 20
  • 64
  • 94
  • Oh! I just thought of a silly workaround to add to `~/.vim/after/ftplugin/qf.vim`: `:nnoremap /^.*\|\d* col \d*\|` and `:nnoremap ?^.*\|\d* col \d*\|`. – romainl Oct 30 '14 at 10:28
  • That didn't work out that nice as `cut` did – firedev Oct 30 '14 at 10:42

4 Answers4

4

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.

Daan Bakker
  • 6,122
  • 3
  • 24
  • 23
  • Hey, that is actually pretty sweet! Is there a way to somehow eval `set columns?` and display the part of the line around the match? My knowlegde of coreutils and vimscript is a bit limited unfortunately. – firedev Oct 30 '14 at 10:23
  • To display the part around the match you could use the `column` part of `ag`s output (the format is `file:linenumber:column:line`). To do that, you'd probably need a wrapper script and put that in `g:agprg`. – Daan Bakker Oct 30 '14 at 10:40
  • Here is a [possible replacement for cut](https://gist.github.com/dbakker/7b1afef181549bffd093). It shortens very long lines but keeps (the context around) the match! – Daan Bakker Oct 30 '14 at 14:58
  • This is simply amazing, however `&columns` is not being refreshed every time, so you can't make screen smaller if you want to see more without restarting Vim. Guess it parses `agprg` when it loads it, so it might need to be extracted into a function. – firedev Oct 30 '14 at 23:32
2

ag now supports a --width switch. rg has a similar --max-columns switch.

Greg Hurrell
  • 5,177
  • 23
  • 27
1

Assuming you are using this plugin. You should add this to your ~/.vimrc as specified by :h g:ag_qhandler

let g:ag_qhandler = 'copen 1'

However you can probably just do let :g:ag_qhandler = 'cc'. This will print the results at the in the bottom. When you move through the quickfix list via :cnext or :cprev it will print the current result as well.

For more help see:

:h g:ag_qhandler
:h :cope
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • This helps in a way, but it is really inconvenient to see only one file at the time. – firedev Oct 30 '14 at 09:36
  • @Nick I apparently misunderstood. I am going to agree with @romainl that you need avoid searching through certain file. As well as setting up a `.agignore` you can also search only in certain types of files, e.g. `--ruby --python`. You may also want to do `set nowrap` in the quickfix window. What I imagine you want is something like `filename.js|col 42|...blah blah pattern blah blah...`. This type of think is not native to either Vim nor Ag so you would need to build some kind of filter to do just that. – Peter Rincker Oct 30 '14 at 13:52
  • Don't get me wrong `.agignore` is in my dotfiles now, but still I want to see what else is possible. `cut` work beautifully, didn't know you can do that. Also when you do project search you don't choose file types. You just do search and see where this identifier or variable is used and then decide what to do. – firedev Oct 30 '14 at 15:57
  • You can certainly pass options into `:Ag`. e.g. `:Ag --php pat dir/`. Please read the documentation for `:h :Ag`. You may also want to read `man ag` – Peter Rincker Oct 30 '14 at 16:54
1

Changing the geometry of the quickfix window won't help you fix your problem: the window is unusable not because of its size but because your search results are polluted by superfluous matches in minimized files.

Minimized JavaScript or CSS is the frontend development's equivalent of a binary and that kind of file should be ignored by search tools, indexing tools, file navigation tools and even version control tools, sometimes, because they are generally irrelevant.

Adding these lines to your ~/.agignore will make Ag search only in actual source files:

*.min*
*-min*
*_min*
*.min.*
bundle
min
vendor
tags
cscope.*

Adjust that list to your liking.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Good point, it got better however is there a way to filter or limit output anyway? Some files I don't want to be ignored even if they have long lines. – firedev Oct 30 '14 at 09:35