Fish plugin for previewing files.
Only file(1)
is mandatory required, the following programs provides previews for more file types:
bat
for textsglow
for*.md
(Markdown)p7zip
for archivestimg
for images and videos- macOS builtin tools
plutil
andqlmanage
for documents and some other stuff
The main function is preview
:
$ preview shell.sh
1 echo Hello world!
2 echo This is shell file example
An awesome use case is to combine the plugin with fzf.fish:
set -U fzf_preview_file_cmd preview
If you don't have fzf.fish, definitely check it out, it is one of the must-have Fish plugins and contributed by me.
Previews are generated via viewers, which is a function named _preview_viewer_<program name>
. The above example is using _preview_viewer_bat
.
To decide which viewer to use, the plugin check for a corresponding function, in the following order:
-
_preview_ext_<extension>
for matching file extensions.E.g.
_preview_ext_md
matches.md
files. -
_preview_type_<type>
for matchingfile -b
(file type description) outputs, replacing all spaces with underscore (_
).E.g.
_preview_type_Apple_binary_property_list
matchesApple binary property list
. -
_preview_mime_<mime>
for matchingfile --brief --mime-type
(MIME) outputs, replacing all slashes (/
) with underscore (_
).E.g.
_preview_mime_application_json
matchesapplication/json
. You can also only specify the part before the slash to match a general category, e.g._preview_mime_text
matchestext/*
.
View the source code for available viewers and matching functions.
If no viewer is available for the previewed file, the output of file -b
(file type description) and file --brief --mime-type
(MIME) will be displayed instead:
$ preview /bin/bash
Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>] [arm64e (caps: 0x02):Mach-O 64-bit arm64e (caps: PAC00) executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>]
(application/x-mach-binary)
You can override existing viewers with a function of the same name under ~/.config/fish/functions
:
# Enable the plain view (`-p`) in `bat`
# You can put it under `~/.config/fish/functions/_preview_viewer_bat.fish`
function _preview_viewer_bat
bat -p --color always $argv
end
Or adding support for a file extension:
# Preview `*.dll` files with `7z`
function _preview_ext_dll
7z l $argv
end