I currently have 2 key bindings for switching between two major modes:

(global-set-key (kbd "<f12>") 'js2-jsx-mode)
(global-set-key (kbd "<f13>") 'web-mode)

How would I reduce that to a single key binding, so that it toggles between these modes depending on the current mode?

1 Answer 1


You can use the variable major-mode to find out the current Major Mode. The value is a Symbol, thus you can compare it using eq.

(defun toggle-between-web-mode-and-js2-jsx-mode ()
  (cond ((eq major-mode 'web-mode) (js2-jsx-mode))
        ((eq major-mode 'js2-jsx-mode) (web-mode))))

(interactive) is required to make the function as an Emacs command, so you can bind some key to it.

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.