I have this in my .vimrc:

" The Silver Searcher
if executable('ag')
  " Use ag over grep
  set grepprg=ag\ --nogroup\ --nocolor\ --ignore\ wiki --ignore\tests    

" bind \ (backward slash) to grep shortcut
command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
nnoremap \ :Ag<SPACE>

it doesn't work, everytime I run vim i get this error

Error detected while processing /Users/abdullah/.vimrc:
line   52:
E518: Unknown option: --ignore\tests
Press ENTER or type command to continue

when i press \ it gives me :Ag in the prompt, but then doesn't ignore the tests directory (just the wiki one).

although if I do the same from the command line, it works just fine:

ag --nogroup --nocolor --ignore tags --ignore tests %search_term%

what am i missing?

  • I think you need a whitespace in your command. Your mapping is --ignore\tests while your working command is --ignore tests. Your mapping should be --ignore tests (or --ignore\ tests if the space needs an escape) – statox Nov 2 '17 at 10:09

You haven't escaped your command correctly:

set grepprg=ag\ --nogroup\ --nocolor\ --ignore\ wiki --ignore\tests
                                                    ^        ^

There is a missing backslash after wiki and a missing space before tests.

Vim is therefore interpreting this as if it were two set commands:

set grepprg=ag\ --nogroup\ --nocolor\ --ignore\ wiki
set --ignore\tests

--ignore\tests isn't a Vim option, hence the error. And you're therefore also not passing that ignore argument to ag, so the tests directory is not ignored.

N.B. It's easier to set options including whitespace using let:

let &grepprg = "ag --nogroup --nocolor --ignore wiki --ignore tests"

Further reading

  • :h :set-args (the bit right down the bottom, before :set-verbose)
  • :h :let-option
  • ok cool.. bonus question: what's grepprg anyways? why is it shoved into the middle of all of this? – abbood Nov 2 '17 at 11:27
  • 1
    @abbood It's used to override the command that is used when you run Vim's :grep command. See :help 'grepprg'. :grep is used by the :Ag plugin you've installed under the hood to do the actual searching. Try using :grep instead of :Ag. – Rich Nov 2 '17 at 11:36
  • 1
    I would recommend using the --vimgrep option instead of --nocolor and --nogroup. It also has the added bonus of finding multiple matches per line just like :vimgrep /pat/g. e.g. let &grepprg = 'ag --vimgrep --ignore wiki --ignore tests $*'. If you use --vimgrep you should also set 'grepformat' to set grepformat=%f:%l:%c:%m according to man ag. – Peter Rincker Nov 2 '17 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.