1

I would like to find the git root directory and use it as argument for :FZF in a key mapping like this:

nnoremap <Leader>eg :FZF `git rev-parse --show-toplevel`<CR>

Unfortunately, this does not work. If I understand it right, backtick expansion works only when vim expects a filename. I think :FZF expects a folder name but maybe vim doesn't know this.

How can this be done?

3
  • 3
    See :help system() and consider experimenting with github.com/junegunn/fzf.vim#advanced-customization.
    – romainl
    Mar 20 at 19:37
  • Thank you. I'm coming closer. At least nnoremap <Leader>eg :echo system(['git', 'rev-parse', '--show-toplevel'])<CR> works as expected.
    – MaxGyver
    Mar 20 at 20:07
  • Feel free to publish an answer to your own question. You can also accept it two days after posting to help other with the same problematic. Mar 21 at 9:35

1 Answer 1

1

You can use the system() function to run an external command and take its output (as you realized, per your own comments.)

Furthermore, you may also need trim() around it, to strip the whitespace (more specifically, the trailing newline at the end of the output from the command.)

You can then use :execute to assemble a call to the :FZF command, passing the git worktree root as an argument.

Putting it all together:

nnoremap <Leader>eg :execute 'FZF' trim(system('git rev-parse --show-toplevel'))<CR>
1
  • 1
    Then I was already close to it but the trim was missing and the error message didn't help me understanding this. Thanks a lot!
    – MaxGyver
    Mar 21 at 16:41

Your Answer

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

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