How to activate a Text Editor?


I can get a active TextEditor by this.

editor = atom.workspace.getActiveTextEditor()

But how to determine whether the Text Edtior is active?
And how to activate a Text Editor?
(use this? -> ???)


Which TextEditor are you checking to see if it’s active?

And how to activate a Text Editor?
(use this? -> ???)

Perhaps. If you’re trying to activate an existing editor, you would use the Pane methods to activate a specific item, but you haven’t provided any information about your use case.


thx your replay.

This TextEditor :

editor = atom.workspace.getActiveTextEditor()

And, sometimes, I need to whether the Text Edtior is active.


You don’t need to activate that editor, because it’s already active by definition.

sFilePath = "./"
oldEditor = atom.workspace.getActiveTextEditor() # path isnt sFilePath, { changeFocus:true }).then (oNewEditor) =>
  console.log "after editor open", oNewEditor

Like this, how can I activate the oldEditor?


Not sure this is exactly what you need, but found some related code in my package jumpy:

    @disposables.add atom.workspace.observeTextEditors (currentEditor) =>
        pane = atom.workspace.paneForItem(currentEditor)

currentEditor = getActiveTextEditor() should give you enough to pass into paneForItem()



@DavidLGoldberg has the right idea. Assign a variable to getActiveTextEditor(), assign a variable to the paneForItem() for that editor, and then activate the pane when you need to get back to the first editor.


Thx for your idea, I will try it soon.:thumbsup:


Thx for your help! :thumbsup: