Skip to content

Commit

Permalink
[Search directory] Allow customizing the file preview command (#156)
Browse files Browse the repository at this point in the history
To allow users to implement custom file previews (great ideas include previewing images or showing hexdumps), the command used to preview files is now configurable. To change it, set the variable fzf_preview_file_cmd.
  • Loading branch information
neersighted authored May 7, 2021
1 parent a0bbc2f commit 3285c08
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,25 @@ They are always appended last to fzf's argument list. Because fzf uses the optio
- [re-populate fzf's input list on demand](https://github.com/junegunn/fzf/issues/1750)
- change the search mode

### Change the command used to preview folders
### Change the command used to preview files

The search directory feature, by default, uses `ls` to preview the contents of a directory. To integrate with the variety of `ls` replacements available, the command used to preview directories is configurable through the `fzf_preview_dir_cmd` variable. For example, in your `config.fish`, you may put:
The search directory feature, by default, uses `bat` to preview the contents of files. `bat` is a well-adopted `cat` replacement with syntax highlighting, line numbers, and more. If you would like to change the preview tool (to `cat` to avoid installing a new dependency, or to add custom logic such as binary or image preview), you may set the `fzf_preview_file_cmd` variable. For example, in your `config.fish`, you may put:

```fish
set fzf_preview_file_cmd cat
```

Do not specify a target path in the command, as `fzf.fish` will [prepend the file][custom preview command] to preview to the command itself.

### Change the command used to preview directories

The search directory feature, by default, uses `ls` to preview the contents of directories. To integrate with the variety of `ls` replacements available (e.g. exa, lsd, tree), the command used to preview directories is configurable through the `fzf_preview_dir_cmd` variable. Set `fzf_preview_dir_cmd` in your `config.fish`:

```fish
set fzf_preview_dir_cmd exa --all --color=always
```

Do not specify a target path in the command, as `fzf.fish` will [prepend the directory][custom preview command] to preview to the command itself.
As above, do not specify a target path in the command, as `fzf.fish` will [prepend the directory][custom preview command] to preview to the command itself.

### Change the files searched

Expand Down
9 changes: 7 additions & 2 deletions functions/__fzf_preview_file.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ function __fzf_preview_file --description "Print a preview for the given file ba
set file_path $argv

if test -f "$file_path" # regular file
bat --style=numbers --color=always "$file_path"
if set --query fzf_preview_file_cmd
# need to escape quotes to make sure eval receives file_path as a single arg
eval $fzf_preview_file_cmd \"$file_path\"
else
bat --style=numbers --color=always "$file_path"
end
else if test -d "$file_path" # directory
if set --query fzf_preview_dir_cmd
# need to escape quotes to make sure eval receives file_path as a single arg
# see above
eval $fzf_preview_dir_cmd \"$file_path\"
else
# -A list hidden files as well, except for . and ..
Expand Down
6 changes: 6 additions & 0 deletions tests/preview_file/custom_file_preview.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set multi_word_dir "tests/_resources/multi word dir"

set fzf_preview_file_cmd rev

set actual (__fzf_preview_file "$multi_word_dir/file 1.txt")
@test "correctly uses custom command to preview files" "$actual" = "1 elif si sihT"

0 comments on commit 3285c08

Please sign in to comment.