fzf.vim icon indicating copy to clipboard operation
fzf.vim copied to clipboard

Add fzf-complete-path-relative

Open netoctone opened this issue 6 years ago • 16 comments

Hello @junegunn

I've implemented a solution for a feature discussed in https://github.com/junegunn/fzf.vim/issues/303

Basic use case is to fuzzyfind a file and insert path to it that is relative to buffer.

It helps a lot with languages that do not have library paths (e.g. JavaScript).

Maybe it can help somebody save some time.

Cheers,

netoctone avatar May 04 '18 11:05 netoctone

High expectation about this PR

rod-stuchi avatar Nov 14 '18 02:11 rod-stuchi

This works really nice!

kibs avatar Apr 12 '19 12:04 kibs

I am also looking forward to the request. This function is needed!

utrumo avatar Apr 18 '19 11:04 utrumo

+1

oyren avatar Apr 18 '19 17:04 oyren

I made an up-to-date fork using this PR and changed the command to fd like so:

inoremap <expr> <c-x><c-f> fzf#vim#complete#path_relative('fd')

And I can say it works really, really well. Please add this in to upstream, that'd make a really useful addition. As far as I can see so far, there's no vim plugins or scripts that can adequately complete relative paths. The combination of fzf, fd and this PR is the best experience of completing paths I've ever had in an editor.

chmanie avatar Apr 26 '19 04:04 chmanie

I made an up-to-date fork using this PR and changed the command to fd like so:

inoremap <expr> <c-x><c-f> fzf#vim#complete#path_relative('fd')

And I can say it works really, really well. Please add this in to upstream, that'd make a really useful addition. As far as I can see so far, there's no vim plugins or scripts that can adequately complete relative paths. The combination of fzf, fd and this PR is the best experience of completing paths I've ever had in an editor.

is there anyway I can implement this by adding your PR's script to my init.vim? I have tried, but my experience in vim script is beyond terrible, I failed

ybbond avatar May 08 '19 10:05 ybbond

@ybbond sure, there's not much to it. Here are the three lines in my .vimrc:

" I use plug
Plug '/usr/local/opt/fzf'
Plug 'chmanie/fzf.vim'

inoremap <expr> <c-x><c-f> fzf#vim#complete#path_relative('fd')

EDIT: Oh sorry, you mean using the upstream fzf.vim? Yeah I tried that as well and failed, too. Maybe it'd be worthwhile to put this into an external plugin that can be included separately. I'm thinking something like fzf-addons-relative-path.vim

chmanie avatar May 09 '19 04:05 chmanie

@chmanie yeah, I meant using junegunn's fzf. Good idea tho, fzf-addons-relative-path.vim sounds neat. but considering your repo is just 1 commit behind junegunn, I am using yours for now!

btw, I noticed your location. Enjoy Indonesia!

ybbond avatar May 09 '19 05:05 ybbond

@junegunn are there any plans to merge this?

DavisOwen avatar Feb 04 '20 19:02 DavisOwen

@chmanie This hasn't merged yet, right?

pvonmoradi avatar Oct 08 '20 11:10 pvonmoradi

Here is a script if you want to use it with current fzf.vim plugin ( I adopted it to js/ts, but the idea stays the same) https://github.com/dragonofdeath/dotfiles/blob/master/nvim/.vim/scripts/fzf_js_import.vim

dragonofdeath avatar Nov 20 '20 21:11 dragonofdeath

I think this also does the trick, no?

  inoremap <expr> <c-x><c-f> fzf#vim#complete("fd <Bar> xargs realpath --relative-to " . expand("%:h"))

cideM avatar Jan 24 '21 22:01 cideM

@cideM could you please explain, what this commands does? It works! However, it bothers me, that I was trying to solve the same problem via fd options, but couldn't find any mention about relative path search. I'm curious about this fragment xargs realpath --relative-to - what is it?

zorgick avatar Sep 15 '21 20:09 zorgick

@cideM this trick was amazing, well done Sir 👏👏.

@junegunn maybe worth it a mention on the wiki vim examples ?

rod-stuchi avatar Jan 03 '22 17:01 rod-stuchi

Here's a slight adjustment to the excellent command of @cideM, in order to deal with filenames that have spaces:

inoremap <expr> <c-x><c-f> fzf#vim#complete("find -print0 <Bar> xargs --null realpath --relative-to " . expand("%:h"))

In the line above I use find instead of fd, and have added -print0 to make sure that find returns a null character at the end of each filename. By passing --null to xargs, we ensure that xargs know that each input item is terminated by a null character.

Here's a version using rg instead (which uses the flag --null instead of -print0):

inoremap <expr> <c-x><c-f> fzf#vim#complete("rg --files --hidden --no-ignore --null <Bar> xargs --null realpath --relative-to " . expand("%:h"))

To answer the question of @zorgick: xargs realpath --relative-to takes the output of fd (or find or rg etc), and uses realpath, a GNU coreutil, to produce the path of each match relative to the current file you have open in Vim. The expand("%:h") tells Vim to provide the filepath of the current file, relative to the directory that you opened it from, in order to let realpath produce the correct "relativeness" ;)

ejhusom avatar Jul 13 '22 08:07 ejhusom

Maybe it's an old thread but I've made this function to insert the filename in the current buffer position

'''vim function! InsertFileName() let selected_file = fzf#run({'source': 'find . -type f'}) if !empty(selected_file) let selected_file_str = join(selected_file, "\n") execute "normal i" . selected_file_str endif endfunction '''

awelormro avatar May 13 '23 01:05 awelormro