emacsautocompleteelispminibuffer

Emacs: Keybinding to TAB breaks autocompletion in minibuffer


Simply put, I just set a keybinding on the TAB key, but now when I push TAB in the minibuffer to auto-complete a command, it fails with the following message: The mark is not set now, so there is no region.

In other words, I only need my TAB keybinding when my cursor is in the buffer (not the minibuffer).

In my example below, how can I set my tab to indent when I am in text/fundamental mode in the buffer without losing autocompletion while in the mini-buffer? I have the following functions and key-bindings:

;; Shift the selected region right if distance is postive, left if
;; negative

 (defun shift-region (distance)
   (let ((mark (mark)))
     (save-excursion
       (indent-rigidly (region-beginning) (region-end) distance)
       (push-mark mark t t)
       ;; Tell the command loop not to deactivate the mark
       ;; for transient mark mode
       (setq deactivate-mark nil))))

(defun shift-right ()
  (interactive)
  (shift-region 2))

(defun shift-left ()
  (interactive)
  (shift-region -2))

;; Bind (shift-right) and (shift-left) function to your favorite keys. I use
;; the following so that Ctrl-Shift-Right Arrow moves selected text one 
;; column to the right, Ctrl-Shift-Left Arrow moves selected text one
;; column to the left:

;; (global-set-key [C-S-right] 'shift-right)
;; (global-set-key [C-S-left] 'shift-left)
(global-set-key [tab] 'shift-right)
(global-set-key [backtab] 'shift-left)

Solution

  • The problem is simply that you bound your command to [tab] rather than to "\t". tab denotes the TAB key under GUIs, but under a tty Emacs instead receives a TAB character (i.e. ?\t), so when you hit tab Emacs first looks for a tab binding and if there isn't any, a function-key-map remapping turns it into a ?\t and tries again. The minibuffer only binds "\t", so any global binding to [tab] will take precedence.

    In short, use (global-set-key "\t" 'shift-right) and this problem will disappear.