LoginSignup
27
19

More than 1 year has passed since last update.

Visual Studio Codeで自動的に複数のターミナルを起動しコマンドを実行させる方法

Last updated at Posted at 2022-04-11

はじめに

プロジェクトを起動するたび、同じ作業を行なっていませんか?
私の場合、起動→名前の変更→コマンドの実行を4回も行っています。
今回はTerminals Managerを使用して、この作業を0にした方法を紹介します。

導入方法

Terminals Managerをインストールする

どちらかを選んでください

設定ファイルを開く

  1. メニューバーからコマンドパレットを選択
    1. 参考: Visual Studio Code キーボード ショートカット - すべてのコマンドの表示
  2. Terminals: Edit Configurationtを入力と選択
    2. .vscodeフォルダとterminals.jsonが作成されます

設定ファイルを記入する

terminalsにHashを追加することで、複数設定できます。

例:自動的に2つのコンソールを起動し片方で rails console を起動する

{
  "autorun": true,
  "terminals": [
    {
      "name": "git",
      "icon": "logo-github",
      "description": "This is a git ",
      "command": "",
      "onlyMultiple": true,
      "focus": true
    },
    {
      "name": "rails c",
      "description": "This is a rails console",
      "icon": "ruby",
      "onlyMultiple": true,
      "commands": [
        "rails console"
      ],
      "open": true
    }
  ]
}

例:自動的に1つのコンソールを起動し、後からTerminals: Run Singleを使用してrails cを起動する

{
  "autorun": true,
  "terminals": [
    {
      "name": "git",
      "icon": "logo-github",
      "description": "This is a git ",
      "command": "",
      "onlyMultiple": true,
      "focus": true
    },
    {
      "name": "rails c",
      "description": "This is a rails console",
      "icon": "ruby",
      "onlySingle": true,
      "commands": [
        "rails console"
      ],
      "open": true
    }
  ]
}

terminals.jsに設定できるもの

  • 詳しい設定内容はこちらを確認してください
"autorun": true // 起動時またはプロジェクトがワークスペースに追加されたときに自動的にターミナルを起動する
"autokill": true // ワークスペースを削除したときに自動的にターミナルを終了する
"env": { "name": "value" } // 全てのターミナルに適用されるグローバル環境変数

terminals

"name": "hoge"// 名前
"description": "hode is hoge" // 説明
"icon": "hoge" //アイコン
"color": "terminal.hoge" // 色

"cwd": "/Users/hoge/hoge" // 実行したいディレクトリのパスがプロジェクトのパスと違う場合
"command": "echo hoge" // 実行したいコマンド1つの場合
"commands": [ // 実行したいコマンドが複数ある場合
  "echo hoge",
  "echo hoge 2"
]

"target": "hoge"// インスタンスでコマンドを実行する
"persistent": "hoge" // 閉じてもプロセスを実行し続ける
"substitution": false // 変数置換を無効にする
"recycle": false // 常に新しいターミナルを作成する
"open": true // コマンドを実行した後にターミナルを開く
"focus": true // コマンド実行後にターミナルを開き、ターミナルですぐに入力できるようにする
"execute": false // 最後のコマンドを実行せず入力したままにする

"onlySingle": true // 自動的に実行しない [Terminals: Run]で表示されない
"onlyMultiple": true // [Terminals: Run Single]で表示されない
"onlyAPI": true // 自動的に実行しない [Terminals: Run Single]で表示されない

"shellPath": "/bin/bash" // shellの実行ファイルへのパス
"shellArgs": ["--hoge"] // shellの実行ファイルに渡す引数
"env": { "name": "value" } // ターミナルで適用する環境変数
"envInherit": false // グローバル環境変数を継承しない

コマンドパレットで実行できるコマンド

Terminals: Edit Configuration // terminals.jsonを開く
Terminals: Kill // 全てのターミナルを終了させる
Terminals: Run // onlyMultipleを設定しているものを起動する
Terminals: Run Single // onlySingleを設定しているものを個別に起動させる

Terminals Managerの設定

"terminals.invertCommandsAndDescription": false // quickpickで表示されるターミナルのコマンドとdescriptionを反転させる
"terminals.showCommands": false // コマンドをquickpickに表示する
"terminals.showDescriptions": true // descriptionをquickpick表示する
"terminals.sortTerminals": false //  ターミナルをアルファベット順に並べ替える
"terminals.env": {} // 全てのターミナルに適用されるグローバル環境変数
"terminals.multiplexer": "screen" // ターミナルに使用したいマルチプレクサ "screen", "tmux"を使用できます

詳しく知りたいかたはこちら

おわりに

苦ではないけれど自動化できると楽だな..と思う作業を解消することで、
気持ちが少し楽になりました:ok_woman_tone1:
それでは良き開発ライフを!

27
19
1

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
27
19