问题
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)
回答1:
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.
回答2:
In addition to what others have said:
The
TAB
key is typically not the same as<tab>
, which is what you are using by specifying[tab]
. TheTAB
key is the same asC-i
, and you can use(kbd "TAB")
to bind it.You can, if you want to, go ahead and bind
TAB
to your command in theglobal-map
, as you have done, but then override that for the minibuffer by rebinding it in each of the minibuffer keymaps to whatever commands you like for them.For example, if you want the usual minibuffer bindings for
TAB
, then do this:Start Emacs using
emacs -Q
(no init file).Optionally load any library that makes a non-default minibuffer binding for
TAB
(e.g. autocomplete?).Load library help-fns+.el, to obtain command
describe-keymap
, bound globally toC-h M-k
.Use
C-h M-k
to check the binding ofTAB
in each of the minibuffer keymaps used by your version of Emacs. This will includeminibuffer-local-map
,minibuffer-local-completion-map
, andminibuffer-local-must-match-map
, and it may include some more. See(elisp) Completion Commands
for the list of minibuffer keymap variables.For example,
C-h M-k minibuffer-local-completion-map
shows you the bindings in that keymap. Look forTAB
in the list.(If you don't want to download
help-fns+.el
then you can uselookup-key
. That library just makes listing the keys in a keymap easy.)Bind
TAB
to that default binding, in your init file, after having bound it globally to your non minibuffer-oriented command. IOW, restore the minibuffer bindings as they should be.
回答3:
You can check whether you are in a minibuffer by using the function window-minibuffer-p
. From the documentation
Return non-nil if WINDOW is a minibuffer window. WINDOW must be a valid window and defaults to the selected one.
Also emacs 24.4 already provides the things you want to achieve in the functions above. The command indent-rigidly
has been enhanced such that you can indent the regions using left, right, tab and shift-tab keys.
You can select the region you want to indent and hit C-xtab after which you will be able to move the region by one space using the right and left keys. You can also move the selected region by one tab space using the tab and shift-tab keys, it is really convenient since the keybindings above remain active until you press any other key.
来源:https://stackoverflow.com/questions/22522398/emacs-keybinding-to-tab-breaks-autocompletion-in-minibuffer