How to cmd+b to string


#1

Hi. I would comment that do not speak English properly, I hope you understand me .

I would create a very simple Snippet . I would simply press cmd + b on a text string and added the html tag:

Hello World
to:
<strong>Hello World</strong>

I’ve been trying through init.coffee and keymap.cson but I can not write the current text selection :

Init.coffee:

atom.commands.add 'atom-workspace', 'editor:insert-tag-snippet', ->
  selection = editor.getSelectedText() #Get string??
  snippetBody = '<${0:strong}>string here!!!!!!!!</${1:strong}>'
  atom.packages.activePackages.snippets?.mainModule?.insert snippetBody

Keymap.cson:

'atom-workspace atom-text-editor:not([mini])':
  'alt-enter': 'editor:insert-tag-snippet'

I hope I explained correctly. Thank you very much.


Creating a keybinding for bold <strong> and italics <em>
#2

You can’t do what you’re suggesting with a snippet. Snippets are triggered by typing some text and then pressing the expansion key, typically Tab. Keybindings like Cmd+B trigger commands. So if you wanted to make Cmd+B wrap selected text in <strong> tags, you could do something like this in your init.coffee:

atom.commands.add 'atom-workspace',
  'custom:wrap-with-strong': ->
    editor = atom.workspace.getActiveTextEditor()
    for selection in editor.getSelections()
      textToWrap = selection.getText()
      selection.insertText("<strong>#{textToWrap}</strong>")

Then you could use this in your keymap.cson:

'atom-workspace':
  'cmd-b': 'custom:wrap-with-strong'

#3

Hi!

Just perfect. It’s just what I needed. Thanks for the help. ; )

regards!