1

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).

A.M.
  • 111
  • 3
  • 1
    How about forcing a literal tab with?: C-q TAB – lawlist Mar 28 '19 at 05:58
  • Or, how about evaluating?: (insert "\t") or (insert 9). To evaluate, type M-: or M-x eval-expression – lawlist Mar 28 '19 at 06: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
  • maybe you need to sacrifice something. I permanently bound (global-set-key (kbd "S-<tab>") 'tab-to-tab-stop) in my config file. This means press shift and tab to insert a real tab – jue Mar 28 '19 at 20:11
  • This is likely a duplicate question, but I don't have time now to check. Perhaps someone else can... – Drew Mar 28 '19 at 22:07
  • @lawlist I have read about a number of *Emacs variables whose values can be changed to modify behaviour (per session I think), and I have been trying to find one with a setting to just let the tab key tab. It would be incredible* if there really isn't one, and I've come across all kinds of suggestions for *Emacs Lisp code to paste in the config file*, but I mentioned in my question that I don't want to go that route at this point. I've been ignoring all Elisp code until now, but I just clued in to your 2nd comment meaning Elisp code isn't just for config files. So...var or code? – A.M. Mar 29 '19 at 03:19
  • So if there is no appropriate var (again, incredible if true), but I can use code that would affect only the current session, then... *goes away and reads about key binding for an hour* ...It looks like I could use global-set-key to affect all major modes, or local-set-key to affect just the current major mode, or define-key text-mode-map (to target text mode, which is where I'm seeing the problem right now). ...and for the input I can use [tab], (kbd "TAB"), or (kbd ""). ...and for the output I can use " " (quoted tab char), 'tab-to-tab-stop, or 'self-insert-command – A.M. Mar 29 '19 at 04:15
  • I also just found non-Elisp approaches: M-x local-set-key self-insert-command ...and... M-x global-set-key self-insert-command – A.M. Mar 29 '19 at 04:18
  • If someone wants to answer with the things in my last two comments (assuming there is no easier answer that involves just setting some variable, and assuming all the approaches in my comments cause just temporary changes, I would accept that answer. Otherwise I'll put the answer up myself after a few days. – A.M. Mar 29 '19 at 04:24
  • (If you do answer, for future readers please spell out what I had somehow missed even after lots of reading about Emacs: Elisp code is not just for config files! It can be executed live in Emacs instead (with M-: or M-x eval-expression), and in that case it has temporary effects. I would not be surprised if trying to fix the strange [Tab] button behaviour is one of the first things some new (potential) Emacs users try to do, so visitors to this question might need to hear that basic info. I did! Thanks @lawlist.) – A.M. Mar 29 '19 at 04:38
  • 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 05:08
  • @lawlist Good to know. Thanks. That binding precedence would be a great thing to add to an answer here. Again, I'd rather have rep go to an answerer, but it's important that people who get to this page see an answer, so I'll write it up myself if no-one posts the answer here soon... – A.M. Apr 02 '19 at 11:41

0 Answers0