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

How to enable code preview #184

Closed
3 of 9 tasks
axot opened this issue Aug 4, 2016 · 18 comments
Closed
3 of 9 tasks

How to enable code preview #184

axot opened this issue Aug 4, 2016 · 18 comments

Comments

@axot
Copy link

axot commented Aug 4, 2016

  • Category
    • Question
    • Bug
    • Suggestion
  • OS
    • Linux
    • Mac OS X
    • Windows
    • Etc.
  • Vim
    • Vim
    • Neovim

Code preview works with Files command, and how to enable code preview for other commands, eg: Ag_raw

let g:fzf_files_options =
  \ '--preview "(coderay {} || cat {}) 2> /dev/null | head -'.&lines.'"'
@axot axot changed the title How to enable Code preview How to enable code preview Aug 4, 2016
@junegunn
Copy link
Owner

junegunn commented Aug 10, 2016

It's a little more complicated as we have to extract the file name and the line number from the output. This can be a start:

command! -bang -nargs=* AgRaw call fzf#vim#ag_raw(<q-args>, fzf#wrap('ag-raw',
\ {'options': "--preview 'coderay $(cut -d: -f1 <<< {}) | sed -n $(cut -d: -f2 <<< {}),\\$p | head -".&lines."'"}))

@axot
Copy link
Author

axot commented Sep 10, 2016

Thank you for giving a hint, I am using this now, works great!

autocmd! VimEnter * command! -nargs=* -complete=file Ag :call fzf#vim#ag_raw(<q-args>, fzf#wrap('ag-raw',
\ {'options': "--preview 'coderay $(cut -d: -f1 <<< {}) 2> /dev/null | sed -n $(cut -d: -f2 <<< {}),\\$p | head -".&lines."'"}))

@axot axot closed this as completed Sep 10, 2016
@termosa
Copy link

termosa commented Feb 6, 2017

Preview works, but files are not opening. nvim receive the piece of code instead of file name.
Did you have this problem? Do you know how can it be solved?

@termosa
Copy link

termosa commented Feb 6, 2017

@junegunn I've spent a night by trying to do the same for GitFiles (I'm not familiar with vimscript, at least a day ago). But the only option I found is to directly edit FZF plugin.
Is it possible to pass options to GitFiles from the .vimrc ? It would be nice to have a preview for all file-type searches (like Locate).

@junegunn
Copy link
Owner

junegunn commented Feb 6, 2017

@termosa Something like this?

call fzf#vim#gitfiles('', fzf#vim#with_preview('right'))

@termosa
Copy link

termosa commented Feb 6, 2017

@junegunn thank you for the support! Have a nice day!

@guilala
Copy link

guilala commented Jun 8, 2018

I am trying to get preview on marks: call fzf#vim#marks('', fzf#vim#with_preview('right'))
But like this, the preview function doesn't parse the line, column and url correctly resulting in 'File not found'. How can i make it work?

@CaioBianchi
Copy link

The above solution is no longer working. Thoughts?

@dylan-chong
Copy link
Contributor

@junegunn Just wondering, the preview window is really really useful, but the preview window is only enabled for git by default, the normal :Files is does not show the preview window and unless I instead call call fzf#vim#files('', fzf#vim#with_preview('right'))

@lengerrong
Copy link

lengerrong commented May 10, 2019

Use cat for preview is just too plain text, no highlight
let g:fzf_files_options =
\ '--preview "(coderay {} || cat {}) 2> /dev/null | head -'.&lines.'"'

We can use less and source-highlight to enhance the preview result on ubuntu.

#install source-highlight
sudo apt-get install source-highlight
#configure less with source-highlight
vim ~/.bashrc
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=" -R "
source ~/.bashrc
#use less to preview
let g:fzf_files_options =
\ '--preview "(coderay {} || less {}) 2> /dev/null | head -'.&lines.'"'

@devenv
Copy link

devenv commented Jul 9, 2019

So, is there a way to enable preview on fzf#vim#marks()?

@junegunn
Copy link
Owner

junegunn commented Jul 10, 2019

If you can come up with a command/script that generates preview output for each line of :marks, you can pass it to the function like so:

call fzf#vim#marks({'options': ['--preview', 'echo line = {}']})

@raducucu
Copy link

Was looking for preview on Marks, thanks for the hint.
I use '--preview', 'cat -n {-1} | egrep --color=always -C 10 ^[[:space:]]*{2}[[:space:]]', does exactly what I need.

@Glavnokoman
Copy link

Preview works, but files are not opening. nvim receive the piece of code instead of file name.
Did you have this problem? Do you know how can it be solved?

have the same issue with nvim. Did you find the solution?

@raducucu
Copy link

Preview works, but files are not opening. nvim receive the piece of code instead of file name.
Did you have this problem? Do you know how can it be solved?

have the same issue with nvim. Did you find the solution?

If current buffer is open, nvim return the line instead of file name. I use something like this:
(test -f {-1} && echo {-1} || echo ' . expand('%.p') . ') | xargs -I{} '.s:bin['preview'].' {}:{2}

@gennaro-tedesco
Copy link

gennaro-tedesco commented Jan 2, 2021

call fzf#vim#gitfiles('', fzf#vim#with_preview('right'))

Strangely enough, this does not work for the GFiles? version, namely the git diff is not shown (the preview buffer is empty instead). Same behaviour happens with any custom configuration being passed to GFiles, the ? version stops producing diff preview.

Screenshot

The following line

command! -bang -nargs=? -complete=dir GFiles call fzf#vim#gitfiles('?', fzf#vim#with_preview('down'), <bang>0)

yields
Screenshot 2021-01-03 at 15 32 28
whereas the preview works just fine if the custom option is not included.

@fedoranvar
Copy link

fedoranvar commented Feb 5, 2021

Here is preview for marks. (Took from https://github.com/chengzeyi/fzf-preview.vim)

function! s:fzf_preview_p(bang, ...) abort
    let preview_args = get(g:, 'fzf_preview_window', ['up:50%', 'ctrl-/'])
    if empty(preview_args)
        return { 'options': ['--preview-window', 'hidden'] }
    endif

    " For backward-compatiblity
    if type(preview_args) == type('')
        let preview_args = [preview_args]
    endif
    return call('fzf#vim#with_preview', extend(copy(a:000), preview_args))
endfunction

  command! -bar -bang MP
        \ call fzf#vim#marks(
        \     s:fzf_preview_p(<bang>0, {'placeholder': '$([ -r $(echo {4} | sed "s#^~#$HOME#") ] && echo {4} || echo ' . fzf#shellescape(expand('%')) . '):{2}',
        \               'options': '--preview-window +{2}-/2'}),
        \     <bang>0)

@ChHaeni
Copy link

ChHaeni commented Apr 6, 2022

Here is preview for marks. (Took from https://github.com/chengzeyi/fzf-preview.vim)

Thx!

Changing the command as below doesn't fail to display marks in the current file on an empty line:

  command! -bar -bang MP
        \ call fzf#vim#marks(
        \     s:fzf_preview_p(<bang>0, {'placeholder': '$([ -r $(echo {4} | sed "s#^~#$HOME#") ]  && [ -n {4} ] || echo ' . fzf#shellescape(expand('%')) . '):{2}',
        \               'options': '--preview-window +{2}-/2'}),
        \     <bang>0)

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

No branches or pull requests