LoginSignup
5
3

More than 5 years have passed since last update.

Visual Studio Code でターミナルにコマンド入力する拡張機能を開発

Posted at

作ったもの

VSCodeの拡張機能を開発しました。具体的な機能は以下の通り。

  • テキスト編集時に、Ctrl + F1で実行される。
  • 実行すると、ターミナルに指定したコマンドが入力される。

VSCodeの公式ドキュメントを参考に作成しました。
https://code.visualstudio.com/docs

手順

ひな形の作成

拡張機能のひな形を作成します。
任意のディレクトリで yo code を実行すると、ひな形が生成されます。

package.jasonの編集

このファイルでは、拡張機能の実行方法を指定します。
今回はCtrl + F1で実行するために、contributesを以下のように書き直します。

package.json
    "contributes": {
        "keybindings": [
            {
                "command": "extension.sayHello",
                "key": "ctrl+f1",
                "when": "editorTextFocus"
            }
        ]
    },

src/extension.tsの編集

このファイルでは、拡張機能の実行内容を指定します。
実行するとメッセージが表示され、ターミナルに入力を行うように書き直しました。

extension.ts
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "xsl-fo-runner" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('extension.sayHello', () => {

        vscode.window.showInformationMessage('runExtension!');
        const terminal = vscode.window.createTerminal();
        terminal.sendText('cd C:/');
    });

    context.subscriptions.push(disposable);
}
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3