How do I format zsh code (something like prettier / shfmt)?
Asked Answered
G

0

9

For bash and sh, there is shfmt.

How do I format zsh code?

I'd like to be able to set some options like in shfmt, ie:

  -i uint   indent: 0 for tabs (default), >0 for number of spaces
  -bn       binary ops like && and | may start a line
  -ci       switch cases will be indented
  -sr       redirect operators will be followed by a space
  -kp       keep column alignment paddings
  -fn       function opening braces are placed on a separate line

zsh-hist says it supports "uniform code formatting", but I can't work out how to run it from the command line and it's main purpose is history manipulation.

How do I format zsh code either in the shell or via something like VSCode prettier?

Goggle answered 19/1, 2022 at 6:23 Comment(5)
Are you searching for a pretty-printer for zsh, similar to shfmt for bash, or are you going to write your own pretty-printer?Blotto
I'm looking for a method to format zsh code without writing a tool from scratch.Goggle
Perhaps you could start by taking shfmt as a base, and create from this your own formatter? Since zsh and bash are syntactically quite similar in spirit, this should be much less work than starting from scratch.Blotto
Could you share an example fie you're trying to format?Kurtzig
While it's still not implemented (as of Jan 2023), this issue on shfmt by the author from 2021 sounds like there is interest in implementing support for it: github.com/mvdan/sh/issues/120#issuecomment-903077180Rodin

© 2022 - 2024 — McMap. All rights reserved.