FZF VIM: removing leading portion of absolute path

594 Views Asked by At

I am using the following to search my project:

let g:fzf_directories = join(
      \ [
      \ "/some/long/path/to/project/dir",
      \ "more paths here..."
      \ ], ' ')

let $FZF_DEFAULT_COMMAND = "rg --files --hidden --smart-case " . $g:fzf_directories

The results look like this:

/some/long/path/to/project/dir/SomeFile.java
/some/long/path/to/project/dir/subdir/SomeOtherFile.java

I would like the results to look like:

SomeFile.java
subdir/SomeOtherFile.java

I know that there is 'options': '--delimiter : --nth 4..' which I think might do what I want but I can't figure out where to put it.

2

There are 2 best solutions below

0
On

I know this question was asked over 6 months ago but I have an answer for you based on the post found at the following link: sort_ripgrep_results_based_on_proximity_to

Just add a '.' right after the --files option like so:

rg --files "."
0
On

Here is my code from my lazy nvim config for fzf plugins:

return {
  { "junegunn/fzf" },
  {
    "junegunn/fzf.vim",
    config = function()
      vim.g.fzf_layout = { window = { width = 0.9, height = 0.6 } }
      vim.g.fzf_buffers_jump = 1
      vim.g.fzf_preview_window = { "right:50%", "ctrl-/" }
      vim.g.fzf_action = {
        ["ctrl-t"] = "tab split",
        ["ctrl-x"] = "split",
        ["ctrl-v"] = "vsplit",
      }
    end,

    keys = {
      {
        "<leader>sP",
        function()
          --vim.fn["fzf#vim#grep"]("rg 'blah' /root/.local/share/nvim/lazy", 1, {'options': '--ansi'})
          vim.api.nvim_command(
            'call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case \\"\\" /root/.local/share/nvim/lazy", { "options": ["--preview",  "/root/.local/share/nvim/lazy/fzf.vim/bin/preview.sh {}", "--delimiter", "\\/", "--with-nth", "7.."  ] } )'

            --'call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case \\"\\" /root/.local/share/nvim/lazy", 1, {"options": "--ansi --preview \\"bat --color=always --style=numbers {}\\" --preview-window=:50%:wrap"}, 0)'
            --'call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case \\"\\" /root/.local/share/nvim/lazy", 1)'
          )
        end,
        desc = "Search Plugin Files",
      },
    },
  },
}

If you are not using lazy nvim, you won't want to copy this exactly. But the important bit is in the keys section where the call is to fzf#vim#grep.

You can see the options are passed. Per docs, these get passed to fzf#wrap. The --delmiter is set to "\/". The first slash escapes the second slash which escapes the third slash. This is needed because it is a regex. But ou will probably only need to do on escape: "/".

Next arg is "--with-nth" which is set to "7..". Change the number according to how many segments of the path you need to eliminate.