Skip to main content
21 votes
Accepted

Insert current date with yasnippet

You should rewrite your snippet so that the backquoted expression doesn't modify the buffer, but instead returns the string to insert: # -*- coding: utf-8 -*- # name: date # key: date # -- `(format-...
npostavs's user avatar
  • 9,223
15 votes
Accepted

Warning (yasnippet): modified buffer in a backquote expression

warning-suppress-types is a list defined in the warnings package. You first need to load this package with: (require 'warnings) and after this, add the warning using add-to-list: (add-to-list '...
Gastove's user avatar
  • 1,561
10 votes
Accepted

Yasnippets: loading two major modes (`org-mode` and `LaTeX`)

You can load an additional modes via yas-activate-extra-mode in the hook of the major mode (it also works with yas-global-mode turned on). Just copy-paste following elisp snippet into your init file: ...
Tobias's user avatar
  • 33.4k
9 votes
Accepted

Is it possible to make yasnippets to expand snippets inside an org-mode babel section in the appropriate mode?

As of 2017, Jan 22, if you set org-src-tab-acts-natively and org-src-fontify-natively, then TAB in source blocks will expand snippets of the block's language. You may want to set yas-buffer-local-...
npostavs's user avatar
  • 9,223
9 votes
Accepted

Run function after specific snippet

You can make snippet-specific bindings with # expand-env: # -*- mode: snippet -*- # name: ipdb # key: ipdb # expand-env: ((yas-after-exit-snippet-hook #'bm-toggle)) # -- import ipdb; ipdb.set_trace() ...
npostavs's user avatar
  • 9,223
8 votes

How to put whitespace into Yasnippet Templates?

Use the expand-env directive, as described in the manual. E.g. # expand-env: ((yas-indent-line 'fixed) (yas-wrap-around-region 'nil))
Tianxiang Xiong's user avatar
8 votes
Accepted

Insert code chunk in R Markdown with yasnippet and polymode

I ran into a similar issue, and wrote a stand-alone function to get around the weird interactions between yasnippet and polymode. The following function mimics the behaviour of the snippet you tried, ...
Tyler's user avatar
  • 22.4k
8 votes
Accepted

Multiple occurrences of variable in yasnippet snippet

Try this snippet: # -*- mode: snippet -*- # name: cout variable # key: cvar # -- cout << "${1:var} = " << $1 << endl;$0 I believe the main problem is that yasnippet doesn't ...
Xaldew's user avatar
  • 1,261
7 votes

yasnippets - finding and modifying some snippets

Considering your configuration, using elpa, they are located in ~/.emacs.d/elpa/yasnippet-20160801.1142/snippets. Your custom snippets should be at ~/.emacs.d/snippets so they don't get overwritten ...
marcanuy's user avatar
  • 818
7 votes
Accepted

What's the best way to remove some of the builtin yasnippet and make emacs upgradable?

Simply change the value of the variable yas-snippet-dirs, which by default includes yas-installed-snippets-dir (pointing to the builtin snippets - those that you want ignored/disabled). For example: (...
VanLaser's user avatar
  • 576
7 votes

How to insert leading spaces/tabs in yasnippet?

Look for expand-env: ((yas-indent-line 'fixed)): # -*- mode: snippet -*- # name: Example for Indent # key: example # expand-env: ((yas-indent-line 'fixed)) # -- example $0
Maxim Kim's user avatar
  • 1,766
6 votes
Accepted

Expand yasnippet only when it's at beginning of the line

Say your snippet has the key mysnippet_, you can use the following condition: # condition: (looking-back "^mysnippet_" nil) Speaking of use cases, it could make sense to have snippets for inserting ...
Timm's user avatar
  • 1,609
5 votes
Accepted

yasnippet truncates clipboard contents

You should use current-kill instead of clipboard-yank because the latter works by side-effect. # -*- mode: snippet -*- # name: footnote # key: fn # -- [fn:: `(current-kill 0)`] I'm actually not ...
npostavs's user avatar
  • 9,223
5 votes

yasnippets - finding and modifying some snippets

In your buffer, M-x yas-visit-snippet-file, then type the name of the snippet you want to edit, or choose from a prompted list. Then it will open a buffer for you to edit. From the buffer you can M-: ...
Jason's user avatar
  • 171
5 votes
Accepted

Org subtree from yasnippet

You can determine the current level within embedded Emacs-lisp code. # -*- mode: snippet -*- # name: org subtree # key: ost # -- `(make-string (org-current-level) ?*)` $1 #+BEGIN_SRC latex \...
Kyle Meyer's user avatar
  • 7,094
4 votes

yasnippet : How to expand snippet inside snippet?

you need to add this to your yasnippet config in .emacs to enable nested triggering of snippets (setq yas-triggers-in-field t)
Chakravarthy Raghunandan's user avatar
4 votes

Nested Snippets

From This SO answer, we see that: yas-triggers-in-field is a variable defined in yasnippet.el. Its value is t Original value was nil Documentation: If non-nil, allow stacked ...
PythonNut's user avatar
  • 10.4k
4 votes
Accepted

Yasnippet including backslash

The backslash is used in regular expressions to escape individual characters: For the most part, \ followed by any character matches only that character. So \( gives you ( in your output. To get ...
Dan's user avatar
  • 33.2k
4 votes

Can you setup a snippet with two choices, where the second choice depends on the first choice?

Here is how I was able to achieve this goal. It is basically like writing an elisp function that gets the information you want, and then constructs the string to put in the buffer. I adapted the idea ...
John Kitchin's user avatar
  • 11.9k
4 votes
Accepted

Yasnippets for active web-mode engine

The right place to use yas-activate-extra-mode is web-mode-on-engine-setted since that function also sets the engine specific snippets of web-mode. Pityingly there is no hook so we have to add an ...
Tobias's user avatar
  • 33.4k
4 votes
Accepted

Is it possible to repeat the last org-mode source block's headers

You can set header arguments in Org mode properties (see (org) Using Header Arguments) to avoid repeating, for example, all code blocks in this org file will have :dir /tmp #+PROPERTY: header-args :...
xuchunyang's user avatar
  • 14.6k
3 votes
Accepted

How to temporary disable yassnipet snippet in snippet

In most of the use cases on $1 I enter err which expands to another snippet. Then you might want to instead use ${1:err} = errors.WithMessage($1, $2))$0 That way you don't even have to type the err ...
npostavs's user avatar
  • 9,223
3 votes
Accepted

Yasnippets won't expand in git-commit-mode?

git-commit-mode is a minor-mode but you have limited the snippets to be used with a major-mode git-commit-mode. Such a major-mode no longer exists, so you have to use a different mechanism to restrict ...
tarsius's user avatar
  • 25.8k
3 votes
Accepted

How to cycle back through parameters on a yasnippet?

Am I missing something or there's a key binding to go back?. S-<tab> should be bound to yas-prev-field (in yas-keymap) which takes you back. Note that it had a bug until recently, where it ...
npostavs's user avatar
  • 9,223
3 votes
Accepted

I cannot type left open bracket into buffer

EDIT and final answer: Okay, so thanks to the @npostavs (can't thank you enough :D) I can finally tell the real problem was me not understanding the use-package binding syntax, we can have either ("C-...
tlegutko's user avatar
  • 356
3 votes
Accepted

How to limit when yasnippet fires?

The yas-expand-only-for-last-commands variable specifically takes a list of functions, not just a function, so to properly set it, the correct form is this: (setq yas-expand-only-for-last-commands '(...
Lucky1313's user avatar
3 votes
Accepted

How can I define a Yasnippet for #if AND if?

The main issue here is understanding the priorities yasnippet uses to determine what snippet it should expand. If I understand the documentation correctly, this is controlled by the yas-key-syntaxes ...
Xaldew's user avatar
  • 1,261
3 votes
Accepted

Quick/electric text expansion

In Org, you're looking for (org) Easy Templates; try typing <sTAB: With just a few keystrokes, Org’s easy templates inserts empty pairs of structural elements, such as ‘#+BEGIN_SRC’ and ‘#+END_SRC’...
Basil's user avatar
  • 12.5k
3 votes
Accepted

Yasnippet: How to automatically advance to next field after `yas-choose-value`

For the example in your question, you don't need a to make field, using a backquoted expression should suffice: <div align="`(yas-choose-value '("right" "center" "left"))`"> $0 </div> ...
npostavs's user avatar
  • 9,223
3 votes

Run function after specific snippet

Solution 1 (add-hook 'yas-after-exit-snippet-hook (lambda () (if (buffer-substring yas-snippet-beg yas-snippet-end) (bm-toggle)))) The yas-after-exit-snippet-hook ...
Lorem Ipsum's user avatar
  • 4,527

Only top scored, non community-wiki answers of a minimum length are eligible