jsmecham/atom-terminal-tab

Run code from active editor

Opened this issue · 2 comments

How could I paste text into the terminal without having to leave the editor by pressing ctrl+enter ?

Would also be good to have a way to append a '\n' or '\r' to the end of the text copied from the editor before it was pasted into the terminal, so it would run after it was pasted into the terminal.

I tried adding the following to my keymap.cson file, but it did not work as the Terminal has to have the focus for this command to even be available.

'atom-workspace atom-text-editor:not([mini])':
  'ctrl-enter': 'Terminal:Paste'

I suppose atom's composed-commands could help with this.

I am stuck.

Does anyone have suggestions on what would need to be added to my init.coffee file?

This would not work.

atom.commands.add 'atom-text-editor', 'custom:run-line', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.selectLinesContainingCursors()
  text = editor.copySelectedText() + '\n'
  terminal = atom.workspace.paneForURI('Terminal')
  terminal.paste(text)

Then add the following to my keymap.cson file

'atom-workspace atom-text-editor:not([mini])':
  'ctrl-enter': 'custom:run-line'

I know this issue is quite old, but here's a solution (with latest version of Atom, obviously):

I could not find a way to get the terminal directly, since it seems to not be stored anywhere, so we just iterate overs pane items to check if the pane's active item is a terminal.

editor = atom.workspace.getActiveTextEditor()
    selection = editor.getSelectedText()
    for p in atom.workspace.getPanes()
        i = p.getActiveItem()
        continue if i == undefined
        if (i.constructor.name == 'TerminalSession')
            term = i
            break
    return unless term?
    term.pty.write(selection + '\n')