Skip to content

Instantly share code, notes, and snippets.

@benspaulding
Created October 17, 2011 01:24
Show Gist options
  • Save benspaulding/1291706 to your computer and use it in GitHub Desktop.
Save benspaulding/1291706 to your computer and use it in GitHub Desktop.
Scripts for using BBEdit, TextWrangler, or MacVim as default command line EDITOR.

EDITOR Helpers

Author

Ben Spaulding

This is a collection of helper scripts for using popular Mac text editors as your terminal EDITOR.

These are certainly not essential, as all of the editors helped here ship with a powerful command-line utility. However, to properly use them for something like editing a git commit message the editor must not fork, and must wait for you to do your work, then return focus to the terminal. This is done by passing switches and arguments to the utility. Unfortunately, some software — notably crontab — chokes on a multi-argument EDITOR. The solution is to use a wrapper script that calls the switches and arguments for you. That is just what these helpers do.

Helpers are included for:

Notably absent is TextMate, because no helper is necessary. See the details in the TextMate manual.

#!/usr/bin/env bash
# Abstract:
# Script for using BBEdit as the default EDITOR.
#
# Author:
# Ben Spaulding <http://benspaulding.us/>
#
# Details:
# This script allows BBEdit to be the ``EDITOR`` in a more robust way than
# the ``bbedit`` command line tool allows. This is because the ``bbedit``
# command exits immediately after the file arguments are opened, unless the
# ``--wait`` switch is used. However, some tools, such as crontab, fail with
# a multiple-term ``EDITOR``. This script can be set as the ``EDITOR``,
# resolving that issue. The ``--resume`` switch is also used, which returns
# focus to the referring app (e.g., Terminal.app) after editing is complete.
#
# Usage:
# Save this script to somewhere like your ~/bin directory. You can then put
# something such as the following in your ~/.bashrc file::
#
# # Set bbeditor as EDITOR.
# if [ -f "$HOME/bin/bbeditor" ]; then
# export EDITOR="$HOME/bin/bbeditor"
# else
# echo "WARNING: Can't find bbeditor."
# fi
#
if [ -f "`which bbedit`" ]; then
bbedit --wait --resume "$@"
else
echo "ERROR: Can't find bbedit. Did you install the command line tools?"
fi
#!/usr/bin/env bash
# Abstract:
# Script for using MacVim as the default EDITOR.
#
# Author:
# Ben Spaulding <http://benspaulding.us/>
#
# Details:
# This script allows MacVim to be the ``EDITOR`` in a more robust way than
# the ``mvim`` command line tool allows. This is because the ``mvim``
# command exits immediately after the file arguments are opened, unless the
# ``-f`` switch is used. However, some tools, such as crontab, fail with a
# multiple-term ``EDITOR``. This script can be set as the ``EDITOR``,
# resolving that issue. The ``-c "au VimLeave * maca hide:"`` switch and
# argument are also used, which returns focus to the referring app (e.g.,
# Terminal.app) after editing is complete.
#
# Usage:
# Save this script to somewhere like your ~/bin directory. You can then put
# something such as the following in your ~/.bashrc file::
#
# # Set mveditor/MacVim as EDITOR.
# if [ -f "$HOME/bin/mveditor" ]; then
# export EDITOR="$HOME/bin/mveditor"
# else
# echo "WARNING: Can't find mveditor. Using vim instead."
# export EDITOR="vim"
# fi
#
if [ -f "`which mvim`" ]; then
mvim -f -c "au VimLeave * maca hide:" "$@"
else
echo "ERROR: Can't find mvim. Did you install the MacVim utility?"
echo "NOTICE: Using vim instead."
vim "$@"
fi
#!/usr/bin/env bash
# Abstract:
# Script for using TextWrangler as the default EDITOR.
#
# Author:
# Ben Spaulding <http://benspaulding.us/>
#
# Details:
# This script allows TextWrangler to be the ``EDITOR`` in a more robust way
# than the ``edit`` command line tool allows. This is because the ``edit``
# command exits immediately after the file arguments are opened, unless the
# ``--wait`` switch is used. However, some tools, such as crontab, fail with
# a multiple-term ``EDITOR``. This script can be set as the ``EDITOR``,
# resolving that issue. The ``--resume`` switch is also used, which returns
# focus to the referring app (e.g., Terminal.app) after editing is complete.
#
# Tip: If you're using TextWrangler 3.5.3 or later, you may get the
# following error when editing a crontab or other similar files::
#
# crontab: temp file must be edited in place
#
# Evidently a change to the way files are saved found it's way in from
# TextWrangler's big brother, BBEdit. (Interestingly, however, this is not
# an issue with BBEdit.) You can fix this issue by quitting TextWrangler and
# running the follow command on the command line::
#
# defaults write com.barebones.textwrangler Filing:SafeSavesDisabled -bool YES
#
# For more information, see the following:
#
# * http://hints.macworld.com/comment.php?mode=view&cid=122750
# * http://www.barebones.com/support/bbedit/arch_bbedit951.html
#
# Usage:
# Save this script to somewhere like your ~/bin directory. You can then put
# something such as the following in your ~/.bashrc file::
#
# # Set tweditor as EDITOR.
# if [ -f "$HOME/bin/tweditor" ]; then
# export EDITOR="$HOME/bin/tweditor"
# else
# echo "WARNING: Can't find tweditor."
# fi
#
if [ -f "`which edit`" ]; then
edit --wait --resume "$@"
else
echo "ERROR: Can't find edit. Did you install the command line tools?"
fi
@kevinashworth
Copy link

Thanks for this!

@benspaulding
Copy link
Author

This gist has been moved to a proper GitHub project at github.com/benspaulding/terminal-editor-helpers.

@anton-matosov
Copy link

Thanks!!! You saved my life!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment