How to create Key Binding for inserting text in editor


#1

For example:

ctrl+f+u should insert string "function " (with space at the end)
ctrl+s should insert string “ś” (polish letter)

I would ask for detailed step-by-step explanation.


#2

Want to take a stab at this @olmokramer?


#3

I do not understand.


#4

I’m asking another person who frequents the forum named @olmokramer, who specializes in init.coffee modifications, to answer your question.


#5

That would be very nice.


#6

Follow these instructions after changing them to suit your needs:

  1. Add this to your init.coffee

    atom.commands.add 'atom-text-editor',
      'custom:insert-foo': ->
        atom.workspace.getActiveTextEditor()?.insertText('foo')
    
  2. Add this to your keymap.cson

    'atom-text-editor':
      'ctrl-shift-alt-z': 'custom:insert-foo'
    
  3. Restart Atom (to load the init.coffee again)

The text above has the following replaceable parts:

  • custom:insert-foo is the command name, it can be changed to any string you want, though convention is package name and command text separated by a colon
  • foo (on the third line of step 1) is the text that is inserted
  • ctrl-shift-alt-z is the key combination to execute the command. It can be replaced with whatever key combination you want.

Also keep in mind that the keymap.cson (like other CSON files) follows certain rules that mean duplicate keys don’t work.


#7

Yes @leedohm I would do exactly that :slight_smile:


#8

Thanks, it worked.


#9

Doesn’t work for me :frowning: It just inserts my text in sime random place when I start Atom.
I added

'atom-text-editor':
  'ctrl-shift-j': 'custom:insert-on-tap'

and

atom.commands.add 'atom-text-editor',
  'custom:insert-on-tap': ->
  atom.workspace.getActiveTextEditor()?.insertText(" = function(el) {el.addEventListener('transitionend', function(e) {});}")

#10

In CoffeeScript, leading whitespace is significant. The third line in your second code block should be indented 2 more spaces.


#11

Thanks, now it works!