开发者

Override Ctrl-TAB in EMACS org.mode

I would lik开发者_如何学Goe 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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜