Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make :Ag not match file names, only the file content #346

Closed
maximbaz opened this issue Mar 22, 2017 · 16 comments
Closed

Make :Ag not match file names, only the file content #346

maximbaz opened this issue Mar 22, 2017 · 16 comments
Labels

Comments

@maximbaz
Copy link

@maximbaz maximbaz commented Mar 22, 2017

  • Category
    • Question
    • Bug
    • Suggestion
  • OS
    • Linux
    • macOS
    • Windows
    • Etc.
  • Vim
    • Vim
    • Neovim

Since there is a command :Files that can search files by name, I'd like to configure :Ag to only look at the file contents, not at the file names.

I'll demostrate my case below, my question is: can this be achieved today, maybe with some configuration?


To see why this is an annoying issue, create a file called demo.txt, put 20 lines each having a random number, and on the 21st line write demo. Then use :Ag demo to search for this string.

Expected: you find only one occurrence.
Actual: you also find 20 irrelevant occurrences.

image

@junegunn

This comment has been minimized.

Copy link
Owner

@junegunn junegunn commented Mar 22, 2017

Try this:

command! -bang -nargs=* Ag call fzf#vim#ag(<q-args>, {'options': '--delimiter : --nth 4..'}, <bang>0)
@junegunn junegunn closed this Mar 22, 2017
@junegunn junegunn added the question label Mar 22, 2017
@maximbaz

This comment has been minimized.

Copy link
Author

@maximbaz maximbaz commented Mar 23, 2017

It looks like magic, but works beautifully, thanks @junegunn! 🎉

@mbylstra

This comment has been minimized.

Copy link

@mbylstra mbylstra commented Feb 23, 2018

I'm trying to figure out how to run that command on startup. Any tips?

@backdoer

This comment has been minimized.

Copy link

@backdoer backdoer commented Feb 23, 2018

Put it in your .vimrc @mbylstra

@mbylstra

This comment has been minimized.

Copy link

@mbylstra mbylstra commented Feb 23, 2018

🤦‍♂ I thought I had to prepend it with autocmd which wasn't working. Thanks!

@vadimshvetsov

This comment has been minimized.

Copy link

@vadimshvetsov vadimshvetsov commented Jul 17, 2018

Is it possible to combine this with my current Ag tweaks?
I have the following:

command! -bang -nargs=* Ag
  \ call fzf#vim#ag(<q-args>,
  \                 {'options': '--delimiter : --nth 4..'}, " Can't add this
  \                 <bang>0 ? fzf#vim#with_preview('up:60%')
  \                         : fzf#vim#with_preview('right:50%:hidden', '?'),
  \                 <bang>0)
@vadimshvetsov

This comment has been minimized.

Copy link

@vadimshvetsov vadimshvetsov commented Aug 13, 2018

@junegunn could you please help, is it possible to combine preview options with search only by files with Ag?

@junegunn

This comment has been minimized.

Copy link
Owner

@junegunn junegunn commented Aug 13, 2018

* fzf#vim#with_preview([[options], preview window, [toggle keys...]])

So you have to write

fzf#vim#with_preview({'options': '--delimiter : --nth 4..'}, 'up:60%')
@vadimshvetsov

This comment has been minimized.

Copy link

@vadimshvetsov vadimshvetsov commented Aug 13, 2018

@junegunn Thank you so much, you've saved my day!

@jssee

This comment has been minimized.

Copy link

@jssee jssee commented Oct 11, 2018

@junegunn i used the method you described above but im experiencing some strange behavior.

i want to have 2 options:

  1. use the default :Ag behavior of searching all file names and content.
  2. use Ag to only search the current file contents.

Im noticing that with my current config, this only works some in some projects, but not others. and on occasion, it will only allow me to use option 2 once in a project, and every time afterwards it goes back to the default behavior. do you have any idea why this is?

here is my config:

nnoremap <Leader>sa :Ag<CR>
nnoremap <Leader>ss :Ag!<CR>

command! -bang -nargs=* Ag
  \ call fzf#vim#ag(<q-args>,
  \                 <bang>0 ? fzf#vim#with_preview({'options': '--delimiter : --nth 4..'}, 'up:60%')
  \                         : fzf#vim#with_preview('right:50%:hidden', '?'),
  \                 <bang>0)

EDIT: added video to demonstrate issue
https://streamable.com/7kbs9

@jssee jssee mentioned this issue Oct 30, 2018
3 of 9 tasks complete
@01abhishekjain

This comment has been minimized.

Copy link

@01abhishekjain 01abhishekjain commented Mar 21, 2019

Try this:

command! -bang -nargs=* Ag call fzf#vim#ag(<q-args>, {'options': '--delimiter : --nth 4..'}, <bang>0)

Thanks for this! Can anyone care to explain (briefly) what this exactly does?

@aos

This comment has been minimized.

Copy link

@aos aos commented May 8, 2019

Try this:

command! -bang -nargs=* Ag call fzf#vim#ag(<q-args>, {'options': '--delimiter : --nth 4..'}, <bang>0)

Thanks for this! Can anyone care to explain (briefly) what this exactly does?

Basically, the {'options'...} part is passed into fzf. If you look at the man page for fzf, you'll notice you can pass a delimiter when searching, in this case our delimiter is colon :. Then we tell it to only search from the 4th item onwards... these are the file contents only.

If you look at the results of :Ag, it uses : as the separator for each result.

bdlangton added a commit to bdlangton/dotfiles that referenced this issue Jun 28, 2019
@danamkaplan

This comment has been minimized.

Copy link

@danamkaplan danamkaplan commented Jul 17, 2019

Can anyone help me write the equivalent for Ripgrep? This:

command! -bang -nargs=* Rg call fzf#vim#rg(<q-args>, {'options': '--delimiter : --nth 4..'}, <bang>0)

Gives me the error
E117: Unkown function: fzf#vim#rg

@fabiomcosta

This comment has been minimized.

Copy link

@fabiomcosta fabiomcosta commented Aug 5, 2019

@danamkaplan fzf#vim#rg function doesn't exists, as the error message says, so you have to use the fzf#vim#grep function.

I got it working with the following command:

command! -bang -nargs=* Rg
  \ call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1, {'options': '--delimiter : --nth 4..'}, <bang>0)

Got that from

\'command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1, <bang>0)',
and then added the same argument that has been provided to Ag.

@fabiomcosta

This comment has been minimized.

Copy link

@fabiomcosta fabiomcosta commented Aug 5, 2019

Oh just noticed this has also been suggested at #714 (comment)

@avral

This comment has been minimized.

Copy link

@avral avral commented Aug 29, 2019

Thank's!

yous added a commit to yous/dotfiles that referenced this issue Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.