16

I have the following snippet:

# -*- mode: snippet -*-
# contributor: Song Qiang <tsiangsung@gmail.com>
# key: m
# group: Math
# name: Inline math \( ... \)
# --
\\($1\\)$0

and I want it to be available for both latex-mode and org-mode. How can this be achieved? I could symlink, but I'm trying to find a smarter way.

Dror Atariah
  • 637
  • 6
  • 15
  • 1
    c-mode and c++-mode both share snippets from cc-mode, you can check how they do it. There should a ".yas-parents" file. – bmag Apr 27 '15 at 14:43
  • You can try yankpad, it worth a try ! https://github.com/Kungsgeten/yankpad https://www.youtube.com/watch?v=xkkyE7d0Bpc&feature=youtu.be – ragloo Sep 14 '18 at 05:33

2 Answers2

23

The Yasnippet documentation explains that you will need to include a .yas-parents file with the parent modes:

It's very useful to have certain modes share snippets between themselves. To do this, choose a mode subdirectory and place a .yas-parents containing a whitespace-separated list of other mode names. When you reload those modes become parents of the original mode.

.
|-- c-mode
|   |-- .yas-parents    # contains "cc-mode text-mode"
|   `-- printf
|-- cc-mode
|   |-- for
|   `-- while
|-- java-mode
|   |-- .yas-parents    # contains "cc-mode text-mode"
|   `-- println
`-- text-mode
    |-- email
    `-- time

Hence, you could, for example, put your snippet in a text-mode directory, and then, in the latex-mode and org-mode directories, include a .yas-parents file that include the text text-mode.

BinaryButterfly
  • 291
  • 1
  • 13
Dan
  • 32,584
  • 6
  • 98
  • 168
  • 3
    However, if I have well understood, you can only share **entire** modes, but not **individual** snippets across several modes? – Picaud Vincent Jan 16 '18 at 12:02
1

A few more approaches for sharing snippets between modes:

  1. Let's say you want certain snippets to be shared in both modes: html-mode & css-mode:

    You need to create a separate folder and give it a unique name like html-css-mode where you put the snippets you want to reuse.

    Once that's done you need to require this virtual mode in html-mode & css-mode folders by creating a .yas-parents file with html-css-mode content matching your virtual mode name.

  2. If your snippet belongs to more that one mode or you're want to take a lazy approach - you can put your snippets into fundamental-mode folder which is available in all modes.

Stefan
  • 26,154
  • 3
  • 46
  • 84
BinaryButterfly
  • 291
  • 1
  • 13