Using snippets in keymap


#1

Hi everyone!

I’m trying to switch to Atom from years of Sublime Text 2/3. In Sublime, I had a shortcut for SCSS grammar — “+ tab” that added a certain snippet. I managed to set up it the same in Atom, however, in an ugly way, by changing both the keymap and the init script.

So my question is, is there a way to add a snippet directly into the keymap? Or, even more preferably, to call a snippet from snippets.cson by its prefix for current grammar.


Right now, first, I add a new command to īnsert the snippet in init.coffee:

atom.commands.add 'atom-text-editor', 'editor:insert-include-snippet', ->
  snippetBody = "@include $1;$0"
  atom.packages.activePackages.snippets?.mainModule?.insert snippetBody

And I have it added to my keymap:

'atom-text-editor[data-grammar="source css scss"]':
  '+ tab': 'editor:insert-include-snippet'

But I feel like this is way more code than should be necessary (specifically in the init script).


#2

You can define the snippet in ~/.atom/snippets.cson:

# Sass Snippets
'.source.css.scss':
  # Include Snippet
  'Include':
    'prefix': '+'
    'body': '@include $1;$0'

Then whenever you write a + inside a Sass file you can hit tab to expand it to your include statement.


#3

Ah, that was the first thing I tried, However, the “+” symbol doesn’t expand when I hit Tab, instead the characters type out as usual. I’m running near-stock Atom 1.0.7 if it helps.

Thanks!


#4

Oops, my selector was wrong, I forgot the leading dot. I updated the snippet, does that work for you?


#5

Yes, that fixed it! I could’ve sworn I had the dot when I first tried it.
But it works now, thanks!