Fzf: Question: How to bind fzf#run using zshrc-defined function

Created on 5 Feb 2018  路  1Comment  路  Source: junegunn/fzf

  • Category

    • [x] Key bindings

    • [x] Neovim

  • OS

    • [x] Mac OS X

  • Shell

    • [x] zsh

Objective:
I wanted a way to open a project from within VIM in a new tab using the fzf#run feature of fzf - and I found what I think will be a great solution- namely:

" ~/.vimrc
nnoremap <silent><leader>p :call fzf#run({ 'source': 'listprojects', 'sink': 'tabedit'})<CR>
# ~/.zshrc
# list all directories that contain a `package.json`
listprojects() {
  find /Users/username/. -maxdepth 2 -name package.json -print0 | xargs -0 -n1 dirname | sort --unique
}
  • Issue:
  • When running !listprojects in vim I get what I expect to see.
  • When running !listprojects | fzf in vim I get no results but fzf loads
  • When hitting <leader><p> I get no results but fzf loads

  • Question:
    How should I modify my source parameter (or anything else for that matter) to achieve this?

Bonus Info

  • I had to pull listprojects into a function within .zshrc because I got all kinds of problems when I tried including the raw command, pipes and all, in the source parameter's string value.
question

Most helpful comment

call fzf#run({'source': 'ls | cat'}) works as expected. The reason you had trouble with |s is that with map commands, Vim interprets a bar character as a command separator (like ; in most other languages), so you have to write them as <bar>, which is going to be quite awkward. It's much simpler if you just define a custom function with plain |s and call it from the mapping.

function! s:foo()
  call fzf#run(fzf#wrap({'source': 'ls | cat'}))
endfunction

nnoremap <silent><leader>p :call <sid>foo()<cr>

For a shell function to be available in Vim, you'll have to properly export it.

>All comments

call fzf#run({'source': 'ls | cat'}) works as expected. The reason you had trouble with |s is that with map commands, Vim interprets a bar character as a command separator (like ; in most other languages), so you have to write them as <bar>, which is going to be quite awkward. It's much simpler if you just define a custom function with plain |s and call it from the mapping.

function! s:foo()
  call fzf#run(fzf#wrap({'source': 'ls | cat'}))
endfunction

nnoremap <silent><leader>p :call <sid>foo()<cr>

For a shell function to be available in Vim, you'll have to properly export it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisamow picture chrisamow  路  3Comments

asilvadesigns picture asilvadesigns  路  3Comments

ghost picture ghost  路  3Comments

leonklingele picture leonklingele  路  3Comments

skamsie picture skamsie  路  3Comments