Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create or reuse existing Emacs GUI frame

Tags:

emacs

Assuming an Emacs server is running, I want emacsclient <file> to either create a new frame (like -c) when there is no existing frame or reuse an existing frame when there is one. In other words, I want the -c only when there is no existing frame. Is that possible?

like image 702
Renke Grunwald Avatar asked Nov 13 '22 07:11

Renke Grunwald


1 Answers

I solved my problem with a set of shell scripts.

my_emacs

#!/bin/sh
emacs24-x $@ 1> /dev/null 2> /dev/null &

You may need to change emacs24-x to something that points to your X11 emacs.

my_emacsclient

#!/bin/sh
emacsclient $@ 1> /dev/null 2> /dev/null || my_emacs

Add both files to you PATH via ~/bin or the like.

In my .emacs I also added the followings lines

(load "server")
(unless (server-running-p) (server-start))

Also change some environment variables and optionally add an alias

export ALTERNATE_EDITOR="my_emacs"
export EDITOR="my_emacsclient -n"
export SUDO_EDITOR="my_emacsclient"
...
alias e="$EDITOR"

When you run e in your shell it should create or resuse an existing GUI frame. Also, running e <filename> opens that file in a frame; you can also pass other flags like -n to e.

For the same behaviour in other applications (say your file manager), you should also change the Emacs .desktop file (for me that's /usr/share/applications/emacs24.desktop) to run my_emacs.

It might also be a good idea to change emacs to my_emacsclient via the alternatives system in Debian-based (?) distributions.

like image 101
Renke Grunwald Avatar answered Jan 05 '23 01:01

Renke Grunwald