Mac – How to send a literal tab to bash in emacs’ shell-mode

emacs

I want to bypass comint-mode's completion support completely, instead relying on the subordinate process to do it for me. Specifically, if I'm running:

  • emacs
    • shell-mode
      • bash

Then I want TAB to be passed to the bash process and expanded by it.

If I'm running:

  • emacs
    • shell-mode
      • bash
        • psql

Then I'd want TAB to be handled by psql.

I've tried this in a shell-mode-hook to no avail:

(define-key shell-mode-map "\t" 'self-insert-command)

When this is set, the TAB key inserts a literal tab on the command line, which is not at all useful to me.

I've also tried this, but when I hit TAB nothing happens:

(defun cr/comint-send-tab ()
  "Send a tab character to the current buffer's process"
  (interactive)
  (comint-send-input t t)
  (process-send-string (current-buffer) "\t"))

(define-key shell-mode-map "\t" 'cr/comint-send-tab)

How can I do this?

Best Answer

Emacs shell-mode buffers are not terminals (i.e., they do not use ptys (pseudo-terminals)), so no program running in such a buffer (the shell, the programs run by the shell, etc.) can perform character-at-a-time input. Each line is typed in full and only sent when ENTER is pressed. To see proof, run the tty command in a shell-mode buffer, and it's output will be not a tty.

Related Question