Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Aquamacs. It can distinguish between <tab>, the actual tab key, and TAB, which comes from typing C-i. I want to bind C-i to 'next-line permanently. This works for that:

(global-set-key (kbd "TAB") 'next-line)

But then mode maps override TAB with auto-complete behavior or whatever and I lose my next-line functionality. I could put my binding in the overriding-terminal-local-map, but I'd rather the mode maps remap <tab> so I can still use the function that the mode assigns to tab.

I can manually rebind tab for every mode I use, but I'm hoping there's an easy way to redirect all the TAB mappings to the tab key without messing up C-i.

share|improve this question
up vote 1 down vote accepted

That's a little tricky, but possible. What I do for this exact situation is create a minor-mode, namely gvol-mode, and then in it bind C-i to previous-line (or whatever you want). Then I bind <tab> to the function below.

(defun gvol-indent-for-tab-command ()
  "This is to fix `indent-for-tab-command' for `gvol-mode'.
It runs [tab] or C-i with `gvol-mode' nil because `gvol-mode'
binds C-i to a different command.  Ideally this should take into
account window system so that it can DTRT in a terminal (whatever
the right thing is)."
  (let* ((gvol-mode nil)
         (command (or (key-binding [tab])
                      (key-binding "\C-i"))))
    ;; This is to satisfy `python-indent-line' which checks
    ;; `this-command' to cycle
    (setq this-command 'indent-for-tab-command)
    ;; Make people think this was called with C-i.  This allows
    ;; `self-insert-command' to work
    (setq last-command-event 9)
    (call-interactively command)))

To explain it a little bit, I let bind gvol-mode to nil so that when I do the key lookup my minor mode won't be in play. Thus it will find the binding for <tab> or C-i that would be in place if the minor mode weren't on. Then, in order for some functions to work I have to set this-command to indent-for-tab-command. I also make it appear as if I typed C-i which allows it to work with yasnippet-mode IIRC.

share|improve this answer
This worked great. Thank you! It's especially nice to have those edge-case fixes, I'm sure you saved me a lot of frustration. – mckeed May 24 '12 at 18:38
I was also successful using this method for [return] and C-m. Very helpful. – mckeed May 24 '12 at 19:12
You're welcome, and yes, there was a lot of frustration involved. :-) – Ivan Andrus May 25 '12 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .