Adding Custom Snippets To Yasnippet
Overview
When using Yasnippets (a popular template system for Emacs), you will usually need to add your custom snippets to easy the task of writing repetitive content.
To extend the snippets, there is a special snippet template that acts as a template in a similar way that yasnippets handle other templates.
Writing a snippet
Snippet directory
The directory for storing snippets is defined by the variable
yas-snippet-dirs
and by default is ~/.emacs.d/snippets
.
C-v yas-snippet-dirs
.Creating the snippet
To create the snippet file execute the command yas-new-snippet
(C-c
& C-n),
yasnippets internally expands the snippet-writing
snippet.
A new buffer will open with the base model to define the new snippet in the snippet-mode major mode.
# -*- mode: snippet -*-
# name:
# key:
# --
Example
The following is an example of the current for
statement in
Python:
# name: for ... in ... : ...
# key: for
# group : control structure
# --
for ${var} in ${collection}:
$0
Saving the snippet
The snippet will be saved in the default snippets directory and inside
the current major-mode subdirectory. For example, if we attempt to
save the previous python snippet it will be located in
.emacs.d/snippets/python-mode
.
Testing the snippet
Once saved, the snippet can be tested with the command M-x yas-tryout-snippet which will test the current snippet buffer template in other buffer with the proper major mode.
To load the new snippet just use M-x yas-load-snippet-buffer
and it
will be available in other buffers.
Notes
If you are extending the snippets definitions with something that would be useful for others, consider making a pull request to the official snippets repo.
Reference
- Using Emacs As A Python Ide with basic Emacs featuresJuly 10, 2017
- Jump To Function Definitions In EmacsJune 23, 2017
- Writing Alternating Between English And Spanish In EmacsJune 17, 2017
- Writing In Emacs Checking Spelling Style And GrammarJune 10, 2017
- Adding Custom Snippets To Yasnippet
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·