junegunn / fzf.vim

fzf :heart: vim
MIT License
9.62k stars 584 forks source link

preview.sh not previewing files with nested path on Windows #1513

Closed zuqini closed 4 months ago

zuqini commented 10 months ago

Info

Problem / Steps to reproduce

Preview window is not showing file content for nested paths that include backslashes (e.g. after\ftplugin\lua.vim as opposed to lua.vim. image vs. image

It looks like it's caused by bash not interpreting the unescaped \ from the path returned by fzf.vim\bin\preview.sh.

Reproduce directly with fzf by:

  1. running fzf --preview='bash {...}\.vim\plugged\fzf.vim\bin\preview.sh {}' --bind shift-up:preview-page-up,shift-down:preview-page-down
  2. select a file with a nested path

To further debug the problem, I logged the command returned by preview.sh and ran it in bash directly and saw that the backslash was swallowed:

> bash
 $ highlight -O ansi -l after\ftplugin\lua.vim || coderay after\ftplugin\lua.vim || rougify after\ftplugin\lua.vim || cat after\ftplugin\lua.vim
bash: highlight: command not found
bash: coderay: command not found
bash: rougify: command not found
cat: afterftpluginlua.vim: No such file or directory

If I escape the path, then it previews the file content successfully:

> bash
$ cat after//ftplugin//lua.vim
set expandtab
set shiftwidth=2
set softtabstop=2
set tabstop=2

Environment

This is happening on a new Windows 11 installation:

OS Name Microsoft Windows 11 Pro
Version 10.0.22621 Build 22621

The vim version shouldn't matter since I reproduced the issue directly with preview.sh, but here it is anyway in case it helps:

NVIM v0.9.4
Build type: RelWithDebInfo
LuaJIT 2.1.1696883897

The bash.exe points to the git bash installed from scoop: C:\Users\{username}\scoop\apps\git\current\bin\bash.exe

I should note that this did not happen prior to my Windows 11 re-install. I have no idea what changed that suddenly caused this issue. Appreciate any pointers!

junegunn commented 10 months ago

Do you experience the same problem with the following command?

:call fzf#run(fzf#wrap({'options': ['--preview', 'cat {}']}))
zuqini commented 10 months ago

Nope, that works!

DMinsky commented 6 months ago

Same here. I could confirm that following works with vim in bash terminal. :call fzf#run(fzf#wrap({'options': ['--preview', 'cat {}']}))

But in gvim cat is unavailable, so still no preview there.

junegunn commented 6 months ago

I don't have a Windows PC to debug and test this on. Pull requests are welcome.

DMinsky commented 6 months ago

It seems I've found a workaround. Although it's not even a workaround, but rather an improvement of the default preview situation, and it's mentioned right in the README of fzf.vim 🫠.

You need to install bat. A tool that, as far as I understand, replaces cat and adds syntax highlighting in the preview window. I just unpacked the archive with the bat binary and added this path to my path. After that, the preview window began to display the contents of the selected files.

Screenshot 2024-04-04 140544

mbilyanov commented 5 months ago

How did you get the syntax highlighting to work in the preview window?

DMinsky commented 5 months ago

How did you get the syntax highlighting to work in the preview window?

I literally did nothing except install bat. Just make sure the folder with the bat executable is available in the PATH so that fzf.vim can find it.

junegunn commented 4 months ago

Fixed in d91dd754a933e3da9797253f9b6d268bb85d0f4d.

And yeah, having bat would avoid the bug.