Where to put "loose" snippets?


#1

I have many snippets in my snippets.cson file.

For longer snippets, I’d rather create each one in its own file, like complex-header-snippet.cson

I see in the docs that “packages” can do this; is there not a way to have a snippets folder in .atom/packages and just have every valid snippet in there be made available??

This also would make it easier to share with the team.


#2

There are a couple open Issues requesting features around having multiple user-snippets files. I doubt there would be any push back if someone implemented a PR so long as the current snippets.cson was maintained as well.


#3

If sharing snippets with your team is the goal, I’d probably create a snippets-team package that includes all the snippets you’d like to share between your team-members and push it to a git repository. This allows for easy installation for every team member:

cd ~/.atom/packages
git clone https://snippets-team-package-git-url.git

It is also possible to organize your snippets in a separate folder, albeit in a hacky way, by using the internals of the snippets package. If you want stability over performance, consider simply concatenating all snippets into ~/.atom/snippets.cson at start-up, which can be easily done inside your init script. The approach below can potentially break every time Atom gets updated, so be careful!

Example folder structure:

~/.atom/
├─┬ customization/
│ ├─┬ init/
│ │ └── import-snippets.coffee
│ └─┬ snippets/
│   ├── my-snippet-one.cson
│   └── my-snippet-two-cson
└── init.coffee

In ~/.atom/init.coffee:

# Import Snippets
require './customization/init/import-snippets.coffee'

In ~/.atom/customization/init/import-snippets.coffee:

path = require 'path'

# Get a reference to the `snippets` package.
snippets = atom.packages.getLoadedPackage 'snippets'
# Get a reference to the `snippets.js` file inside the `snippets` packages' `lib` folder.
Snippets = require path.join snippets.mainModulePath, '..', 'snippets.js'
# The directory to load the snippets from, e.g. `~/.atom/customization/snippets`.
snippetsDirectory = path.join atom.getConfigDirPath(), 'customization', 'snippets'
# Load all snippets from the `snippetsDirectory` directory.
Snippets.loadSnippetsDirectory snippetsDirectory, (ignoreNull, snippetsInSnippetsDirectory) ->
  for filepath, snippetsBySelector of snippetsInSnippetsDirectory
    Snippets.add filepath, snippetsBySelector

Once you’ve set that up, all the snippets you place inside the ~/.atom/customization/snippets folder will be automatically loaded when you start Atom.