msys2+vscode(完善后转载)

转载的原文地址

VSCode是微软出品的代码编辑器。这个编辑器原生支持C、C++等语言的编译运行和调试,也可以通过插件实现其他语言的调试。它还有一个内置的终端功能,默认使用powershell。然而有些时候我们需要的是msys2这种类Linux环境而不是纯windows命令行。比如ESP-IDF就要求在msys2的环境下编译,cmd和powershell下均会报错。这种情况下就需要把VSCode内置的终端替换成msys2。

正确配置msys2环境

根据安装文档,先配置msys2,使直接启动msys2后能成功编译程序,注意将环境变量设置写入/etc/profile中,不要修改~/.bashrc的内容。

配置msys2启动脚本

直接替换powershell为bash是不可行的,因为msys2启动时会运行/etc/profile这个脚本用来设置环境变量等配置,直接替换就会跳过这个配置文件,造成环境不完整,编译报错。而指定配置文件为/etc/profile后启动时会运行/etc/post-install/05-home-dir.post这个脚本,造成启动后目录切换到/home/xxx,而我们需要的是启动后仍然保持当前目录,所以我们需要对/etc/profile进行修改。

首先复制/etc/profile到/etc/profile_vscode,然后在profile_vscode中找到以下代码:

for postinst in $(export LC_COLLATE=C; echo /etc/post-install/*.post); 
    do [ -e "${postinst}" ] && . "${postinst}"
done

将其替换为:

for postinst in $(export LC_COLLATE=C; echo /etc/post-install/*.post); do
if [ -n "echo $postinst|grep home" ]
then
continue
fi
[ -e "${postinst}" ] && . "${postinst}"
done

修改vscode配置文件

在配置文件中添加以下代码(添加自己没有的部分,注意自己的msys2路径)

setting.json的配置改为:
{
"java.errors.incompleteClasspath.severity": "ignore",
"explorer.confirmDelete": false,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"terminal.integrated.shell.windows": "C:\\xinsys\\usr\\bin\\bash.exe",
"terminal.integrated.env.windows": { "CHERE_INVOKING":"1" },
"terminal.integrated.shellArgs.windows": [ "--login" ]
}

注意根据具体msys2安装位置设置其中的参数。

最终效果

msys2+vscode(完善后转载)