Ben Oliver

How to get Ranger to preview Markdown files

Pretty-up your file manager
18 May 2015

Ranger1 has a really nifty preview pane on its right hand side that tries to give you an idea of what a file is before you open it. This is mainly why I use it at all - rather than open and close every single PDF or image in a folder, I can simply flip through them. Same goes with text.

That’s when I wondered if it would be possible for it to show the formatting in Markdown2 files rather than just the plain text versions. Turns out it is, and it’s pretty easy!

Take a look below.

Before the change.

You can see above that the file is previewed on the right in plain text.

After the change.

Now you can see that the pound signs are gone, the numbered list is actually numbered and the code is indented.

It’s not perfect, the horizontal rule isn’t there and it’s lacking some colour, but as a quick way to tidy up the preview it’s not bad.

How you can do this

You’ll need npm3 to get the required package installed, on my distro (Arch4) it was in the repositories so it was

sudo pacman -S npm

Then you want to install ansimd5 with npm. This is what creates an output from markdown files that is readable in the terminal.

To do this I just had to do

sudo npm install ansimd -g

Then you need to edit your ~/.config/ranger/ with the following in the $extension section:

# Markdown files
        try ansimd "$path" && { dump | trim; exit 5; } || exit 2;;

Put it after any ;; but before the esac line.

All done! Open ranger, hover over a markdown file, and you’ll see it’s formatted now.