Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Start from CLI
# Emacs starter for Emacs mac port
# Thanks to Aquamacs Project and David Reitter
my $args = "";
my $tmpfiles = "";
for my $f (@ARGV) {
$args .= '"'.$f.'" ';
$tmpfiles .= '"'.$f.'" ' if (! -e $f);
system("touch $args") if ($tmpfiles);
system("open -a /Applications/ $args");
# delay deletion because AE drag&drop doesn't work with non-existing documents
system("(sleep 3; rm $tmpfiles) &") if ($tmpfiles);

ptb commented May 19, 2015

I rewrote this as a shell script and added the capability to accommodate filenames with spaces:


if [ -e '/Applications/' ]; then

  if [ ${#@} -ne 0 ]; then
    while IFS= read -r file; do
      [ ! -f "$file" ] && t+=("$file") && /usr/bin/touch "$file"
      file=$(echo $(cd $(dirname "$file") && pwd -P)/$(basename "$file"))
      $(/usr/bin/osascript <<-END
        if application "" is running then
          tell application id (id of application "") to open POSIX file "$file"
          tell application ((path to applications folder as text) & "")
            open POSIX file "$file"
          end tell
        end if
      ) &  # Note: END on the previous line may be indented with tabs but not spaces
    done <<<"$(printf '%s\n' "$@")"

  if [ ! -z "$t" ]; then
    $(/bin/sleep 10; for file in "${t[@]}"; do
      [ ! -s "$file" ] && /bin/rm "$file";
    done) &
  vim -No "$@"

Since I switched from vi, I can't help but to type vi filename every time, so I saved this script as /usr/local/bin/vi. In the off chance I haven't yet installed, it uses the built in system vim instead.


railwaycat commented May 30, 2015

Thanks @ptb :)

lmartins commented Jun 8, 2015

Sorry to ask guys, how can I install this?

@lmartins Paste into a file named emacs (or whatever you want the command to be) in a directory in your path (ahead of the location of the existing emacs command). Then chmod +x /path/to/emacs.

jasonm23 commented Oct 1, 2016

It'd be better to use emacsclient (and kick off as the alternate editor if it's not running already.)

tasmo commented Oct 25, 2016

The command open with the argument -a does not need the full application path. It's not useless but if you have installed Emacs in a different location I would consider just to use

system("open -a Emacs $args");

mwolson commented Nov 24, 2016

Here's a variation that ensures is launched, waits for it to be ready, and then uses emacsclient to open the file in a GUI frame and wait for edits to be completed.



$(/usr/bin/osascript <<-END
  tell application ((path to applications folder as text) & "")
  end tell

while ! "$CLIENT" -e '"pass"' 2>&1 | grep '"pass"' > /dev/null; do
    sleep 0.3

if test "$#" -gt 0; then
    exec "$CLIENT" -c $@

railwaycat commented Nov 30, 2016

Hi @mwolson, thanks for the update. I believe your starter script works for Nextstep port (although I haven't verify that by myself), but it not works for Mac port. Because Emacs Mac port lacks multi-tty support, which means, emacsclient from mac port can only able to create tty frames but not GUI frames.

ivanbrennan commented Jan 26, 2017 edited

@ptb Just curious, why do you use a sub-shell to initialize t in your script? Any reason not to use t="" instead?

Edit: After some digging, I see I was misinterpreting the parentheses and mistaking an array assignment for a sub-shell.

achikin commented May 12, 2017

Do I need to start server in emacs config before running this? because emacsclient gives me the following error

emacsclient -c nginx.conf
emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".

ivanbrennan commented Jun 5, 2017 edited

I made some small modifications to the script @ptb provided. This variation is callable with or without file args, and will make emacs visible even if it was previously hidden.

The if [ $# -gt 0] block that handles file args is run in the background (see the closing fi &), but its contents run synchronously with respect to each other. That prevents deleting the tempfiles before they've been visited, with no need for sleep.



if [ ! -x $emacs_app ]; then
  echo " not found" >&2
  exit 1

/usr/bin/osascript -e "tell application \"$emacs_app\" to activate" &

if [ $# -gt 0 ]; then

  while IFS= read -r filename; do
    if [ ! -f "$filename" ]; then
      /usr/bin/touch "$filename"

    file=$(echo $(cd $(dirname "$filename") && pwd -P)/$(basename "$filename"))
    /usr/bin/osascript -e "tell application \"$emacs_app\" to open POSIX file \"$file\""
  done <<< "$(printf '%s\n' "$@")"

  for tempfile in "${tempfiles[@]}"; do
    [ ! -s "$tempfile" ] && /bin/rm "$tempfile"
fi &

This will create buffers of any number of existing or non-existing files with or without spaces in their names in a new instance of emacs_starter file1 file2 file3 ...

#! /usr/bin/env perl
local $" = qq{" "};
my (@have, @havenot);
for (@ARGV) {-e && {push @have, $_} || push @havenot, $_}
system qq{open -a "@have" --args "@havenot"};

If you have an emacs server running, then typing emacsclient file1 file2 file3 ... will use the server.

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