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

Fzf with arg or with word under cursor #50

Closed
danihodovic opened this issue Dec 2, 2015 · 14 comments
Closed

Fzf with arg or with word under cursor #50

danihodovic opened this issue Dec 2, 2015 · 14 comments
Labels

Comments

@danihodovic
Copy link
Contributor

Is it possible to call fzf or fzf#tags with the word under the cursor or pass an argument to fzf.vim so that it expands fzf with the argument? If not, how difficult would this be to implement?

@danihodovic danihodovic changed the title Fzf word under cursor Fzf with arg or with word under cursor Dec 2, 2015
@danihodovic
Copy link
Contributor Author

Seems like the plugin is pretty extensible. I got it to work with

fu! FzfTagsCurrWord()
  let currWord = expand('<cword>')
  if len(currWord) > 0
    call fzf#vim#tags({'options': '-q ' . currWord})
  else
    execute ':Tags'
  endif
endfu

@junegunn
Copy link
Owner

junegunn commented Dec 3, 2015

Nice, you can further simplify the code like follows, no need for if-else branch:

call fzf#vim#tags({'options': '-q '.shellescape(expand('<cword>'))})

Note that :Tags command passes layout option as well.

call fzf#vim#tags({'options': '-q '.shellescape(expand('<cword>')), 'down': '~40%'})

Is there anything else you want to know?

@danihodovic
Copy link
Contributor Author

Cool!

Is it possible to do this with :Ag too? Provide the word under cursor or a visual selection?

@junegunn
Copy link
Owner

junegunn commented Dec 3, 2015

This is what I have in my .vimrc.

nnoremap <silent> <Leader>ag :Ag <C-R><C-W><CR>

Looks like we can now close the issue.

@junegunn junegunn closed this as completed Dec 3, 2015
@danihodovic
Copy link
Contributor Author

👍

@cj
Copy link

cj commented Sep 22, 2016

@junegunn when I try your solution in nvim I get this error

@junegunn
Copy link
Owner

@cj Right, the function signature was changed in b9285c8

So you can now simply do this:

call fzf#vim#tags(expand('<cword>'))

@cj
Copy link

cj commented Sep 22, 2016

@junegunn ah, there we go. thank you!

@ghost
Copy link

ghost commented Oct 28, 2017

@junegunn : it's beautiful.

@xarthna
Copy link

xarthna commented Aug 10, 2020

Taking cues from this thread, I mapped over <c-]> to act like :tjump to jump to the tag if a single match or open it with FZF otherwise:

function! FzfTagsCurrentWord()
  let l:word = expand('<cword>')
  let l:list = taglist(l:word)
  if len(l:list) == 1
    execute ':tag ' . l:word
  else
    call fzf#vim#tags(l:word)
  endif
endfunction

noremap <c-]> :call FzfTagsCurrentWord()<cr>

bhalash added a commit to bhalash/dotfiles that referenced this issue Nov 17, 2021
@shaypal5
Copy link

Just adding for those preferring to search the current word in :Ag:

" map <Leader>+F to search in ag the word under the cursor
map <Leader>F :call fzf#vim#ag(expand('<cword>'))<kEnter>

@martinklepsch
Copy link

martinklepsch commented Jun 30, 2022

This is pretty awesome! Is there a way to have it show the preview like :Ag does as well?

EDIT: I found that the following does show a preview :)

nnoremap <silent><Leader>r :Rg <C-R><C-W><CR>

@shaypal5
Copy link

shaypal5 commented Jul 1, 2022

Awesome! I stole your version! Why Rg and not Ag, though?

Edit: Oh, to use ripgrep instead of The Silver Searcher, right?

@nornagon
Copy link

nornagon commented Dec 4, 2023

Here's what I ended up with to ripgrep for the token under the cursor (i.e. the whole word, not as a substring of another token):

nnoremap <silent>+ :call fzf#vim#grep(
  \ "rg --column --line-number --no-heading --color=always --smart-case -- "
  \ . fzf#shellescape('\b'.expand('<cword>').'\b'),
  \ fzf#vim#with_preview(), 0)<cr>

(it's too bad there's no fzf#vim#rg like there is fzf#vim#ag, that would have made this snippet a lot shorter! As it is, this is copied from the definition of :Rg, as below)

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

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

No branches or pull requests

7 participants