Skip to content

Add fzf-complete-path-relative#628

Open
netoctone wants to merge 1 commit intojunegunn:masterfrom
netoctone:master
Open

Add fzf-complete-path-relative#628
netoctone wants to merge 1 commit intojunegunn:masterfrom
netoctone:master

Conversation

@netoctone
Copy link
Copy Markdown

Hello @junegunn

I've implemented a solution for a feature discussed in #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,

@rod-stuchi
Copy link
Copy Markdown

High expectation about this PR

@kibs
Copy link
Copy Markdown

kibs commented Apr 12, 2019

This works really nice!

@utrumo
Copy link
Copy Markdown

utrumo commented Apr 18, 2019

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

@oyren
Copy link
Copy Markdown

oyren commented Apr 18, 2019

+1

@chmanie
Copy link
Copy Markdown

chmanie commented Apr 26, 2019

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.

@ybbond
Copy link
Copy Markdown

ybbond commented May 8, 2019

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

@chmanie
Copy link
Copy Markdown

chmanie commented May 9, 2019

@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

@ybbond
Copy link
Copy Markdown

ybbond commented May 9, 2019

@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!

@DavisOwen
Copy link
Copy Markdown

@junegunn are there any plans to merge this?

@pvonmoradi
Copy link
Copy Markdown

@chmanie This hasn't merged yet, right?

@dragonofdeath
Copy link
Copy Markdown

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

@cideM
Copy link
Copy Markdown

cideM commented Jan 24, 2021

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"))

@zorgick
Copy link
Copy Markdown

zorgick commented Sep 15, 2021

@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?

@rod-stuchi
Copy link
Copy Markdown

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

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

refractalize added a commit to refractalize/dotfiles that referenced this pull request Apr 8, 2022
@ejhusom
Copy link
Copy Markdown

ejhusom commented Jul 13, 2022

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" ;)

@awelormro
Copy link
Copy Markdown

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
'''

@thoughtsunificator
Copy link
Copy Markdown

thoughtsunificator commented Jan 9, 2026

Tested the PR and it fails when you try to complete an existing path that begins with "./" for example, instead of completing the path relatively to the opened file, it resolves the path and you get a path that is relative to the project.

import { Foo } from "./f" // press c-x c-f with cursor positionned after f letter.

In contrast, the following works as described:

import { Foo } from "f" // press c-x c-f with cursor positionned after f letter.

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

Successfully merging this pull request may close these issues.