Home Link

Hello, my name is Brett Terpstra, and it’s nice to meet you. Elegant solutions to complex problems. Curious?

A Bash function for Markdown bloggers

A Bash function for Markdown bloggers Tagged: bashmarkdownmdfindscriptingspotlight
Jun 24, 2011 (226 days ago)

spacer I store all of my writing as separate Markdown files. A basic tagging system1 adds more “searchability,” and I can quickly locate any file with Spotlight2. Given the amount of time I spend in Terminal (well, iTerm 2 these days), I use mdfind quite a bit to do the Spotlight searching. This function just makes it a little more convenient to search for and quickly edit an existing document.

By default, it looks in a Dropbox “Writing” folder, but you can adjust that to restrict to any folder at the top of the script. You can also set your edit command there (mate, mvim, vim, etc.). Then it takes all arguments and uses them as a Spotlight search, restricted to your writing folder and the Markdown filetype, and offers you a quick menu of matches. Selecting a match opens the file with your preferred editor. If there’s only one match, it’s opened automatically.

It’s written as a function to be included in your .bash_profile (or sourced from there). If you want to run it as a shell script, just remove the edmd () { at the top and the closing }, put it in your path and run chmod a+x filename.

Once it’s installed you can just type edmd keyword to find posts and drafts related to keyword. You can use multiple words (no quotes required) and prefixes like “keyword:” or “tag:”, just like a Spotlight query.

# Edit Markdown File from Writing directory
# Finds Markdown files matching a Spotlight-style search query
# If there's more than one, you get a menu
edmd () {
  WRITINGDIR="~/Dropbox/Writing"
  EDITCMD="mate"
  filelist=$(mdfind -onlyin "$WRITINGDIR" "($@) AND kind:markdown")
  listlength=$(mdfind -onlyin "$WRITINGDIR" -count "($@) AND kind:markdown")
  if [[ $listlength > 0 ]]; then
    if [[ $listlength == 1 ]]; then
      $EDITCMD $filelist
    else
      IFS=$'\n'
      PS3='Edit which file? (1 to cancel): '
      select OPT in "Cancel" $filelist; do
        if [ $OPT != "Cancel" ]; then
          $EDITCMD $OPT
        fi
        break
      done
    fi
  else
    return 1
  fi
  return 0
}

  1. I save drafts and posts to my desktop as .md files. During my daily review I use Tags.app or Filr to quickly add target keywords (‘blogging’, a tag for which blog it’s for and one, maybe two topical tags). Hazel picks these up and stores them in my Dropbox “Writing” folder, in subfolders based on blog and filetype. ↩

  2. I never realized it, but apparently Spotlight doesn’t search these by default. My use of the QLMarkdown Quick Look plugin fixes that, I guess. ↩

Related posts:

  • Fixing Spotlight indexing of Markdown content
  • oft: Bash function for opening a specific filetype
  • fk: a useful bash function
  • A simple but handy Bash function: console
  • Markdown QuickTags: WordPress plugin for Markdown lovers
This entry was posted on Friday, June 24th, 2011 at 1:37 pm and is filed under Blog, Code.You can leave a response, or trackback from your own site. Tagged: bashmarkdownmdfindscriptingspotlight

Leave a Reply

Click here to cancel reply.

Comments may use standard Markdown formatting


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Saying…

Follow me on Twitter

spacer spacer

Markdown? spacer

marked

MultiMarkdown preview.
Everywhere.

Learn More

Looking for me elsewhere? Here's my card.

spacer
Writing…RSS
  • Introducing Gather, a Cocoa Markdownifier
  • Connecting nvALT and Address Book
  • ScrivWatcher, one more time
  • From my Macworld Diary
  • ScrivWatcher droplet, an easier live Scrivener preview
  • Preview a full Scrivener document in Marked, live
  • Web excursions: January 20, 2012 — January 24, 2012
  • A Service for writing MultiMarkdown footnotes inline
  • iOS-inspired popup box CSS
  • System Service: Clip to Day One
Listening… (last.fm)
  • Sorting…
    • Blog
    • Bookmarks
    • Code
    • Featured
    • Misc
    • Music
    • Reviews
    • Write
Thanks for reading!

Entries (RSS) and Comments (RSS), or Subscribe by Email

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.