View Markdown Files in your Terminal
How you can view Markdown files in your terminal easily
I spend all day in my terminal and for better or worse I deal with a lot of Markdown files throughout the day.
One thing I often find myself needing to do is preview or read a markdown file, a lot of the time this could be a README.md for a project I am coming back to or maybe a blog post that is sitting in draft somewhere.
I decided to write a very quick function that would facilitate previewing Markdown files in the terminal. I wanted to share this because I am sure this annoys more than just me!
There are two programs I am using to help me do this.
The first is Pandoc, which is great at taking things like Markdown and changing it into something else (say, HTML).
The second is Lynx, which is a text based web browser that you can run in your terminal.
Installing Pandoc and Lynx
On OS X I would recommend installing these using homebrew.
brew install pandoc
brew install lynx
If you are running Linux, your package manager of choice should have them.
Setting up the script
Once you have these installed the helper function we need to join them together is very simple.
Open up ~/.profile
or whatever text file you like using for your terminal preferences and paste the following;
rmd () {
pandoc $1 | lynx -stdin
}
What this does is defines a function called rmd (Read Markdown) and in the body of the function it tells Pandoc to read in whatever file you have specified and pass this to lynx which will be listening for standard input.
Once you have this in and have saved your file you will need to run source ~/.profile
(or whatever file you edited).
Usage
To use this just call rmd path/to/markdowndown/file.md
and it will open up as a webpage in Lynx.
To quit out of Lynx just press q and you will be back to your terminal.
Because Lynx is a fully fledged browser you can follow links in the markdown file and interact just like you would if you were viewing it in your normal browser.