summaryrefslogtreecommitdiffstats
path: root/content/posts/using-emacsclient-on-macos/index.org
blob: c0513ab9bc332b8104ffcdde3c888c792af34abc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#+TITLE: Using emacsclient on MacOS
#+DATE: 2020-06-18T17:10:15-04:00
#+DRAFT: false
#+DESCRIPTION: Getting emacsclient setup nicely on MacOS
#+TAGS[]: macos emacs
#+KEYWORDS[]:
#+SLUG:
#+SUMMARY:

#+ATTR_HTML: :alt MacOS Emacsclient
#+ATTR_HTML: :title MacOS Emacsclient
[[file:cover.png]]

Emacs is an amazing editor, but it can be a little slow to start
sometimes. That's why =emacsclient= lets you run Emacs as a daemon and
connect to it as a client, negating the startup time and letting you
jump directly into editing.

The two primary ways of getting Emacs onto your Mac are by either
downloading it from [[https://emacsformacosx.com/][Emacs For MacOS X]] or by installing it through
[[https://brew.sh/][homebrew]] using =brew cask install emacs=. I prefer the homebrew method
as it automatically adds =emacs= and =emacsclient= to your =$PATH=.

Using it from the command line is a breeze. I have this alias in my
=.bashrc=

#+BEGIN_SRC shell
alias em="emacsclient -t -a ''"
#+END_SRC

This way I can type =em <filename>= to edit a file. =-t= tells
emacsclient to start in terminal mode, and =-a ''= tell it to start the
Emacs daemon process if it isn't already running.

Getting the GUI version to run without first opening the command line
is a little trickier. Homebrew provides an app for =emacs=, but no
launcher for =emacsclient=. The way I've worked around that is by
using [[https://github.com/deseven/icanhazshortcut][iCanHazShortcut]], which lets you bind arbitrary shell commands to
keyboard shortcuts. You can download the app from the [[https://github.com/deseven/icanhazshortcut/releases][release]] area on
github.

To add a new shortcut, download and install iCanHazShortcut, then open
the app and click the =Shortcuts= tab at the top. Then click the plus
in the bottom right corner, click inside the =Shortcut= field, press
the desired shortcut keys, give it an action name, and a command.

#+ATTR_HTML: :title Create a new shortcut
#+ATTR_HTML: :alt create a new shortcut in icanhazshortcut
[[file:create%20new%20shortcut.png]]

#+ATTR_HTML: :title My emacsclient shortcut
#+ATTR_HTML: :alt my emacsclient shortcut
[[file:emacs%20client%20shortcut.png]]

To launch =emacsclient=, I have a shortcut bound to
@@html:<kbd>⌃</kbd> + <kbd>⇧</kbd> + <kbd>⌘</kbd> + <kbd>E</kbd>@@
which runs
#+BEGIN_SRC shell
emacsclient -c -n -a ''
#+END_SRC

The =-c= flag tells =emacsclient= to open a GUI window instead of in
terminal mode. The =-n= flag tells the command to return once the
window is opened, instead of waiting for the client to close. Finally,
like before the =-a ''= flag tells =emacsclient= to start the Emacs
daemon if it isn't already running.

Now all I have to do is press @@html:<kbd>⌃</kbd> + <kbd>⇧</kbd> + <kbd>⌘</kbd> + <kbd>E</kbd>@@
and a new Emacs window pops up with no startup delay!

It's also possible to create an "application" shortcut for
=emacsclient= using [[https://support.apple.com/en-ca/guide/automator/welcome/mac][Automator]].

Create a new automation as an Application with the Run Shell Script
step. Then add the following as the contents of the script.

#+begin_src shell
/opt/homebrew/bin/emacsclient \
    --no-wait \
    --quiet \
    --suppress-output \
    --create-frame \
    "$@"
#+end_src

Replacing =/opt/homebrew/bin/emacsclient= with the path to your
=emacsclient= if it's located elsewhere.

Then set =Pass input= to =as arguments=.

#+ATTR_HTML: :title Automator emacsclient script
#+ATTR_HTML: :alt Automator emacsclient script
[[file:emacsclient automator.png]]

Finally place the Automator application in your =/Applications=
directory.

You should now be able to open Emacsclient through spotlight and
associate it with filetypes.