Wrap Selected Text - What's the best way?


#1

I’d like to create a few key mappings to wrap selected text with various character strings, html comments, for instance.

What’s the best way to arrange for this?

I’ve looked for ways to use selected text in snippets, and to then trigger the snippets with key maps, but I’m not getting far with that.

It seems a simple thing. Could someone point me in the right direction?

Thanks


#2

Check out the autoflow package and its Reflow Selection command.


#3

That’s not what he wants, @leedohm. He wants to turn x into <foo>x</foo> or into <!-- x --> or something like that.

I had the same misunderstanding at first :smile:


#4

That’ll teach me to respond between meetings again :laughing: One of these days I’ll learn!

Thanks @kgrossjo


#5

It seems https://atom.io/packages/vim-surround does part of this. But somehow there must be an easy solution…


#6

Wrapping things with comments can be done with the Comment Selection command. But that doesn’t solve the general case. When I get home from work tonight if nobody has solved this one, I’ll probably build an init.coffee script that can be used for this kind of thing.


#7

Thanks for getting involved, guys. I appreciate it,

kgrossjo has it mostly right. In the example of <foo>x</foo>:

If only the “x” was selected, I’d like to create a way to add a “#” to each side of it, making <foo>#x#</foo>.

Or if it was all selected, “#<foo>x</foo>#”.

But I have a number of wrapper strings.

Ok, Ok, I admit it. I’m a ColdFusion programmer.

That means I’d like to select some text and wrap it in <cfoutput>My Selected Text Here</cfoutput>, among other things.

The idea of working around a selected block of text seems pretty basic to me. Am I missing something, or is this difficult in Atom?

BTW, I’m very pleased with Atom. I have five, count 'em five code editors, and it’s only a sliver away from being my top one.

Keystroke macros would be welcome.

Thanks again for your interest.


#8

It depends on your definition of “difficult”, I suppose. Is there something already written to wrap selected text with arbitrary text on both sides in any language? Not that I know of. Can something be whipped up pretty quickly? Probably.

If you know what you want to surround the selected text with (such as cfoutput tags) you could use the following in your init.coffee script:


wrapSelection = (selection, before, after) ->
  after ?= before
  selectedText = selection.getText()
  selection.insertText("#{before}#{selectedText}#{after}")

atom.commands.add 'atom-text-editor',
  'custom:wrap-with-cfoutput', ->
    editor = @getModel()
    editor.transact ->
      wrapSelection(selection, "<cfoutput>", "</cfoutput>") for selection in editor.getSelections()

From there you can map keys to the custom commands and other such things. I’m pretty sure you can pop up a simple input dialog to enter text to wrap around things … but hopefully you get the idea.


Customizing ERB comments
Extension/custom command for comments within lines?
#9

Great stuff, Lee. I’ll jump right in and put a few of these together.

Thanks a lot.


#10

I’ve tried and tried but am not having any joy. I’ve tried variations and gotten varied and seemingly inconsistent errors.

For starters, I pasted your code into the init.coffee script.

Then, I added to the keymap.cson:

‘.editor’:
‘ctrl-shift-o’: ‘custom:wrap-with-cfoutput’

But the keymap does not work and the command does not show up in the command palette either.

I’m not getting any load errors in the console.

Do you have any ideas what I may be doing wrong?


#11

Which version of Atom are you using? Did you restart Atom after changing your init script? Did you properly indent the keymap.cson? (By the way, the board also supports fenced code blocks as in GitHub-Flavored Markdown.)


#12

Got it! I updated from 0.135 to 0.144, but I’ll bet I had messed up the indenting in init.coffee or keymap.cson.

Many thanks. This makes Atom my first-choice editor. I’ll have to learn some coffeescript.


#13

Emmet https://atom.io/packages/emmet

wrap with abbreviation

select your text
shift-cmd-A
cfoutput
RETURN

you can also do things like type

div.focus

and get

selected text