14

I would like to use Ctrl + Tab in EMACS for my own use, but Emacs org mode already has this bound. How can I use my own binding instead of the org-mode binding.

In my .emacs file I use:

(global-set-key (kbd "<C-tab>") 'switch-view )

and it works everywhere except in org-mode

elemakil
  • 3,681
  • 28
  • 53
yazz.com
  • 57,320
  • 66
  • 234
  • 385

3 Answers3

26

The key binding you describe is defined in org.el like this:

(org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)

This means that it is only valid in org-mode-map, one of org-mode's local keymaps. The following code adds a hook that is run when org-mode starts. It simply removes that key binding from org-mode-map.

(add-hook 'org-mode-hook
          '(lambda ()
             (define-key org-mode-map [(control tab)] nil)))

Add this code to your .emacs file and then restart emacs.

elemakil
  • 3,681
  • 28
  • 53
paprika
  • 2,424
  • 26
  • 46
  • Where do I add this "add-hook" line? If I could add it to .emacs it would be ideal :) – yazz.com Dec 02 '10 at 10:34
  • I added this hook to my .emacs file and no change also – yazz.com Dec 02 '10 at 10:43
  • I also tried commenting out the ord-defkey in org.el, but no change. I guess there is some sort of precompiling going on so the modified org.el file is ignored – yazz.com Dec 02 '10 at 10:46
  • @Zubair: Yes, you can just add this to your .emacs file. I tried it with Emacs 23.2 with the built-in version of org-mode. You shouldn't need to edit the org-mode sources, but yeah, org.el is ignored if org.elc (compiled Elisp) is in the same directory. – paprika Dec 02 '10 at 11:23
  • You can try out the define-key without the hook. Just start up Emacs, make sure org-mode is loaded (e.g. open an org-file) and paste the following into the *scratch* buffer (without quotes): "(define-key org-mode-map [(control tab)] nil)". Now move the cursor behind the last parenthesis and press C-x C-e. This evaluates the last expression. Try if this has some effect. If it does the it's the hook that's not working. – paprika Dec 02 '10 at 11:28
  • When I enter into the scratch buffer and run "(define-key org-mode-map [(control tab)] nil)" then when I ctrl-TAB from the org-mode I get an error "Arg out of range..." – yazz.com Dec 02 '10 at 11:36
  • So assuming it is the hook that is not working, how can I enable "hooks"? Or do I misunderstand how it works – yazz.com Dec 02 '10 at 11:41
  • Don't bother fixing the hook if evaling define-key doesn't bring the desired effect. As unsetting a key shouldn't have any dramatic effect though I assume that the "arg out of range ..." error is thrown by switch-view. Check the meaning of your key binding in the org-mode buffer by hitting 'C-h k' and then 'C-TAB'. – paprika Dec 02 '10 at 12:04
  • It says: runs the command org-force-cycle-archived, which is an interactive compiled Lisp function in `org.el'. It is bound to . (org-force-cycle-archived) Cycle subtree even if it is archived. – yazz.com Dec 02 '10 at 12:47
  • Thanks !!!! "(define-key org-mode-map [(control tab)] nil)" works now. Yes, the out of range error was in my code... thanks for working me through it! :) – yazz.com Dec 02 '10 at 12:55
  • Using eval-after-load will also work (the code needs to be evaluated only once, not everytime org mode is started). – YoungFrog Jan 13 '14 at 10:46
5

A more robust way to set the keybindings that you want to take effect everywhere regardless of the major mode is to define a global minor mode with a custom keymap.

Minor modes can also have local keymaps; whenever a minor mode is in effect, the definitions in its keymap override both the major mode's local keymap and the global keymap

( http://www.gnu.org/software/emacs/manual/html_node/emacs/Local-Keymaps.html )

That way you don't need to mess with the major mode's local keymap every time you encounter a mode that clobbers your keybinding.

See this Q&A for details:
Globally override key binding in Emacs

Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
2

This doesn't work because, as you said, org-mode uses its own keybinding for C-TAB. In other words, even if you define a global keybinding, as soon as you invoke org-mode, it will overwrite that binding with its local keybindings.

What you can do, however, is add a callback function that is invoked whenever you start org-mode, and in that callback function you reset C-TAB to invoke switch-view:

(add-hook 'org-mode-hook (lambda () (local-set-key [(control tab)] 'switch-view)))

Put the above line in your .emacs file and next time you start a new Emacs you should be good to go.

elemakil
  • 3,681
  • 28
  • 53
Thomas
  • 17,016
  • 4
  • 46
  • 70