From feb0e563c1fd5e415e83f0523527a5f5e5acbea7 Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Thu, 6 May 2021 23:22:31 -0600 Subject: [PATCH] [Search files] allow customizing the file preview command --- README.md | 14 ++++++++++++-- functions/__fzf_preview_file.fish | 9 +++++++-- tests/preview_file/custom_file_preview.fish | 6 ++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tests/preview_file/custom_file_preview.fish diff --git a/README.md b/README.md index ad2b7075..ab8e4879 100644 --- a/README.md +++ b/README.md @@ -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 directory][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. As with `fzf_preview_file_cmd` ```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 diff --git a/functions/__fzf_preview_file.fish b/functions/__fzf_preview_file.fish index 3b821573..9a83c49c 100644 --- a/functions/__fzf_preview_file.fish +++ b/functions/__fzf_preview_file.fish @@ -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 .. diff --git a/tests/preview_file/custom_file_preview.fish b/tests/preview_file/custom_file_preview.fish new file mode 100644 index 00000000..39ff0ebe --- /dev/null +++ b/tests/preview_file/custom_file_preview.fish @@ -0,0 +1,6 @@ +set multi_word_dir "tests/_resources/multi word dir" + +set fzf_preview_file_cmd cat + +set actual (__fzf_preview_file "$multi_word_dir/file 1.txt") +@test "correctly uses custom command to preview files" "$actual" = "This is file 1"