Skip to content

shfmt

A shell formatter (sh/bash/mksh)


Install shfmt on Gentoo Linux

git clone https://github.com/patrickvane/shfmt
cd shfmt
cd /mnt/usb/git/shfmt/cmd/shfmt
go build
install -m 755 shfmt /usr/local/bin/shfmt

Test shfmt in command line

printf 'if true;then echo hi;fi\n' | shfmt

The number that we used after the -i option indicates the number of spaces that we want to use for indentation.

shfmt -i 4 emagnet.sh

Indent a shell script in Vim

<S-v>   # Shift + v to select the current line
<S-G>   # Select everything from the current line to the end of the file
=       # With everything selected, press `=` to auto-indent using shfmt

Example 2: Indent an entire shell script

ggVG    # Select the entire file
=       # Auto-indent using shfmt