3

I have recently started using vim and i have been playing around with it for sometime now.

i use FZF with Ag to get searching files and searching in files done. but i am not able to search in particular file types for example

i want to search "getUserInfo" only in .js files.

here are my configs

bashrc

[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
export FZF_DEFAULT_COMMAND='ag -g ""'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

init.vim

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'

i simply use :Ag in vim to search in entire directory

hannad rehman
  • 4,133
  • 3
  • 33
  • 55

3 Answers3

0

You can use --js to restrict the search to JavaScript files:

$ ag getUserInfo --js

See $ man ag.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • I don't use that specific plugin or any other to integrate `ag` so I can't answer that question. Try it and see. – romainl Jun 04 '20 at 18:18
0

To use a file type search with FZF, you'll need to add a customized configuration in your .vimrc. For example, if you only wanted to search fo javascript files, you add this:

autocmd! VimEnter * command! -nargs=* -complete=file AgJS :call fzf#vim#ag_raw('--js '. <q-args> .' ~/myRepo/src/')

Note the specification of a directory (~/myRepo/src/). To invoke this kind of search, you'd use the :AgJS command. That command in turn, can be bound to some other mapping.

Customization is discussed in the plugin documentation: https://github.com/junegunn/fzf.vim#example-customizing-files-command

Your specific feature request was discussed in an issue: https://github.com/junegunn/fzf.vim/issues/92

gregory
  • 10,969
  • 2
  • 30
  • 42
0

Both of the other answers did not work for me, after digging through related issues to the one Gregory linked I found a solution that worked for me:

let s:ag_options = ' --python '

command! -bang -nargs=* Agpy call fzf#vim#ag(<qargs>,s:ag_options,<bang>0)

If you type

ag --list-file-types

Into the terminal you can determine what to replace --python with in the above example to capture the correct filetype.

Finally you simply type :Agpy or whatever you name the command into vim to execute.

This also executes in whatever your present working directory is, which is preferable to only a fixed directory in the other example.

  • I like the approach, but there seem to be a few typos in your given command. In the end I got it to work and made it into a plugin; feel free to submit a PR for the filetypes you end up needing; I only added the 4 I work with right now. https://github.com/TamaMcGlinn/vim-agtypes – TamaMcGlinn Jul 04 '22 at 14:00