简体   繁体   中英

git opens a file with empty buffer in VS Code using WSL

I have Ubuntu installed for WSL and I'm working with it to do my programming stuff. Visual Studio Code is installed in the Windows system.

The problem is that whenever git needs to open a file in its core editor (Visual Studio Code in my case), it opens a file in my editor, but it has an empty buffer though.

I've set VS Code as core editor using this command:

git config --global core.editor "code --wait"

When it opens a file, using git commit for instance, I get this: empty file in VS Code

I'm pretty sure it is caused by the WSL because I've never had this problem before using other alternative but I can't figure why.

This is because your VS Code is a Windows program and you're running in WSL.

When Git in WSL invokes the editor to edit something like a commit message, it passes a path to the file to edit. This path is a Linux path. However, your VS Code is a Windows program, and it doesn't know that the path that's provided should be interpreted as a Linux (WSL) path and instead it interprets it as a Windows path.

You have a couple of options:

  1. Use a Linux version of VS Code instead of a Windows version. That will likely involve setting up an X server.
  2. Use a different editor in Linux.
  3. Change your editor to use a shell script that invokes VS Code with a Windows version of a path. This will only work if VS Code can handle the special WSL paths, which not all Windows programs can, but it would look like this:
$ git config --global core.editor \
  'f () { printf '\''%s\0'\'' "$@" | xargs -0 -I{} wslpath -w {} | xargs -d'\''\n'\'' code --wait; };f'

This sets your editor to a shell function which invokes wslpath -w on each path and then provide each of those to VS Code on the command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM