[SOLVED] Insert text snippet


#1

Hi guys,

Day one with Atom. Am trying to get it to insert some custom snippets when I press a particular key combo.

Have added this to keyman.cson …

'.editor:not(.mini)':
  'ctrl-alt-shift-F': 'editor:insert-fn-snippet'

… and this to init.coffee:

atom.commands.add 'atom-text-editor',
  'custom:insert-fn-snippet': ->
    atom.workspace.getActiveEditor()?.insertText('function(){}')

Have opened the Keybinding Resolver and when I hit the combo it shows my new function in green with a tick next to it. But nothing shows up in the editor :confused:

Have restarted Atom but no joy. Can anyone see where I’m going wrong?

Thanks


#2

getActiveEditor is a function that is no longer part of the Atom API. You can replace it with getActiveTextEditor and it should work.


#3

Hi @leedohm, thanks for your reply. I have updated that call as you suggested but sadly the same problem persists. Do you know anything else I can try? Thanks, Mike


#4

Your keybinding selector should be atom-text-editor:not([mini]).


#5

Hi Lee, Thanks for your reply - I’ve made that change too but still seeing nothing when I hit the combo. Are there any particular preferences I need to set for these custom keybindings to work? I’ve tried a few other examples I’ve found on here and on StackOverflow and none of them work. Feels like I’m missing something fundamental …


#6

Which examples?

Feels like I’m missing something fundamental …

It would be helpful if you shared your whole init.coffee in a gist or similar service.


#7

Hi DamnedScholar, thanks for your reply.

My init.coffee has nothing in it except the three lines included in my original post:

atom.commands.add 'atom-text-editor',
  'custom:insert-fn-snippet': ->
    atom.workspace.getActiveEditor()?.insertText('function(){}')

I tried the following example from StackOverflow but it doesnt work for me: http://stackoverflow.com/questions/24456995/how-do-i-write-a-custom-command-in-atom

The result is the same as with my orignal post: as I press the key combination the Keybinding Resolver shows my new function name in green with a tick next to it, but nothing actually happens.

Any suggestions are welcome.
Cheers,

Mike


#8

@leedohm told you that getActiveEditor() should be replaced with getActiveTextEditor(), and you said that you had done that. Which is it? You also have a colon after the command name when you need a comma. The question mark is also unnecessary. Try

atom.commands.add 'atom-text-editor',
  'custom:insert-fn-snippet', ->
    atom.workspace.getActiveTextEditor().insertText('function(){}')

#9

@DamnedScholar Thanks for your reply. Apologies for any confusion - I have tried both getActiveEditor() and getActiveTextEditor() - neither work. Thank you also for pointing out the issue with the comma & question mark. I have replaced the contents of my init.coffee with your code block, but the same problem persists.


#10

And your keymap.cson? Does it invoke editor:insert-fn-snippet or custom:insert-fn-snippet? Because that’s important.


#11

It was invoking editor:insert-fn-snippet as per my original post, however I’ve just changed to custom:insert-fn-snippet and, lo and behold, it works! Thank you, glad we got there in the end :slight_smile: