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

Allow pasting from buffer with <c-r> #672

Closed
1 of 3 tasks
dylan-chong opened this issue Jul 15, 2018 · 13 comments
Closed
1 of 3 tasks

Allow pasting from buffer with <c-r> #672

dylan-chong opened this issue Jul 15, 2018 · 13 comments

Comments

@dylan-chong
Copy link
Contributor

  • Category
    • Question
    • Bug
    • Suggestion

It would be very useful to be able to use <c-r>xto paste from buffer x into the search prompt. This will be useful when:

  • You copy some text to a buffer and want to paste it into the search
  • you want to search for the word under the cursor with <c-r><c-w>
  • you want to search for the current file name with <c-r>%
@junegunn
Copy link
Owner

That would be nice, but it's not possible since fzf is an external program rather than a native Vim plugin so it doesn't have access to Vim registers.

@junegunn
Copy link
Owner

If you use Neovim or use plain Vim but set up to use the embedded terminal emulator (e.g. let g:fzf_layout = { 'window': 'botright 10split' }), you might be able to implement the feature by defining a set of terminal mappings (tnoremap).

@boris-petrov
Copy link

@junegunn, @dylan-chong - I migrated from CtrlP to fzf and this here is my biggest pain - I am used to pasting some search text in the prompt with Shift-Insert. Is there any way to mimic this in fzf? I'm using gVim 8.1 on Linux.

@halilim
Copy link

halilim commented Aug 19, 2019

I hope my suggestion helps at least one of the use cases. fzf supports a -q parameter:

       -q, --query=STR
              Start the finder with the given query

This is what I have in my vimrc, leader-f for FZF and leader-F for FZF pre-filled with the word under cursor:

nnoremap <Leader>f :FZF<CR>
nnoremap <Leader>F :FZF -q <C-R><C-W><CR>

@daliusd
Copy link

daliusd commented Aug 29, 2019

@junegunn neovim has access to system clipboard with set clipboard+=unnamedplus and I believe many neovim users use it. Can fzf access system clipboard? That would allow solving this problem.

@daliusd
Copy link

daliusd commented Aug 30, 2019

Adding xsel -b output to search query is all I need but I don't find a way to add binding for this (just did RTFM).

@tuxflo
Copy link

tuxflo commented Nov 11, 2019

For neovim users:
With the help of this great vimcast episode I'm able to map Alt+R to paste a buffer into a terminal session like this:

if has('nvim')
  tnoremap <expr> <M-r> '<C-\><C-N>"'.nr2char(getchar()).'pi'
endif

Now you can press Alt+ followed by a register name to paste the content of this register to any terminal buffer. Since fzf's :Rg command is executed in a terminal buffer you can create a mapping like

vmap <c-f> "hy:Rg<Enter><M-r>h

wich will map Ctrl+F to yank the current highlighted selection to register h then call the :Rg command, press Alt+M, press h.

The good thing of this solution (in opposite of the solution using -q from above) is, that one is still able to interact with the entered text, for example if there was trailing space in the visual selection.

@daliusd
Copy link

daliusd commented Nov 12, 2019

@tuxflo, Thanks :)

For those who don't use registers and wants to use ctrl-v:

tnoremap <expr> <C-v> '<C-\><C-N>pi'

@callumacrae
Copy link

I couldn't get any of the above working with neovim v0.4.3, but got it working using :execute and getreg:

vnoremap <Leader>F "py:execute ":FZF -q " . getreg("p")<CR>

Above yanks the currently selected text to the p register, and then opens FZF searching for that text.

@boris-petrov
Copy link

This has stopped working for me recently.

tnoremap <S-INSERT> <C-w>"+

This still works in terminal Vim but in gVim now it only inputs "+. My vim:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Nov 15 2020 16:46:37)
Included patches: 1-1989

Anyone got it working on gVim?

@denvaar
Copy link

denvaar commented Jul 20, 2022

Maybe I'm not understanding this question correctly, but it is in fact possible to yank some text into a register and then paste that into the fzf window.

  1. Yank some text
  2. Open the fzf or rg window.
  3. CTRL-\ + CTRL-n to enter terminal-normal mode. Now you can see that you can move your cursor around just like in normal mode, confined within the window dimensions.
  4. "p" to paste as you normally would.
  5. "i" or "a" ect to enter insert mode, but you will be taken back to terminal mode.

@daliusd
Copy link

daliusd commented Jul 21, 2022

Maybe I'm not understanding this question correctly, but it is in fact possible to yank some text into a register and then paste that into the fzf window.

  1. Yank some text
  2. Open the fzf or rg window.
  3. CTRL-\ + CTRL-n to enter terminal-normal mode. Now you can see that you can move your cursor around just like in normal mode, confined within the window dimensions.
  4. "p" to paste as you normally would.
  5. "i" or "a" ect to enter insert mode, but you will be taken back to terminal mode.

You just proposed 4 keyboard steps solution to people who are looking for ctrl-v. That's how people end up migrating to Telescope.

@junegunn
Copy link
Owner

who are looking for ctrl-v

CTRL-V will naturally paste the content in the system clipboard (set clipboard=unnamed)

In the year 2022, fzf opens in a terminal buffer both in Vim and Neovim, unless you have configured it to open in a tmux popup (e.g. let g:fzf_layout = { 'tmux': '-p90%,60%' }), so you can do something like this:

autocmd! FileType fzf tnoremap <expr> <C-r> getreg(nr2char(getchar()))

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

No branches or pull requests

8 participants