Skip to content

Commit

Permalink
[Search files] allow customizing the file preview command
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed May 7, 2021
1 parent a0bbc2f commit 08d250b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
14 changes: 12 additions & 2 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 files

The search directory feature, by default, uses `bat` to preview the contents of a file. `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` for availability reasons, or to add custom logic like 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 folders

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 `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. Set `fzf_preview_dir_cmd` in your `config.fish` similar to the above.

```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 08d250b

Please sign in to comment.