I am looking for a command I can enter that will make the TAB key input an actual TAB character.

I'm open to anything that is temporary or that will not stomp on TAB mappings that do things other than indenting the cursor (like autocompletion etc). i.e. any or all of these cases would be great to know about:

  1. It can be permanent and affect all major modes as long as it will not affect anything other than indentation of the cursor. (AFAICT, any permanent changes require an edited config file, but maybe I'm wrong about that.)
  2. It can be permanent and affect just the current major mode as long as it will not affect anything other than indentation of the cursor. (Again, maybe permanent change options shouldn't be on this list because I do not want to achieve this through config file editing, but maybe there is another way to make permanent changes that I haven't come across yet.)
  3. It can be temporary and affect all major modes.
  4. It can be temporary and affect only the current major mode.
  5. It can be temporary and affect only the current buffer. (I am unclear on whether an option change like this would persist if I then switch the buffer to a different major mode.)

I have found similar questions here but none that quite hit the mark.

  • This question is almost the same but missing my caveats, and the only answer says it risks clobbering useful TAB mappings. can TAB behave like M-i?
  • This question is different but hits on a great idea, and could even sidestep the useful mapping clobbering issue (if C-q TAB or even M-i could stand-in for TAB for any useful TAB mappings), but it has no answers at all. Translate TAB with C-q TAB
  • This question kind of deals with the global risk issue but the only answer looks like a config file edit.

Side note: I am completely baffled by the way the TAB key works in fundamental mode and in text mode. It tries to indent to align with the start of the next word...OK...but it sometimes skips words, and, much worse, successive indentations end up leaving clusters of space characters amongst tab characters (the space characters are completely useless if they come before tab characters, and they make deleting indentation more difficult since there are extraneous characters to delete).

  • 1
    How about forcing a literal tab with?: C-q TAB
    – lawlist
    Mar 28 '19 at 5:58
  • Or, how about evaluating?: (insert "\t") or (insert 9). To evaluate, type M-: or M-x eval-expression
    – lawlist
    Mar 28 '19 at 6:06
  • @lawlist Thanks for the suggestions. I did come across C-q TAB (and even better, M-i) already, but I really need any character I want to type often to be mapped to a single key press, not a key chord or worse (a multi-step process).
    – A.M.
    Mar 28 '19 at 13:32
  • 1
    You can bind any key to insert one or more characters; e.g., (global-set-key [f5] " ") There is a literal tab between the double-quotes in the example from the previous sentence.
    – lawlist
    Mar 28 '19 at 15:39
  • 1
    Global key bindings are trumped by major-mode key bindings, and major-mode key bindings are trumped by minor-mode key bindings; and, there are some additional ones such as local bindings, text-property / overlay key bindings, etc. Each major/minor mode may have its own keybindings. When a key doesn't behave as expected, type C-h k and see what it is bound to. You may need to set up a few different key binding settings for the various major/minor modes that you use on a daily basis ... Some will be easy, and others may take time to track down how they work and learn how to modify them...
    – lawlist
    Mar 29 '19 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.