← 返回命令列表

Linux command

zformat 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Format a string

zformat -f REPLY "Name: %n, Age: %a" n:Alice a:30

Format with field width

zformat -f REPLY "%-20n %5a" n:Alice a:30

Use ternary expressions

zformat -f REPLY "Status: %(s.active.inactive)" s:1

Align strings

zformat -a result " = " "key1:value1" "key2:value2" "longkey:value3"

Format with existence-based

zformat -F REPLY "%(n.Name: %n.anonymous)" n:Alice

说明

zformat is a builtin command provided by the zsh/zutil module. It performs string formatting and alignment operations commonly used in zsh completion system internals and custom scripts. The -f form replaces %char sequences in a format string with values from named specifiers. Each specifier is a single character followed by a colon and a value string. Optional minimum and maximum field widths can be specified as %min.maxc where negative minimum widths pad to the left. Ternary expressions in the form %(X.true_text.false_text) compare the specifier value numerically against a test number (default 0). The -F form works identically except ternary expressions test whether the specifier value is present and non-empty rather than performing numeric comparison. The -a form aligns a list of left:right string pairs by padding the left portions with spaces so that the separator strings all line up when displayed vertically. This is useful for formatting option lists and help text. The module must be loaded before use with zmodload zsh/zutil.

参数

-f _param_ _format_ _spec_ ...
Format a string. Each _spec_ is char:string. Every %char in _format_ is replaced by the corresponding _string_. The result is stored in _param_. Supports field widths and ternary expressions with numeric comparison.
-F _param_ _format_ _spec_ ...
Like -f, but ternary expressions choose between true/false text based on whether the format specifier is present and non-empty, rather than numeric comparison.
-a _array_ _sep_ _spec_ ...
Align strings. Each _spec_ is left:right. The colon is replaced by _sep_ and left strings are padded with spaces so all separators align vertically. Result is stored in _array_.

FAQ

What is the zformat command used for?

zformat is a builtin command provided by the zsh/zutil module. It performs string formatting and alignment operations commonly used in zsh completion system internals and custom scripts. The -f form replaces %char sequences in a format string with values from named specifiers. Each specifier is a single character followed by a colon and a value string. Optional minimum and maximum field widths can be specified as %min.maxc where negative minimum widths pad to the left. Ternary expressions in the form %(X.true_text.false_text) compare the specifier value numerically against a test number (default 0). The -F form works identically except ternary expressions test whether the specifier value is present and non-empty rather than performing numeric comparison. The -a form aligns a list of left:right string pairs by padding the left portions with spaces so that the separator strings all line up when displayed vertically. This is useful for formatting option lists and help text. The module must be loaded before use with zmodload zsh/zutil.

How do I run a basic zformat example?

Run `zformat -f REPLY "Name: %n, Age: %a" n:Alice a:30` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -f _param_ _format_ _spec_ ... do in zformat?

Format a string. Each _spec_ is char:string. Every %char in _format_ is replaced by the corresponding _string_. The result is stored in _param_. Supports field widths and ternary expressions with numeric comparison.