Add text to the beginning of the editor.selectedText()


#1

I’d like to add a generic string to the head of the document using JavaScript :

var editor = atom.workspace.getActiveTextEditor();
    if (editor) {
      console.log("Package Active");
      var selection = editor.getSelectedText();
      editor.insertText("Generic Text" + selection);
    }
  }

I can not understand why the text is being inserted at the position of the cursor and not at the head of the document. Thanks


#2

Because the cursor is where text is inserted, that is its reason for existing :grinning: And because you’re using editor.insertText. If you use selection.insertText, then it should work as you expect.