Sublime Forum

Add links to selected text

#1

I was wondering if anyone might have a recommendation for a package to enable adding of links to a text.

I am looking for a feature similar to Cmd+K to quickly add a link to a selected text, but I am not aware of the most appropriate package to do that.

Any pointers would be helpful. Thank you!

0 Likes

#2

Can you show me an example of what the file looks like and what text you would like to turn into a link? maybe a before and after, I might be able to help.

0 Likes

#3

Thanks for taking a look!

Not sure I am explaining this right, but I would like to hyperlink the text shown below.

Eventually it should appear in this format - https://cloudup.com/cp41Pzkp17y

Does that make sense? Basically I would like to create HTML (hyperlinks) rapidly using keyboard shortcuts.

Could not add more than one image on this post - This Discourse’s rule. :confused:

0 Likes

#4

You can achieve that with a snippet :slight_smile:

<snippet>
    <content><![CDATA[
<a href="${1}">${SELECTION}</a>
]]></content>
</snippet>

I saved it as link.sublime-snippet and bound it to this key binding

{ "keys": ["ctrl+f"], "command": "insert_snippet", "args": {"name": "Packages/User/link.sublime-snippet"} },
3 Likes

#5

Works great. Thanks for this tip about snippets! I never knew there were snippets and keybindings on Sublime Text 3.

If it’s okay, can I ask for a tip on how to auto-insert the clipboard content in the place of {1}. My search skills on Google are failing me.

Appreciate all your help!

0 Likes

#6

If you want that functionality you would have to use a plugin instead.

import sublime
import sublime_plugin

import textwrap


def _prepare_template(template):
    # Normalize line endings in the template.
    template.replace('\r\n', '\n').replace('\r', '\n')

    # Remove common indent from all of the lines, but throw away the first
    # line.
    template = textwrap.dedent(template[1:])

    # Throw away any trailing blank lines
    return template.rstrip()


class LinkSnipCommand(sublime_plugin.TextCommand):
    template = _prepare_template("""
    <a href="${PASTED}">${SELECTED}</a>
    """)


    def run(self, edit):
        selectedText = self.view.substr(self.view.sel()[0])
        pasted = sublime.get_clipboard()
        self.view.replace(edit, self.view.sel()[0], '')

        self.view.run_command("insert_snippet", {
            "contents": self.template,
            "PASTED": pasted,
            "SELECTED": selectedText
            })

And create a key map

{ "keys": ["ctrl+f"], "command": "link_snip" },
1 Like

#7

As a note on the above, the insert_snippet command already knows how to expand the selected text into a variable named $SELECTION, and inserting a snippet while text is selected will automatically replace the selection with the snippet contents.

Thus a shorter version that does the same thing would be:

import sublime
import sublime_plugin


class LinkSnipCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", {
            "contents": "<a href=\"${PASTED}\">${SELECTION}</a>",
            "PASTED": sublime.get_clipboard()
            })

It’s also possible to do the above as just a simple sublime-macro that executes insert_snippet and paste if you don’t want to mess with plugins at all.

4 Likes

#8

I was looking how to do that but couldn’t figure it out.

Thanks @OdatNurd :slight_smile:

0 Likes