Emacs systemd service

Since upgrading to Ubuntu 15.04, I’ve been living with systemd. I’ve been a Debian user mainly and was wary of the doom-and-gloom comments about systemd but actually, I’ve been a bit impressed with it so far. One thing that is undeniably easier with systemd is writing what System V init sytems called init scripts. In systemd they are known as service files.

Anyway, I usually run emacs as daemon and I had the idea that emacs could run as a systemd service! Immediately I fell into the trap that Umberto Eco describes:

there are two laws no human being can escape: the first idea that comes into a person’s mind will be the most obvious one; and, having had an obvious idea, nobody ever thinks that others may have had the same idea before.

So yes, there plenty of systmed emacs service files out there, but this one is mine.

[Unit]
Description=Emacs daemon
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(progn (setq kill-emacs-hook 'nil) (kill-emacs))"
Restart=always
Environment=SSH_AUTH_SOCK=/run/user/1000/keyring/ssh GPG_AGENT_INFO=/run/user/1000/keyring/gpg:0:1
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

The main difference from my config is that I’ve added the GPG_AGENT_INFO data so that emacs can uses GPG agent. I run keychain so my ssh-agent and gpg-agent are launched at login. There are probably nicer ways to pass the environment variables. This only works if you user ID is 1000, which it most likely will be if you are the first user. Just check the output of id -u and change the 1000 to whatever it says and it will probably work.

Lastly, I run this service as a user so I put the file in ~/.config/systemd/user/emacs.service and then did:

systemctl --user enable emacs.service   
systemctl --user start emacs.service

I use emacsclient to connect to the server, which I’ve aliased to just e, so I’ll type e -nc to open a new instance for example.

Back in (BeagleBone) Black

After a short hiatus, I’ve dusted off my BeagleBone Black (BBB) and started tinkering again.  This time I replaced Angstrom with Ubuntu 13.04.  Angstrom is a good distribution for the truly embedded projects, i.e. ones that require a small footprint and low-level IO, but for more application-level play, I think Ubuntu is a bit more user friendly.

Continue reading “Back in (BeagleBone) Black”

Open Emacs dired buffer from shell

Lately, I’ve had a falling-out with M-x shell.  There are a few ways to incorporate the shell in Emacs and for the most part, the inferior shell (as it’s called in Emacs) has served me well.  However there are some annoyances and the biggest one for me is that it’s not a *real* terminal.

While I like the idea of eshell, it doesn’t quite cut it as a true shell replacement.  Fortunately with Byobu and zsh, I’ve been quite happy with my shell experience.  Yet sometimes when I’m levels deep in a repository, I just want to oped a dired buffer!  So I added this small shell function to my rc file:

# open a dired window for the current directory
dired() {
    emacsclient -e "(dired \"$PWD\")"
}

It’s a neat trick (the “-e” option) where you can pass Lisp code to be evaluated instead of opening a file.  This assumes that you are running Emacs as a server.  By the way, using emacsclient -t while in a shell is a way to accomplish “quick” editing.  Since Emacs is running at a daemon, the oft-cited long Emacs start times are no longer an issue.

Update: *Face Palm* original title was “update Emacs dired buffer from dired!” Argh…