Fzf: How To Grep Using FZF / Vim

Created on 20 Jul 2014  路  7Comments  路  Source: junegunn/fzf

Is it possible to open a grep result directly in Vim ? I know of using it directly like: ag foo | fzf

Is it possible to 'sink' it to Vim - do we have an option already for that ? :FZF something ?

Below discussion here greps a file but its for a buffer and it scans each line , what I am looking for is a grep tool . My idea is to execute ag/ack {string} | fzf and open in the Vim recognizing the Line Nr and the Buffer Name . I am working on a solution using something similar but wondering if anyone has done that already ?

Source : http://www.reddit.com/r/vim/comments/28eh6k/fzf_vim_tmux/

function! BufGet()
return map(getline(1, '$'), "printf('%5d %s', v:key + 1, v:val)")
endfunction

function! LineOpen(e)
execute 'normal! '. matchstr(a:e, '[0-9]+'). 'G'
endfunction

nnoremap f :call fzf#run({
\ 'source': BufGet(),
\ 'sink': function('LineOpen'),
\ 'options': '+m',
\ 'tmux_height': '40%'
\ })

question tip

Most helpful comment

function! s:escape(path)
  return substitute(a:path, ' ', '\\ ', 'g')
endfunction

function! AgHandler(line)
  let parts = split(a:line, ':')
  let [fn, lno] = parts[0 : 1]
  execute 'e '. s:escape(fn)
  execute lno
  normal! zz
endfunction

command! -nargs=+ Fag call fzf#run({
  \ 'source': 'ag "<args>"',
  \ 'sink': function('AgHandler'),
  \ 'options': '+m',
  \ 'tmux_height': '60%'
\ })

(I'm sorry about the name :smirk:)

All 7 comments

function! s:escape(path)
  return substitute(a:path, ' ', '\\ ', 'g')
endfunction

function! AgHandler(line)
  let parts = split(a:line, ':')
  let [fn, lno] = parts[0 : 1]
  execute 'e '. s:escape(fn)
  execute lno
  normal! zz
endfunction

command! -nargs=+ Fag call fzf#run({
  \ 'source': 'ag "<args>"',
  \ 'sink': function('AgHandler'),
  \ 'options': '+m',
  \ 'tmux_height': '60%'
\ })

(I'm sorry about the name :smirk:)

Thanks , name is fine :) Will share my FZF specific vimrc GIST for Vimmers later ! Cheers

@junegunn I've just found this and it is super useful! However when I run it fzf always opens up in a full screen pane and the configs I've set for it don't apply. Here is my config:

``` " fzf config
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-i': 'split',
\ 'ctrl-s': 'vsplit' }
let g:fzf_layout = { 'down': '~20%' }

function! s:escape(path)
  return substitute(a:path, ' ', '\\ ', 'g')
endfunction

function! GrepHandler(line)
  let parts = split(a:line, ':')
  let [fn, lno] = parts[0 : 1]
  execute 'e '. s:escape(fn)
  execute lno
  normal! zz
endfunction

command! -nargs=+ FGrep call fzf#run({
  \ 'source': 'grep -srnw --exclude-dir={build,vendor,node_modules,.git} --include=*.{rb,erb,vcl,conf,jade,js,styl,php,json,config,html} --exclude={*.min.js,tags} "<args>"',
  \ 'sink': function('GrepHandler'),
\ })

nmap <C-p> :FZF<cr>
imap <c-x><c-l> <plug>(fzf-complete-line)

```

Any idea's how I can get it to maintain the 20% split?

Unrelated question, but I might as well ask here. If I set the layout to top, left or right I get a full screen panel as well. I'd love to be able to set it to top and still see the code.

Thanks

@bag-man fzf#wrap will take care of it.

call fzf#run(fzf#wrap({ ... }))

See https://github.com/junegunn/fzf#fzfwrap

Nice one thanks, Although I should add I just found this excellent article: https://medium.com/@crashybang/supercharge-vim-with-fzf-and-ripgrep-d4661fc853d2#.xgkib3w5f

Which gives an even nicer solution:

    command! -bang -nargs=* Find call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>), 1, <bang>0)

Is there a way to make fzf not search in the filename portions of that ripgrep rg command output?

@trusktr you need to supply delimiter and nth options to fzf

  command! -bang -nargs=* Rg
        \ call fzf#vim#grep(
        \       'rg --column --line-number --no-heading --color=always '.shellescape(<q-args>), 0,
        \       {'options': '--no-hscroll --delimiter : --nth 4..'},
        \       <bang>0)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbeier picture lbeier  路  3Comments

olethanh picture olethanh  路  3Comments

jan-warchol picture jan-warchol  路  3Comments

fenuks picture fenuks  路  3Comments

jberglinds picture jberglinds  路  3Comments