2

I have hidden buffers and I'd like to be able to get an import from another file to place into a file. For example, if I were to have this in another buffer:

import javafx.controls.Button;

if I were to type import and then do <c-x> <c-n> or <c-x> <c-l> I'd be able to get that line. But would it be possible for me to type in

Button

and then do a sequence to get matches for things that come before I type in Button? I suppose it's kind of a "reverse omni" or "line" completion.

3
  • This doesn't answer your question, but if you're looking for Java import help, you might be more interested in something like coc.nvim. Commented Mar 16, 2021 at 7:15
  • 1
    @Zoe yes I've used coc in the past, it a bit finicky on my system when used with gradle/maven so I've just avoided it. Being able to do a "reverse" autofill would be pretty cool in other circumstances though, thanks though!
    – joshpetit
    Commented Mar 16, 2021 at 11:29
  • 2
    Welcome to Vi and Vim, by the way!
    – D. Ben Knoble
    Commented Mar 16, 2021 at 16:43

1 Answer 1

3

EDIT Actually junegunn already has everything covered for you. Since you already use fzf you can add this to your vimrc:

imap <c-x><c-l> <plug>(fzf-complete-line)

And now <c-x><c-l> will use fzf to fuzzy match your line against the existing one in your buffers and complete directly in your buffer.

See fzf's readme for the list of available completion functions.


One way to do it would to be leverage the :Lines function provided by fzf. This is probably a bit overkill if you don't already have the plugin in your workflow but if you, do you could add this mapping to your vimrc:

nnoremap <leader>i :execute('Lines ' . expand('<cWORD>'))<CR>

The mapping calls the Lines command with the WORD under your cursor as argument (see :h <cWORD> and :h execute())

Then when you are in normal mode and your cursor is on Button you can use <leader>i (or any key you'll use in your mapping) and this will show this kind of window with the list of the lines in the open buffers matching the word under your cursor.

enter image description here

Fzf provides two related commands which could be useful here:

enter image description here

You could also make something using only built-in vim features but that requires a bit more work. A starting point would probably be to read :h 'grepprg'.

7
  • I actually have fzf in my workflow so this is very useful! I've used lines but now it's nice to enable it to use the word under the cursor. Is it possible to input the line of that text to where my cursor is?
    – joshpetit
    Commented Mar 16, 2021 at 17:30
  • @joshpetit I'm not sure I completely understand, do you mean something like :execute('Lines ' . getline('.'))?
    – statox
    Commented Mar 16, 2021 at 17:40
  • so for example I mean (this may need to be created in a function call I assume) but once you execute the original command with <leader>i and the search result is found with the import, how could we insert the line of text from the found import into the line we currently are on?
    – joshpetit
    Commented Mar 16, 2021 at 17:44
  • 1
    How right I didn't notice that hitting Enter on the window moves to the line instead of inserting it. A first, not ideal, idea would be to hit Escape to toggle normal mode and then yank the line as you'd do in a regular buffer, close the window and past the yanked line. But this is not very convenient. Let me see if I can find a better solution.
    – statox
    Commented Mar 16, 2021 at 17:51
  • Turns out junegunn already thought of everything! See my edit I think it's the best solution with fzf.
    – statox
    Commented Mar 16, 2021 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.