shyamupa
shyamupa

Reputation: 1628

How to define keybinding to toggle between minor modes in emacs?

EDIT: From the comments, I came to know that org-mode is not a minor mode. So this question is not relevant to org-mode. But it will be useful if someone wants to toggle b/w minor modes in emacs.

I have to switch to org-mode often while operating on a buffer, and am too lazy to type M-x org-mode all the time.

Is there a way I can specify a keybinding in my init.el file to toggle the mode for the buffer? I want to use something like F12

Upvotes: 1

Views: 599

Answers (2)

user355252
user355252

Reputation:

You can use something similar to this

(defvar previous-major-mode nil)
(make-variable-buffer-local 'previous-major-mode)
(put 'previous-major-mode 'permanent-local t)

(defun toggle-org-mode ()
  (interactive)
  (cond
   (previous-major-mode
    (call-interactively previous-major-mode)
    (setq previous-major-mode nil))
   (t
    (setq previous-major-mode major-mode)
    (call-interactively 'org-mode))))

(global-set-key (kbd "<f12>") #'toggle-org-mode)

But if you need this frequently, it's a good indication that you are doing something wrong, and probably misunderstood the concepts of major modes.

Upvotes: 2

gsg
gsg

Reputation: 9377

(global-set-key (kbd "<f12>") 'org-mode) should do the trick.

Upvotes: 0

Related Questions