6

The Tab keybinding of yasnippet often overwrites other useful keys.

Is there a way to disable Tab binding of Yasnippet to enable other Tab usage?

Lauraducky
  • 674
  • 11
  • 25
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237

4 Answers4

9

These will remove yasnippet's key binding:

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

Should work. Or you can bind tab to another command.

Saddle Point
  • 3,074
  • 4
  • 23
  • 33
  • As to Yasnippet version 0.8.0 running on OSX, `yas-expand` is defined within `yasnippet.el` and `yasnippet.elc` as `read-kbd-macro`, which uses the keyboard bindings defined within each individual snippet. If there are one or more snippets in the particular mode folder within the snippet directory that uses a tab (e.g., `C-I`), that will disable the regular tab key. The classic unset or define key settings placed within the `.emacs` will have no effect. Therefore, each snippet would need to be modified to remove the `C-I` and replaced with something else -- e.g., `# binding: M-/` – lawlist Jun 03 '13 at 15:45
  • 2
    it actually doesnot work, i also tried (setq yas/trigger-key nil) also does not work, i jsut want to disable TAB for yasnippet, why it is so difficult? – shelper Jun 27 '13 at 19:46
8

I'm late for the party but came upon the accepted answer in this question which... didn't work.

Experimented a bit and finally found a solution. Thought I should contribute an answer that does work:

;; It is crucial you first activate yasnippet's global mode.
(yas/global-mode 1)

;; The following is optional.
(define-key yas-minor-mode-map [backtab]     'yas-expand)

;; Strangely, just redefining one of the variations below won't work.
;; All rebinds seem to be needed.
(define-key yas-minor-mode-map [(tab)]        nil)
(define-key yas-minor-mode-map (kbd "TAB")    nil)
(define-key yas-minor-mode-map (kbd "<tab>")  nil)
migdsb
  • 677
  • 1
  • 8
  • 19
  • I suspect it's not crucial that you *activate* yasnippet. The library would need to be *loaded*, however, before you can manipulate its keymaps. `eval-after-load` provides a way to specify these kinds of configurations without having to load the library immediately. – phils Feb 12 '15 at 21:08
3

With use-package:

(use-package yasnippet
  :demand t
  :bind (:map yas-minor-mode-map
         ("TAB" . nil)
         ("<tab>" . nil))
  :config
  (yas-global-mode))
Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
-1
(setq yas-minor-mode-map ;This MUST before (require 'yasnippet)
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "M-i") 'yas-expand)
    (define-key map "\C-c&\C-n" 'yas-new-snippet)
    (define-key map "\C-c&\C-v" 'yas-visit-snippet-file)
    map)) 

(require 'yasnippet)
tumashu
  • 21
  • 1