Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Wrap selection #317

Open
Skydancer9 opened this issue Apr 15, 2022 · 3 comments
Open

Wrap selection #317

Skydancer9 opened this issue Apr 15, 2022 · 3 comments

Comments

@Skydancer9
Copy link

Hi,
Sorry for my beginner's question. Is it possible to wrap selected text in snippet code?
Example:
Select some text, for example number 10 below:
<p>Some text<sup>10</sup></p>

AFTER (inserts a snippet and adds the selected number 10 in three places):
<p>Some text<sup><a epub:type="noteref" role="doc-noteref" href="#fn10" id="ref10">10</a></sup></p>

@savetheclocktower
Copy link
Contributor

No, but you can define a custom command in your init.js to do this.

Haven't tested this, but here's how your example could work:

atom.commands.add('atom-text-editor', {
  'custom:some-command': () => {
    let editor = atom.workspace.getActiveTextEditor();
    let selection = editor.getLastSelection();

    let output = `<a epub:type="noteref" role="doc-noteref" href="#fn${selection} id="ref${selection}">${selection}</a>`;

    selection.insertText(output);
  }
});

Feel free to name it whatever you want, of course. Then you could map it to a keystroke in your keymap.cson:

'atom-text-editor':
  'ctrl-shift-0': 'custom:some-command'

There's a similar programmatic interface to the snippets module, so you could define a snippet on-the-fly that incorporates the selection. But in your example, there's no reason to do so; it's simpler to do it with Atom's built-in APIs.

@Skydancer9
Copy link
Author

@savetheclocktower Thank you!

@pbsds
Copy link

pbsds commented May 24, 2022

Could it be possible to add support for the $TM_SELECTED_TEXT variable, or $VISUAL as in vim?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants