WSLからWindowsのVSCodeを起動

1. Windows上のcodeのパスを調べる

 PS> Get-Command code
 
 CommandType Name     Version Source
 ----------- ----     ------- ------
 Application code.cmd 0.0.0.0 C:\software\VSCode\bin\code.cmd
  • PowerShell上で上のように調べる
  • ここでは C:\software\VSCode\bin\code.cmd

2. WSL上でパスを設定する

2.1 WindowsのパスをLinuxのパスに変換する

 $ wslpath 'C:\software\VSCode\bin\code.cmd'
 /mnt/c/software/VSCode/bin/code.cmd
  • WSL上でwslpathコマンドを実行して、1.で調べたパスを変換する
  • ここでは /mnt/c/software/VSCode/bin/code.cmd

2.2 パスをシェルのエイリアスに設定する

 $ vi ~/.bashrc
 --------------------------------------------
 alias code='/mnt/c/software/VSCode/bin/code'
 --------------------------------------------
  • .bashrcや.bash_aliasesなどのシェルの設定ファイルを開いてエイリアスを設定する
  • この時、code.cmd ではなく、code とする。C:\software\VSCode\bin\ の中にはWindows用のcode.cmdファイルとLinux用のcodeファイルがあり、WSLでは後者を利用する

3. VSCodeの起動

 $ cd ~/src/myproject
 $ code .
  • 上のように目的のフォルダに移動して、VSCodeを起動できる

補足 1 codeをバックグラウンドで動かしたい場合

 function code {
   /mnt/c/software/VSCode/bin/code "$1" &
 }
  • 上のように.bashrcなどに記述してcode関数を作り、それを使うようにする

補足 2

 Do you trust the authors of the files in this folder?
 
 Yes I trust the authors
  • VScode起動時に上のように警告が出たら、Yesを選べばいい

補足 3

 ln -s /mnt/c/software/VSCode/bin/code ~/bin/code
  • 上のようにシムリンクを張ってもいい

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2022-08-01 (月) 06:48:44