将按键绑定到使用 Visual Studio 代码中的当前文件的 shell 命令 [英] Bind a keypress to a shell command that uses the current file in visual studio code

查看:20
本文介绍了将按键绑定到使用 Visual Studio 代码中的当前文件的 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建一个键绑定来对文件执行 shell 命令?类似:

Is there a way to create a keybinding to execute a shell command on a file? something like:

{
    "key": "ctrl+shift+e",
    "command": "run",
    "command": "touch $file",
    "when": "editorTextFocus"
}

我不想使用任务,因为这需要对整个编辑器是全局的,而不是特定的工作区.

I don't want to use tasks as this needs to be global for the whole editor, not to a particular workspace.

推荐答案

[请参阅下面我的编辑 - 现在更容易做到.]

我想出了一种方法来做你想做的事,但这有点像黑客.您可能知道,如果您可以在 .vscode/tasks.json 中创建任务并使用类似以下内容,则绑定任务很容易:

I figured out a way to do what you want but it is a bit of a hack. As you probably know it is easy to bind a task if you can create the task in .vscode/tasks.json and use something like the following:

{
  "key": "shift+escape",
  "command": "workbench.action.tasks.runTask",
  "args": "Start server and process files"
},

在没有预先存在的任务的情况下通过键绑定运行脚本要复杂得多.但是,您可以尝试以下利用 "workbench.action.terminal.runSelectedText", 命令的方法.但是我们需要先让脚本进入一个选择,所以:

It is much trickier to run a script via a keybinding without a pre-existing task. However, you can try the following which takes advantage of the "workbench.action.terminal.runSelectedText", command. But we need to get the script into a selection first so:

使用 宏扩展(有点粗糙),以便我们可以将多个命令联系在一起.在您的用户设置中:

Use the macros extension (which is a little rough) so that we can tie together multiple commands. In your user settings:

"runCommandInTerminal": [

   {
     "command": "type",
     "args": {
        "text": "node -v"
     }
  },
  { 
    "command": "cursorMove",
    "args": {
       "to": "wrappedLineStart",
       "by": "wrappedLine",
       "value": 1,
       "select": true
    }
  },
  "workbench.action.terminal.runSelectedText",
  "editor.action.clipboardCutAction",
  //  "workbench.action.terminal.focus"
],

这是一个设置 "runCommandInTerminal" 的一般示例,然后您可以将其绑定到 keybindings.json 中您希望的任何键和弦,例如

This is a general example of a setting "runCommandInTerminal" that you can then bind to any key chord you wish in keybindings.json, like

{
    "key": "ctrl+shift+e",
    "command": "macros.runCommandInTerminal"
},

您的示例有点难,因为您想访问诸如 ${file} 之类的内容,而您无法在设置中访问它,只能访问 tasks.json 和 launch.json.幸运的是,有一个命令可以获取当前文件:"copyFilePath".所以试试

Your example is a little harder because you want to access something like ${file} which you cannot do in the settings, only tasks.json and launch.json. Fortunately, there is a command which will get the current file: "copyFilePath". So try

"runCommandInTerminal": [

      "copyFilePath",
      {
        "command": "type",
        "args": {
          "text": "touch "
        }
      },
      "editor.action.clipboardPasteAction",
      { 
      "command": "cursorMove",
        "args": {
        "to": "wrappedLineStart",
        "by": "wrappedLine",
        "value": 1,
        "select": true
        }
      },
      "workbench.action.terminal.runSelectedText",
      "editor.action.clipboardCutAction",
      // "workbench.action.terminal.focus"
],

首先获取文件路径,然后输出脚本命令的第一部分touch".

First get the file path, then output the first part of your script command "touch".

接下来将文件路径附加到命令的末尾.

Next append the filepath to the end of the command.

移动光标选择上一个.

在终端中运行选择.

从编辑器中剪切选择.

这可以与您的键绑定一起运行.您将看到正在键入和剪切脚本的闪光,但您的编辑器代码不会受到影响.最好从编辑器中的空白行运行它,但如果您愿意,也可以从不相关代码行的开头运行它(但现在缩进可能会丢失).

This can be run with your keybinding. You will see the flash of the script being typed and cut but your editor code will be unaffected. It is best to run it from a blank line in the editor but you can run it from the beginning of a line of unrelated code if you wish (but indentation may be lost for now).

它很笨拙,但似乎有效.我很想知道是否有扩展程序或其他方法来做这个清洁工.

It is hacky but seems to work. I would love to know if there is an extension or another way to do this cleaner.

编辑 2019 年 5 月:

EDIT May, 2019:

自从我的原始答案(如@Jeff 所示)以来,vscode 添加了 sendSequence 命令.以及在该命令中使用变量的能力.所以现在OP的问题更容易完成:

Since my original answer (as @Jeff indicated) vscode has added the sendSequence command. And the ability to use variables with that command. So now the OP's question is much easier to accomplish:

{
  "key": "alt+x",
  "command": "workbench.action.terminal.sendSequence",
  "args": {"text": "touch ${file}"}
}

在您的 keybindings.json 文件中.请参阅使用 sendSequnce 命令的变量.

in your keybindings.json file. See variables with a sendSequnce command.

这篇关于将按键绑定到使用 Visual Studio 代码中的当前文件的 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆