Vim 可以缩进 bash 脚本。但在缩进之前不要重新格式化它们。 备份 bash 脚本,用 vim 打开它,输入 gg=GZZ ,缩进将被纠正。 (不耐烦的人请注意:这会覆盖文件,因此请务必进行备份!)
不过,<< 存在一些错误(期望 EOF 作为一行中的第一个字符)例如编辑
:ZZ 不是 ZQ
Vim can indent bash scripts. But not reformat them before indenting. Backup your bash script, open it with vim, type gg=GZZ and indent will be corrected. (Note for the impatient: this overwrites the file, so be sure to do that backup!)
Though, some bugs with << (expecting EOF as first character on a line) e.g.
发布评论
评论(4)
Vim 可以缩进 bash 脚本。但在缩进之前不要重新格式化它们。
备份 bash 脚本,用 vim 打开它,输入 gg=GZZ ,缩进将被纠正。 (不耐烦的人请注意:这会覆盖文件,因此请务必进行备份!)
不过,
<<
存在一些错误(期望 EOF 作为一行中的第一个字符)例如编辑:ZZ 不是 ZQ
Vim can indent bash scripts. But not reformat them before indenting.
Backup your bash script, open it with vim, type
gg=GZZ
and indent will be corrected. (Note for the impatient: this overwrites the file, so be sure to do that backup!)Though, some bugs with
<<
(expecting EOF as first character on a line) e.g.EDIT: ZZ not ZQ
bash5+ 有一个 --pretty-print 选项..它会删除注释,包括第一行 '#!/bin...'
bash5+ has a --pretty-print option.. it will remove comments though, including a first-line '#!/bin...'
在 bash 中我这样做:
这消除了注释并重新缩进了脚本“bash way”。
如果你的脚本中有 HEREDOCS,它们就会被前面函数中的 sed 破坏。
所以使用:
但是你的所有脚本都会有 4 个空格的缩进。
或者你可以这样做:
它也可以处理heredocs。
In bash I do this:
this eliminates comments and reindents the script "bash way".
If you have HEREDOCS in your script, they got ruined by the sed in the previous function.
So use:
But all your script will have a 4 spaces indentation.
Or you can do:
which takes care also of heredocs.
shfmt 效果很好。
您可以格式化 bash 脚本,还可以从预提交挂钩检查格式。
唯一缺少的选项是它不会根据行长度重新格式化(还)。
由于它是用 go 编写的,因此您只需下载适用于大多数平台的二进制文件,例如 Travis (
.travis.yml
):npm 上还有一个交叉编译的 js 版本和许多编辑器插件 (请参阅相关项目)
shfmt works very well.
You can format bash scripts and also check the formatting from pre-commit hooks.
The only option absent is that it does not reformat according to line length (yet).
Since it is written in go you can just download the binary for most platforms, e.g. for Travis (
.travis.yml
):There is also a cross compiled js version on npm and a lot of editor plugins (see related projects)