【问题标题】:How to send selection to external Terminal in vscode?如何在 vscode 中将选择发送到外部终端?
【发布时间】:2018-02-12 05:11:27
【问题描述】:

我希望能够选择文件中的文本并将其发送到外部终端。这可能吗?

如果没有,可以通过applescript来完成吗?

【问题讨论】:

  • “已发送到外部终端”是什么意思?
  • @pvg 基本上复制粘贴就行了
  • 你想把它粘贴到什么地方? Terminal.app的窗口?哪个窗口?还是标签?目前还不清楚您要完成什么。
  • @pvg 是的,iTerm.app 的当前活动标签
  • 这听起来可行,但可能会被误导。这个答案有一个指向扩展的指针,该扩展对您可以修改的选择 stackoverflow.com/questions/35184509/… 进行操作,并且可以从那里修改 applescript(您可以使用 shell 命令调用 AS)。

标签: visual-studio-code


【解决方案1】:

我最终编写了自己的 extension 来使用 AppleScript 执行此操作。就这么简单:

function activate(context) {
  const runIterm = vscode.commands.registerCommand('run-external.iterm', function() {
    const editor = vscode.window.activeTextEditor;
    let textToPaste;
    if (editor.selection.isEmpty) {
      textToPaste = editor.document.lineAt(editor.selection.active.line).text;
    } else {
      textToPaste = editor.document.getText(editor.selection);
    }
    exec(
      `osascript ` +
        ` -e 'tell app "iTerm"' ` +
        ` -e 'set mysession to current session of current window' ` +
        ` -e 'tell mysession to write text "${textToPaste}"' ` +
        ` -e 'end tell'`
    );
  });

  context.subscriptions.push(runIterm);
}
exports.activate = activate;

【讨论】:

  • 我可以让它与扩展一起工作。我需要对 AppleScript 做些什么吗?
  • @AtanasJanackovski 扩展正在生成 AppleScript。无需手动执行任何操作
  • 我无法让它工作。我通过 VSCE 命令在 GitHub 上打包了你的 repo,安装了它,但什么也没发生。我们做错了什么?
猜你喜欢
  • 1970-01-01
  • 2011-04-01
  • 2019-01-08
  • 2014-05-15
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多