Quick tips are random posts regarding something I discovered on my way to something bigger. They usually get longer than “quick” would imply, for which I refuse to apologize.

Here’s a quick trick I’ve been using to load different editors from the command line based on the type of file. It can be extended to whatever file types you want to handle and use whichever editor you prefer.

I use Vim to edit git messages, MultiMarkdown Composer (via the mmdc command available from the website) for text and Markdown files, and Sublime Text 2 for just about everything else.

First, I created an editor.sh file in my scripts folder:

editor.shraw
"
#!/bin/bash

case "$1" in
	*_EDITMSG|*MERGE_MSG|*_TAGMSG )
		/usr/local/bin/vim "$1"
		;;
	*.md )
		/usr/local/bin/mmdc "$1"
		;;
	*.txt )
		/usr/local/bin/mmdc "$1"
		;;
	* )
		/usr/local/bin/subl -w "$1"
		;;
esac

Then I just make it executable (chmod a+x ~/scripts/editor.sh) and point my EDITOR variable to it in my .bash_profile:

export EDITOR="$HOME/scripts/editor.sh"

Now if I run a git merge and need to enter a message, it pops up Vim right in the terminal I’m working in. If I edit a Markdown file, it automatically opens it in MultiMarkdown composer. Anything else gets sent to Sublime Text 2. Like I said, it’s easy to customize with more file types and editors, but I thought I’d share the idea for anyone who’d find it helpful.

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.