4

I'm trying to write a script that switches focus to Emacs. This is what I have:

#!/bin/bash
wmctrl -a 'emacs@pat-ubuntu-desktop'

It works fine when there is only one Emacs window (or "frame," in Emacs parlance) open, but it doesn't do anything when multiple Emacs windows are open. The problem seems to be that the window titles change when a second window is opened. When there's a single window open, its name is emacs@pat-ubuntu-desktop:

➜  ~  wmctrl -l
0x05c000a3  0 pat-ubuntu-desktop emacs@pat-ubuntu-desktop

But when I open a second window, the window titles change:

➜  ~  wmctrl -l
0x05c000a3  0 pat-ubuntu-desktop *scratch*
0x05c00921  0 pat-ubuntu-desktop *scratch*

EDIT: The following issue was illusory, the result of my web browser having "emacs" in its title (because I was searching information about my first problem).

Another issue (perhaps related, perhaps not), is that even when there is only a single Emacs window open, the command wmctrl -a 'emacs' doesn't work, but wmctrl -a 'emacs@' (or wmctrl -a 'emacs@pat-ubuntu-desktop') does. Why must the @ be included?

3
  • 1
    I don't have either of the problems you do. My emacs windows have consistent titles (you might need to mess with frame-title-format in your .emacs), and wmctrl -a emacs works in all cases for me Jun 17, 2013 at 1:50
  • I can no longer reproduce my second problem. I think the reason that I had to include the @ symbol was that I was searching in my web browser for information about my first problem, and the title of the web-browser window naturally contained "emacs." Focus was shifted to the web browser, but I didn't even notice, probably because it already had focus. Silly me! Jun 17, 2013 at 3:47
  • Adding (setq frame-title-format '("" invocation-name " %f")) to my .emacs solved my problem. Thanks! @MichaelMrozek: Are you using Ubuntu with Unity? Perhaps the inconsistent window names have something to the OS I'm using. Jun 17, 2013 at 3:51

2 Answers 2

2

If you just want to switch focus, why not use emacsclient?

emacsclient --eval '(select-frame-set-input-focus (selected-frame))'

If you want to open a file at the same time, you can even use

emacsclient -n FILE

If you need to use wmctrl for other reasons, I guess you can get (emacs-pid), (frame-parameter frame 'display), (frame-parameter frame 'window-id), etc. by emacsclient and then feed them to wmctrl.

Also have a look at: https://github.com/tkf/emacs-wmctrl

1

Matching on the window title isn't very reliable. For example, if you're viewing this question in your browser, then wmctrl -a 'emacs' might activate your browser.

You can customize the frame title format with frame-title-format. I use (multiple-frames "%b" ("" invocation-name "@" system-name)). But I don't recommend relying on this in your script.

You can tell wmctrl to look for a window by class with the option -x. That's both simple and reliable.

wmctrl -x -a Emacs

Alternatively, you can make Emacs do the job. This gives you a better chance of picking the “best” frame when there are multiple active frames.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .