Difference between revisions of "Default viewer script"
Jump to navigation
Jump to search
m (Fix numbering) |
(→Default viewer script: Better description part) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | 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 [https://en.wikipedia.org/wiki/Hex_dump hex dump]. | ||
− | < | + | == Source code == |
+ | |||
+ | <source lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
Line 28: | Line 33: | ||
head -$nlines "$1" | enconv -g -L $language -x $encoding | head -$nlines "$1" | enconv -g -L $language -x $encoding | ||
fi | fi | ||
− | </ | + | </source> |
The script uses: | The script uses: | ||
Line 37: | Line 42: | ||
# [http://linux.die.net/man/1/enconv <code>enconv</code>] program for automatic conversion to the <code>$encoding</code> charset for text files trying to guess source encoding of <code>$language</code>. | # [http://linux.die.net/man/1/enconv <code>enconv</code>] program for automatic conversion to the <code>$encoding</code> charset for text files trying to guess source encoding of <code>$language</code>. | ||
− | == Plugging the script into | + | == Plugging the script into Vifm == |
− | To use it in | + | To use it in Vifm: |
− | # Save the script as <code>defviewer</code> at one of directories listed in your <code>$PATH</code> environment variable or under <code>$VIFM/scripts</code>(most likely that it's <code>~/.vifm/scripts</code>). | + | # Save the script as <code>defviewer</code> at one of directories listed in your <code>[https://en.wikipedia.org/wiki/PATH_(variable) $PATH]</code> environment variable or under <code>$VIFM/scripts</code> (most likely that it's <code>~/.vifm/scripts</code>). |
# On Unix-like operating systems make the script file executable by running<br><code>[https://en.wikipedia.org/wiki/Chmod chmod] +x defviewer</code> | # On Unix-like operating systems make the script file executable by running<br><code>[https://en.wikipedia.org/wiki/Chmod chmod] +x defviewer</code> | ||
− | # Add the following line to your | + | # Add the following line to your <code>vifmrc</code>:<br><code>fileview * defviewer %c</code> |
− | Here is what it looks like: | + | == Usage == |
+ | |||
+ | * In quick view activated via <code>:view</code> command. | ||
+ | * In explore mode activated via <kbd>e</kbd> normal mode key. | ||
+ | |||
+ | Here is what it looks like for a binary file in explore mode: | ||
[[File:hex-dump-screenshot.png]] | [[File:hex-dump-screenshot.png]] | ||
+ | |||
+ | == Possible improvements == | ||
+ | |||
+ | * Use [http://www.mnogosearch.org/guesser/ mguesser] to automatically determine language and encoding of text files. |
Latest revision as of 08:50, 4 October 2014
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]
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
# upper limit of lines to display for text files
nlines=150
# upper limit of bytes to display for binary files
nbytes=2048
# language of text files
language=russian
# output encoding for text files
encoding=utf-8
info=$(head -$nlines "$1" | file --mime -)
charset=${info#*=}
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"
else
head -$nlines "$1" | enconv -g -L $language -x $encoding
fi
The script uses:
head
utility to process only first$nlines
of file.file
tool to determine whether file is of binary format or not.hexdump
tool to display binary files in classic hex dump form.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:
- 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
). - On Unix-like operating systems make the script file executable by running
chmod +x defviewer
- Add the following line to your
vifmrc
:fileview * defviewer %c
Usage[edit]
- 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.