2

TAB has multiple key bindings in different conditions. I want to change the key binding for yas-expand from TAB to M-TAB. How can I do it? Basically I want TAB to run indent-for-tab-command in all conditions.

TAB runs the command indent-for-tab-command (found in global-map), which is an
interactive compiled Lisp function in ‘indent.el’.

TAB runs the command yas-expand (found in yas-minor-mode-map), which is an
alias
for ‘yas-expand-from-trigger-key’ in ‘yasnippet.el’.

It is bound to TAB, <tab>, <menu-bar> <YASnippet> <Expand trigger>.

my setup:

(defvar my-keys-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (global-set-key (kbd "<tab>") 'indent-for-tab-command)

3
  • 2
    Have a look at the relevant portion in the source code as of 06/17/2021 (lines 679 and 680): github.com/joaotavora/yasnippet/blob/master/yasnippet.el#L679 After the yasnippet.el library loads, you can try removing those definitions with something like this: (define-key yas-minor-mode-map [(tab)] nil) and (define-key yas-minor-mode-map (kbd "TAB") nil) Then define a new keyboard assignment binding the variable yas-maybe-expand to your proposed M-TAB using the yas-minor-mode-map. I do not have an opportunity at the moment, however, to test that out ...
    – lawlist
    Jun 17, 2021 at 19:26
  • 1
    ... continued ... I see that you will likely also have to modify the variable yas-keymap which is used while the snippet is expanding ... see the source code as of 06/17/2021 (line 423): github.com/joaotavora/yasnippet/blob/master/yasnippet.el#L423 There are a few ways to do that ....
    – lawlist
    Jun 17, 2021 at 19:29
  • I have added (define-key yas-minor-mode-map [(tab)] nil); (define-key yas-minor-mode-map (kbd "TAB") nil); (define-key yas-minor-mode-map (kbd "M-TAB") 'yas-expand) and seems like it works but I have not done anything related to your second comments. Please have look when you have time
    – alper
    Jun 18, 2021 at 8:37

1 Answer 1

1

Per this question you can use:

(define-key yas-minor-mode-map [(tab)] nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)

This worked for me (using use-package) with:

(use-package yasnippet
  :ensure
  :bind
  (:map yas-minor-mode-map
        ("C-'". yas-expand)
        ([(tab)] . nil)
        ("TAB" . nil))
  :config
  (yas-reload-all)
  (add-hook 'prog-mode-hook 'yas-minor-mode))

I remapped yas-expand to C-'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.