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

Question: new fzf feature reload inside vim #907

Closed
1 of 3 tasks
monkoose opened this issue Nov 16, 2019 · 8 comments
Closed
1 of 3 tasks

Question: new fzf feature reload inside vim #907

monkoose opened this issue Nov 16, 2019 · 8 comments

Comments

@monkoose
Copy link

monkoose commented Nov 16, 2019

  • Category
    • Question
    • Bug
    • Suggestion

Is it possible to use it and execute vim function or command? If yes, can you please explain how to do it. Because my several attempts failed with [Command failed: ...].
Or by now only possible way is to imitate same command as source of fzf#run?
Thank you.

@junegunn
Copy link
Owner

Because my several attempts failed

Please post the code you tried.

@junegunn
Copy link
Owner

junegunn commented Nov 17, 2019

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, options, a:fullscreen)
endfunction

command! -nargs=1 -bang RF call RipgrepFzf(<q-args>, <bang>0)

This works for me. There is one problem though, that the command requires an initial query. This is to avoid starting ripgrep command for an empty query that can run for a very long time producing a lot of lines. And fzf cannot kill the initial command on "change", because it wasn't started by fzf process.

# fzf can kill subsequent-rg-command because it started it, but not initial-rg-command
initial-rg-command | fzf ... --bind 'change:reload:subsequent-rg-command'

A workaround is to set FZF_DEFAULT_COMMAND and start fzf without the standard input command.

# fzf can now kill both initial command and subsequent command
FZF_DEFAULT_COMMAND=initial-rg-command \
  fzf ... --bind 'change:reload:subsequent-rg-command'

However, fzf#vim#grep currently doesn't have an option for this mode of execution, but I can change that.

junegunn added a commit that referenced this issue Nov 17, 2019
Related #907
Also junegunn/fzf#1750

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, options, a:fullscreen)
endfunction

command! -nargs=* -bang RF call RipgrepFzf(<q-args>, <bang>0)
@junegunn
Copy link
Owner

Now this works well with the above commit.

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(options), a:fullscreen)
endfunction

command! -nargs=* -bang RF call RipgrepFzf(<q-args>, <bang>0)

@monkoose
Copy link
Author

Thank you.

@amebrahimi
Copy link

I have an issue about this topic when I try to start :Rg I get this error:
image
and here is my fzf vim settings file:

nnoremap <C-p> :GFiles<CR>
map <C-f> :Files<CR>
map <leader>b :Buffers<CR>
nnoremap <leader>g :Rg<CR>
nnoremap <leader>m :Marks<CR>

let g:fzf_layout = {'up':'~90%', 'window': { 'width': 0.8, 'height': 0.8,'yoffset':0.5,'xoffset': 0.5, 'highlight': 'Todo', 'border': 'sharp'  } }
let $FZF_DEFAULT_OPTS = '--layout=reverse --info=inline'
let $FZF_DEFAULT_COMMAND = 'rg --files --hidden'

let g:fzf_action = {
  \ 'ctrl-t': 'tab split',
  \ 'ctrl-x': 'split',
  \ 'ctrl-v': 'vsplit' }


let g:fzf_colors =
  \ { 'fg':      ['fg', 'Normal'],
  \ 'bg':      ['bg', 'Normal'],
  \ 'hl':      ['fg', 'Comment'],
  \ 'fg+':     ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
  \ 'bg+':     ['bg', 'CursorLine', 'CursorColumn'],
  \ 'hl+':     ['fg', 'Statement'],
  \ 'info':    ['fg', 'PreProc'],
  \ 'border':  ['fg', 'Ignore'],
  \ 'prompt':  ['fg', 'Conditional'],
  \ 'pointer': ['fg', 'Exception'],
  \ 'marker':  ['fg', 'Keyword'],
  \ 'spinner': ['fg', 'Label'],
  \ 'header':  ['fg', 'Comment'] }

let g:fzf_tags_command='ctags -R'

"Get Files
command! -bang -nargs=? -complete=dir Files
            \ call fzf#vim#files(<q-args>, fzf#vim#with_preview({'options': ['--layout=reverse', '--info=inline']}), <bang>0)

" Get text in files with Rg
command! -bang -nargs=* Rg
    \ call fzf#vim#grep(
    \   'rg --column --line-number --no-heading --color=always --smart-case'.shellescape(<q-args>), 1,
    \   fzf#vim#with_preview(), <bang>0)

function! RipgrepFzf(query, fullscreen)
    let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
    let initial_command = printf(command_fmt, shellescape(a:query))
    let reload_command = printf(command_fmt, '{q}')
    let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
    call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(options), a:fullscreen)
endfunction

command! -nargs=* -bang RF call RipgrepFzf(<q-args>, <bang>0)

command! -bang -nargs=* GGrep
            \ call fzf#vim#grep(
            \ 'git grep --line-number '.shellescape(<q-args>), 0,
            \ fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0)

huyvohcmc added a commit to huyvohcmc/dotfiles that referenced this issue Aug 3, 2020
huyvohcmc added a commit to huyvohcmc/dotfiles that referenced this issue Aug 4, 2020
@upgradeQ
Copy link

@amebrahimi I had the same issue , turns out I've not installed rg , maybe it is the case?
Since you on windows try running this command : choco install ripgrep

huyvohcmc added a commit to huyvohcmc/dotfiles that referenced this issue Sep 25, 2020
huyvohcmc added a commit to huyvohcmc/dotfiles that referenced this issue Sep 25, 2020
huyvohcmc added a commit to huyvohcmc/dotfiles that referenced this issue Sep 27, 2020
@tonybeyond
Copy link

I've the same issue as amebrahimi here above even though I've installed all required packages (arch distro).

" This is the default extra key bindings
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-x': 'split',
\ 'ctrl-v': 'vsplit' }

" Enable per-command history.
" CTRL-N and CTRL-P will be automatically bound to next-history and
" previous-history instead of down and up. If you don't like the change,
" explicitly bind the keys to down and up in your $FZF_DEFAULT_OPTS.
let g:fzf_history_dir = '~/.local/share/fzf-history'

map :Files
map b :Buffers
nnoremap g :Rg
nnoremap t :Tags
nnoremap m :Marks

let g:fzf_tags_command = 'ctags -R'
" Border color
let g:fzf_layout = {'up':'~90%', 'window': { 'width': 0.8, 'height': 0.8,'yoffset':0.5,'xoffset': 0.5, 'highlight': 'Todo', 'border': 'sharp' } }

let $FZF_DEFAULT_OPTS = '--layout=reverse --info=inline'
let $FZF_DEFAULT_COMMAND="rg --files --hidden"

" Customize fzf colors to match your color scheme
let g:fzf_colors =
\ { 'fg': ['fg', 'Normal'],
\ 'bg': ['bg', 'Normal'],
\ 'hl': ['fg', 'Comment'],
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
\ 'hl+': ['fg', 'Statement'],
\ 'info': ['fg', 'PreProc'],
\ 'border': ['fg', 'Ignore'],
\ 'prompt': ['fg', 'Conditional'],
\ 'pointer': ['fg', 'Exception'],
\ 'marker': ['fg', 'Keyword'],
\ 'spinner': ['fg', 'Label'],
\ 'header': ['fg', 'Comment'] }

"Get Files
command! -bang -nargs=? -complete=dir Files
\ call fzf#vim#files(, fzf#vim#with_preview({'options': ['--layout=reverse', '--info=inline']}), 0)

" Get text in files with Rg
command! -bang -nargs=* Rg
\ call fzf#vim#grep(
\ 'rg --column --line-number --no-heading --color=always --smart-case '.shellescape(), 1,
\ fzf#vim#with_preview(), 0)

" Ripgrep advanced
"function! RipgrepFzf(query, fullscreen)
" let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
" let initial_command = printf(command_fmt, shellescape(a:query))
" let reload_command = printf(command_fmt, '{q}')
" let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}

@DZ84
Copy link
Contributor

DZ84 commented Nov 2, 2020

I have this "Command failed..." issue shown in the screenshot above. It happens when I start :Rg with a command and it finds no matches (e.g. :Rg ksdfjsdjfskdlfsalkdjf). Start it with something where it will have hits (e.g. :Rg vim) and it's fine.

Would be nice if done more elegantly though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants