2

I am using Python but some pre-defineted snippets are acting not as I wanted, hence I want to remove some, if possible.

In most cases, I am using TAB for indent alignment, hence yasnippets apply themselve, which is pretty annoying.

For example:

In a Python file writing log and press TAB, emacs write followings code piece, which I want to disable:

logger = logging.getLogger("name")
        logger.setLevel(logging.level)

my setup:

(add-hook 'python-mode-hook 'lsp)
(add-hook 'python-mode-hook #'lsp-deferred)

# taken from: https://github.com/joaotavora/yasnippet
(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)

Hide Yas Snippet Dirs: [Value Menu] List of directories:
[INS] [DEL] Choice: [Value Menu] Directory: ~/.emacs.d/snippets
[INS] [DEL] Choice: [Value Menu] Variable: yasnippet-snippets-dir
[INS] [DEL] Choice: [Value Menu] Variable: yasnippet-classic-snippets-dir

Please note that when I try to create a new snippet it generates it under /home/user/etc/yasnippet/snippets/python-mode/ instead of ~/.emacs.d/snippets.

Default snippets are located under /usr/share/yasnippet-snippets/python-mode; but since they require permission would it be a good idea to remove the ones that I don't want to use?


For example I want to remove following snippet keys for:

__str__, str, size, def, self, __


Basically,I just want to remove all key snippets for the given key. For example, when I type str and press TAB emacs says:

Possible completions are:
__str__                              str
. Choose a snippet:

where I don't want to see neither of it and completely remove all keys for the str key.

2
  • Snippets can be stored in a file. The top section of the snippet contains items such # key: ...; # binding: ... See the tutorial: joaotavora.github.io/yasnippet/snippet-development.html If you don't like the key that triggers the snippet, then remove the entire line, or simply change the key to something you like better ... I'm sure there is a way to reload the snippets on the fly after manually modifying them, but it is probably easier to just turn on/off the minor-mode, or restart Emacs. Keep in mind that snippets can be inherited from another major-mode snippet folder.
    – lawlist
    Commented Jun 7, 2021 at 21:51
  • I am trying to remove file that has # key: str but I couldn't where the main file for it to change @lawlist
    – alper
    Commented Jun 14, 2021 at 16:01

2 Answers 2

2
+50

It is not a good idea to remove a snippet from their place - at first update of yasnippet-snippet package, it will be again present in your Emacs.

So, a simpler method is to create new or modify/rename/save to the default yas-snippet-dir, which is ~/.emacs.d/snippets folder (if you did not modify it already). But to be organised, add here a new folder python-mode where the new python snippets will be placed.

To modify an existing snippet and to place it in the default folder created, visit it and then use the command save-as, bound to C-x C-w instead the usual save command C-x C-s. The “save-as” will allow you to choose destination folder.

The last thing to do is to take care to load all snippets (the original ones + yours) after python-mode is loaded - so add the line (yas-reload-all) after python mode is configured.

8
  • I have created ~/.emacs.d/snippets/python-mode,((setq yas-snippet-dirs "~/.emacs.d/snippets/")) added few snippet files in it and added (yas-reload-all) to the end of the .emacs file. But snippets are not fetched detected within the python-mode.
    – alper
    Commented Jun 9, 2021 at 12:25
  • I had to do C-h v yas-snippet-dir customize-variable -> in order to re-set its path without having a problem
    – alper
    Commented Jun 9, 2021 at 12:45
  • For example for __str__ and str I was not able to remove both of them and I not not sure where does __str__ comes from.
    – alper
    Commented Jun 14, 2021 at 15:59
  • The __str__ comes from group dunder methods (google for "dunder methods") - visit the snippet file with C-c & C-v. Seems to be a name collision between str object from lsp and the key of __str__. You may change the key for dunder method and save it in other place.
    – Ian
    Commented Jun 15, 2021 at 11:29
  • Its pretty difficult to code when there is random snippets apply themselves. I have look into all files under ~/.emacs.d/ I haven't able to find key for __str__ any where. Please see my updated question for the directory paths.
    – alper
    Commented Jun 15, 2021 at 12:23
2

It might be easiest to remove the template in your init file after yasnippets is loaded. That way you don't have to modify the system directories. I think this should do it:

(yas--remove-template-by-uuid (yas--table-get-create 'python-mode) "logging")
4
  • This solution did not work on my end :-( Still snippets under /usr/share/yasnippet-snippets/python-mode takes place when I try to remve them for example adding: (yas--remove-template-by-uuid (yas--table-get-create 'python-mode) "f")
    – alper
    Commented Jun 10, 2021 at 13:21
  • This doesn’t remove the actual snippet file, but after you run it I think that snippet should stop expanding which is what you want right? Commented Jun 10, 2021 at 14:23
  • Yes sir I was hoping to achieve that but it still exands after I added the yas--remove-template-by-uuidline
    – alper
    Commented Jun 10, 2021 at 17:03
  • It might be the template name you need to use to remove it, and not the shortcut. Commented Jun 10, 2021 at 18:26

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.