I'm running Emacs in a GUI, and I like having a key binding that quickly switches me to a terminal. Yes, I know there's eshell, term, and shell in Emacs, but I still prefer a dedicated terminal emulator for a lot of my work.

When I'm on macOs, I use a function that switches application focus to iTerm,

(defun os-switch-to-term ()
  (when (display-graphic-p)
    (do-applescript "tell application \"iTerm\" to activate")))

I'm pretty sure I lifted this from doom-emacs. I'm now spending most of my time in Linux, so I'm searching for a way to translate the above functionality to something that will work in a different desktop environment (I'm currently using Gnome on Ubuntu, and gnome-terminal, though I'm still hunting for a terminal emulator I like better).

I imagine the options will depend on the flavor of Linux, and which window manager and/or desktop manager you're using, but as a start, how might I translate this to a Gnome environment w/gnome-terminal?

  • 1
    If you can call it from a terminal, and bring it to the front, then you can write your own using start-process or other shell-command function.
    – lawlist
    Commented Nov 9, 2017 at 3:45

1 Answer 1


Perhaps something like this?

(defun os-switch-to-term ()
  "Open an external terminal window."
  (unless (catch 'found
            (when (display-graphic-p)
               ;; OSX
               ((and (eq system-type 'darwin)
                     (fboundp #'do-applescript))
                (do-applescript "tell application \"iTerm\" to activate")
                (throw 'found t))
               ;; GNU/Linux
               ((memq system-type '(gnu gnu/linux))
                (dolist (command '("gnome-terminal" "xterm"))
                  (when (executable-find command)
                    (start-process command nil command)
                    (throw 'found t)))))))
    ;; Fallback
    (call-interactively #'ansi-term)))
  • 1
    This will start a new instance of the terminal, wmctrl will switch to an existing one if it's installed and supported by the window manager
    – matteol
    Commented Nov 9, 2017 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.