fzf.vim
fzf.vim copied to clipboard
Add fzf-complete-path-relative
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,
High expectation about this PR
This works really nice!
I am also looking forward to the request. This function is needed!
+1
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.
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 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 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!
@junegunn are there any plans to merge this?
@chmanie This hasn't merged yet, right?
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
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 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?
@cideM this trick was amazing, well done Sir 👏👏.
@junegunn maybe worth it a mention on the wiki vim examples ?
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" ;)
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 '''