Skip to content

πŸ”πŸŸ Fish plugin for previewing files

License

Notifications You must be signed in to change notification settings

kidonng/preview.fish

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”πŸŸ Preview.fish

Fish plugin for previewing files.

Installation

  • plug.fish

    plug install kidonng/preview.fish
  • Fisher

    fisher install kidonng/preview.fish

Dependencies

Only file(1) is mandatory required, the following programs provides previews for more file types:

  • bat for texts
  • glow for *.md (Markdown)
  • p7zip for archives
  • timg for images and videos
  • macOS builtin tools plutil and qlmanage for documents and some other stuff

Usage

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.

Viewers

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:

  1. _preview_ext_<extension> for matching file extensions.

    E.g. _preview_ext_md matches .md files.

  2. _preview_type_<type> for matching file -b (file type description) outputs, replacing all spaces with underscore (_).

    E.g. _preview_type_Apple_binary_property_list matches Apple binary property list.

  3. _preview_mime_<mime> for matching file --brief --mime-type (MIME) outputs, replacing all slashes (/) with underscore (_).

    E.g. _preview_mime_application_json matches application/json. You can also only specify the part before the slash to match a general category, e.g. _preview_mime_text matches text/*.

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)

Customization

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

About

πŸ”πŸŸ Fish plugin for previewing files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages