bash function: md.view

One of my favorite bash functions is md.view, which is available in my dotfiles repository.

# test markdown files, probably a better way to test for programs.
function md.view {
    local i=true
    type -p markdown &> /dev/null || i=false
    if $i ; then 
        local output="/tmp/md.view-$(date +%F).html";
        markdown $1 > "$output";
        google-chrome "$output"; # xdg-open would open default browser after next line executes
        rm "$output";
        echo "markdown is not installed"

There’s not really anything particularly cool about this function. It checks for markdown and reminds me to install it if I haven’t yet. It doesn’t do this for Google Chrome because that’s the first application I install on new machines. Usually, you’d use xdg-open to choose a browser, but this can cause problems with the remove command at the end of the function (depending on how the browser creates its process).

Anyway, this is really useful when I’m creating README files using markdown for my github repositories. For instance, I was just updating a README to refer to *_32.png images. The problem is that * and _ are special tokens in markdown. I’d never attempted to escape one after the other, which is often broken in parsers. With this function in my extended .bash\functions, I can quickly check output with:

$ md.view

I know, it’s such a simple thing to have as a favorite. Sometimes really simple things have a huge impact, especially if you type out README files regularly

Related Articles