Categories
Software

How To Easily Open A File From Terminal In Coda 2

I’ve been playing a lot more with Coda 2 upon purchasing it since its initial release a couple of weeks ago. While I had been teetering on purchasing version 1 last year, I held off on anticipation of the version 2 release. In the meantime I played with Sublime Text 2.

While I’m certainly no coding ninja I did find Sublime Text 2 a little difficult to undertake all my grasshopper coding stuff. In most cases I found 4 pieces of software open for my workflow:

  1. The Sublime Text editor itself (where all the coding action happened)
  2. Terminal (where I would interface with the server)
  3. Panic’s Transmit (if needed when uploading images, etc.)
  4. The browser (to check everything was working)

During this time I wore out my ⌘-Tab keys a lot!

Thankfully, when Coda 2 was released I jumped at the opportunity and bought straight away. All the things I have open in my Sublime Text 2 workflow were now all rolled into one nice neat piece of software.

However, upon using it more I did begin to ask myself the question: How can I open files from the command-line in Terminal and have them opened in Coda 2’s text editor?

For example, if I run the command:

$ coda test.js

I want text.js now opened as a tab in the text editor, and if text.js hasn’t been created or doesn’t exist in the current working folder then create it.

After scouring the internet for a while I stumbled upon Noah Frederick’s shell script code and slightly modified it to allow what I wanted in my Terminal. Here’s what I did to get it to work:

  1. Open up Terminal and navigate to your home directory (generally cd ~ or wherever your .bash_profile file is)
$ cd ~
  1. Edit your .bash_profile file by copying and pasting the code found below – I usually do quick edits in Terminal by using:
$ nano .bash_profile
#!/bin/sh
# Coda from the command line
# Version 1.0
#
# Open Coda or open files in Coda from the command line
#
# (C) Noah Frederick 2011 <http://noahfrederick.com>
#
# Version 1.1
# point codapath to Coda 2.app
# make coda default editor with open command
# create alias for 'coda'
###################### CONFIG ######################
# Path to Coda.app
codapath="/Applications/Coda 2.app"
####################################################
# make default editor
export EDITOR="$codapath -w"
# create 'coda' alias
alias coda=open_with_coda
# open_with_coda ():
# Open any number of files with Coda.app
# Files that don't exist will be created
open_with_coda () {
for arg in "$@"
do
if [ ! -f "$arg" ] ; then
touch "$arg"
fi
done
open -a "$codapath" "$@"
}
# open_coda ():
# Launch Coda.app
open_coda () {
open "$codapath"
}
# display_help ():
# Show a helpful usage message
display_help () {
echo "usage: $0 [--help|-h] [<file> ...]"
echo
echo '\t--help|-h show this message'
echo '\t<file> file will be opened in Coda.app'
echo '\t (omit to launch Coda.app)'
echo
}
####################################################
if [ $# -gt 0 ]; then
case "$1" in
--help|-h)
display_help
;;
*)
open_with_coda "$@"
;;
esac
else
open_coda
fi
view raw coda-shell-script-sh hosted with ❤ by GitHub
  1. Save your .bash_profile
  2. Restart your .bash_profile with the command:
$ source .bash_profile
  1. Test your code by opening a file and then creating a file – hopefully it all works!