Default viewer script

From Vifm Wiki
Revision as of 08:50, 4 October 2014 by Xaizek (talk | contribs) (→‎Default viewer script: Better description part)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script can be used to view files for which no viewer is registered. It has some convenience features like:

  • displaying only beginning of a file, so that huge files are processed at the same speed as the small ones;
  • converting text files to system encoding;
  • displaying binary files as a hex dump.

Source code[edit]


if [ "$#" -ne 1 ]; then
    echo "Usage: $0 filename"
    exit 1

# upper limit of lines to display for text files
# upper limit of bytes to display for binary files
# language of text files
# output encoding for text files

info=$(head -$nlines "$1" | file --mime -)

if [ "x$charset" == "xbinary" ]; then
    hexdump -e '"%08_ax: "' -e '8/1 "%02x " "  " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
    head -$nlines "$1" | enconv -g -L $language -x $encoding

The script uses:

  1. head utility to process only first $nlines of file.
  2. file tool to determine whether file is of binary format or not.
  3. hexdump tool to display binary files in classic hex dump form.
  4. enconv program for automatic conversion to the $encoding charset for text files trying to guess source encoding of $language.

Plugging the script into Vifm[edit]

To use it in Vifm:

  1. Save the script as defviewer at one of directories listed in your $PATH environment variable or under $VIFM/scripts (most likely that it's ~/.vifm/scripts).
  2. On Unix-like operating systems make the script file executable by running
    chmod +x defviewer
  3. Add the following line to your vifmrc:
    fileview * defviewer %c


  • In quick view activated via :view command.
  • In explore mode activated via e normal mode key.

Here is what it looks like for a binary file in explore mode:


Possible improvements[edit]

  • Use mguesser to automatically determine language and encoding of text files.