bash 格式化shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45412325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:20:02  来源:igfitidea点击:

format the shell script

bashshell

提问by Ajay Singh

I have some 1000 lines shell script with all the kepywords

我有一些包含所有关键字的 1000 行 shell 脚本

if/else, while, HERE document

if/else, while, HERE 文档

Can anyone tell the best way to format the script, so the script looks good.

谁能告诉格式化脚本的最佳方法,所以脚本看起来不错。

I was trying VG= in vim but that displaces HERE document content as well.

我在 vim 中尝试 VG= 但这也取代了 HERE 文档内容。

回答by Daniel YC Lin

check this https://github.com/mvdan/sh, here is the sample command for indent with two spaces

检查这个https://github.com/mvdan/sh,这里是两个空格缩进的示例命令

shfmt -i 2 -w yourscript

shfmt -i 2 -w yourscript

回答by Arun Kurian

By default the gg=Ggives you 8 tab spaces. To reduce the 8 tab spaces to 3 spaces edit your .vimrcfile:

默认情况下,gg=G它为您提供 8 个制表符空间。要将 8 个制表符空格减少到 3 个空格,请编辑您的.vimrc文件:

set tabstop=3
set shiftwidth=3
set expandtab

This SO questionqives you a good, in context discussion of everything gg=Grelated.

这个SO 问题让您在上下文中对所有gg=G相关内容进行了很好的讨论。

回答by phk

PyCharm's BashSupport plugin now has an experimental bashcode formatter.

PyCharm 的 BashSupport 插件现在有一个实验性的bash代码格式化程序。

Sadly the BashSupport plugin "will only receive fixes for major problems" as of 2017.

遗憾的是,截至 2017 年,BashSupport 插件“只会收到针对主要问题的修复”

回答by DanieleO

Using VIM add the parameter below in order to indent the entire file

使用 VIM 添加以下参数以缩进整个文件

gg=G