Insert text and select it?


#1

I’m attempting to make a simple function to create a Markdown-formatted link from selected text, such that

selected text

becomes

[selected text](URL)

but I’d like the “URL” text to be selected (such that if you then start typing your link, it will replace that text with your link).

I’ve gotten it to a point where I can get the text inserted:

editor = atom.workspaceView.getActiveView().getEditor()
selection = editor.getLastSelection()
curText = selection.getText()
selection.insertText('['+curText+'](URL)')

But then how do I move the editor’s cursor/selection to “URL”?

Thanks!


#2

I would use selection.getBufferRange() and selection.setBufferRange() to modify the selection you already have to exactly where you want it to be.