VSCode格式化shell脚本

安装格式化插件:shell-format

用VSCode打开shell脚本之后,按格式化快捷键Ctrl+Alt+F,会提示没有格式化shell的工具,然后安装插件,我装的是这个插件:shell-format

在这里插入图片描述

介绍:https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format

GitHub:https://github.com/mvdan/sh

在这里插入图片描述

在这里插入图片描述

用法

  • Ctrl+Alt+F
  • 右键 --> 格式化文档

插件不生效问题排查及处理

安装成功后,尝试格式化好几次,都没有生效,然后发现控制台有报错:

shfmt hasn't downloaded yet!Error: ENOENT: no such file or directory, stat 'c:\Users\Administrator\.vscode\extensions\foxundermoon.shell-format-7.2.5\bin\shfmt_v3.6.0_windows_amd64.exe'
Shfmt will be downloaded automatically!
download url: https://github.com/mvdan/sh/releases/download/v3.6.0/shfmt_v3.6.0_windows_amd64.exe
download to: c:\Users\Administrator\.vscode\extensions\foxundermoon.shell-format-7.2.5\bin\shfmt_v3.6.0_windows_amd64.exe
If the download fails, you can manually download it to the dest directory.
Or download to another directory, and then set the "shellformat.path" as the path
download shfmt page: https://github.com/mvdan/sh/releases
You can't use this plugin until the download is successful.
Effective shfmt flags: -i=4
Effective shfmt flags: -i=4
Effective shfmt flags: -i=4
Effective shfmt flags: -i=4
Effective shfmt flags: -i=4
Effective shfmt flags: -i=4

原来又是偶尔连不上GitHub导致不能下载这个文件:https://github.com/mvdan/sh/releases/download/v3.6.0/shfmt_v3.6.0_windows_amd64.exe,尝试重连,不行再用加速网站尝试:https://ghproxy.com/,下载成功后,放到报错提示的路径下,然后就可以正常格式化了。