Reading binary files in the Shell and friends
Table of Contents
Preview PDFs, Office documents, and other binary formats directly in terminals and TUIs without launching heavy GUI apps:
Install converters such as poppler, unrtf, pandoc, LibreOffice, or Tika, then enable lesspipe (or vim-office), and tailor ~/.mailcap to route previews per context.
-
On Windows — whether in PowerShell or Command Prompt (cmd.exe) — use the vim-office plugin to extract and display human‑readable text from binary files in Vim.
-
On macOS, Linux, or on Windows via Windows Subsystem for Linux (WSL), Git Bash (or more generally MSYS2), use lesspipe.sh or lesspipe to preview binary files in tools such as
less,mutt,vim, or file managers likeranger,lf,nnn, andyazi.
See the lesspipe wiki for setup instructions to integrate with these programs and preview binary files. -
To customize text conversion, use run-mailcap (installed by default on Debian and derivatives such as Ubuntu or Linux Mint) with a suitable ~/.mailcap file that routes callers (
less,mutt,vim, and file managers likeranger,lf,nnn, andyazi) to appropriate handlers to the appropriate handlers (using mutt_bg_run).
application/pdf; $HOME/.config/mutt/bin/mutt_bgrun "${PDFVIEWER:-zathura}" %s; test=test -n "$DISPLAY"; nametemplate=%s.pdf; description="PDF Document"
application/pdf; pdftotext -nopgbrk -q -- %s -; test=test -n "$VIM"; nametemplate=%s.pdf; copiousoutput
application/pdf; pdftotext -l 20 -nopgbrk -q -htmlmeta -- %s - \| w3m -dump -T text/html; test=ps -o comm= -p "$PPID" | grep -Eq '^(mutt|neomutt)$'; nametemplate=%s.pdf; copiousoutput
The conversion from a file in a binary format to text requires the installation of appropriate external converters such as unrtf, pandoc, docx2txt.pl, odt2txt, xlscat, xlsx2csv.py or pptx2md, but already LibreOffice or Tika can go a long way.
If you use Debian or one of its derivatives, such as Ubuntu or Linux Mint, these packages have you covered:
sudo apt install unrtf catdoc abiword xlsx2csv wv docx2txt odt2txt poppler-utils djvulibre-bin w3m qpdfview p7zip-full zathura