Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nvim function to README docs (Issue #64) #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,51 @@ the default is `/usr/local`.

fzy is a drop in replacement for [selecta](https://github.com/garybernhardt/selecta), and can be used with its [usage examples](https://github.com/garybernhardt/selecta#usage-examples).

### Use with Vim
### Use with Vim/Neovim

fzy can be easily integrated with vim.
fzy can be easily integrated with vim/neovim.

``` vim
function! FzyCommand(choice_command, vim_command)
try
let output = system(a:choice_command . " | fzy ")
catch /Vim:Interrupt/
" Swallow errors from ^C, allow redraw! below
endtry
redraw!
if v:shell_error == 0 && !empty(output)
exec a:vim_command . ' ' . output
endif
endfunction
if has('nvim')
function! FzyCommand(choice_command, vim_command) abort
let l:callback = {
\ 'window_id': win_getid(),
\ 'filename': tempname(),
\ 'vim_command': a:vim_command
\ }

function! l:callback.on_exit(job_id, data, event) abort
bdelete!
call win_gotoid(self.window_id)
if filereadable(self.filename)
try
let l:selected_filename = readfile(self.filename)[0]
exec self.vim_command . l:selected_filename
catch /E684/
endtry
endif
call delete(self.filename)
endfunction

botright 10 new
let l:term_command = a:choice_command . ' | fzy > ' . l:callback.filename
silent call termopen(l:term_command, l:callback)
setlocal nonumber norelativenumber
startinsert
endfunction
else
function! FzyCommand(choice_command, vim_command)
try
let output = system(a:choice_command . " | fzy ")
catch /Vim:Interrupt/
" Swallow errors from ^C, allow redraw! below
endtry
redraw!
if v:shell_error == 0 && !empty(output)
exec a:vim_command . ' ' . output
endif
endfunction
endif

nnoremap <leader>e :call FzyCommand("find . -type f", ":e")<cr>
nnoremap <leader>v :call FzyCommand("find . -type f", ":vs")<cr>
Expand Down