Skip to main content
Module

x/denops_std/function/mod.ts>printf

📚 Standard module for denops.vim
Go to Latest
function printf
import { printf } from "https://deno.land/x/denops_std@v6.4.0/function/mod.ts";

Return a String with {fmt}, where "%" items are replaced by the formatted form of their respective arguments. Example:

printf("%4d: E%d %.30s", lnum, errno, msg)

May result in: " 99: E42 asdfasdfasdfasdfasdfasdfasdfas"

When used as a method the base is passed as the second argument:

Compute()->printf("result: %d")

You can use call() to pass the items as a list.

Often used items are: %s string %6S string right-aligned in 6 display cells %6s string right-aligned in 6 bytes %.9s string truncated to 9 bytes %c single byte %d decimal number %5d decimal number padded with spaces to 5 characters %x hex number %04x hex number padded with zeros to at least 4 characters %X hex number using upper case letters %o octal number %08b binary number padded with zeros to at least 8 chars %f floating point number as 12.23, inf, -inf or nan %F floating point number as 12.23, INF, -INF or NAN %e floating point number as 1.23e3, inf, -inf or nan %E floating point number as 1.23E3, INF, -INF or NAN %g floating point number, as %f or %e depending on value %G floating point number, as %F or %E depending on value %% the % character itself

Conversion specifications start with '%' and end with the conversion type. All other characters are copied unchanged to the result.

The "%" starts a conversion specification. The following arguments appear in sequence:

    % [pos-argument] [flags] [field-width] [.precision] type

pos-argument At most one positional argument specifier. These take the form {n$}, where n is >= 1.

flags Zero or more of the following flags:

`#`         The value should be converted to an "alternate
          form".  For c, d, and s conversions, this option
          has no effect.  For o conversions, the precision
          of the number is increased to force the first
          character of the output string to a zero (except
          if a zero value is printed with an explicit
          precision of zero).
          For b and B conversions, a non-zero result has
          the string "0b" (or "0B" for B conversions)
          prepended to it.
          For x and X conversions, a non-zero result has
          the string "0x" (or "0X" for X conversions)
          prepended to it.

0 (zero)  Zero padding.  For all conversions the converted
          value is padded on the left with zeros rather
          than blanks.  If a precision is given with a
          numeric conversion (d, b, B, o, x, and X), the 0
          flag is ignored.

-         A negative field width flag; the converted value
          is to be left adjusted on the field boundary.
          The converted value is padded on the right with
          blanks, rather than on the left with blanks or
          zeros.  A - overrides a 0 if both are given.

' ' (space)  A blank should be left before a positive
          number produced by a signed conversion (d).

+         A sign must always be placed before a number
          produced by a signed conversion.  A + overrides
          a space if both are used.

field-width An optional decimal digit string specifying a minimum field width. If the converted value has fewer bytes than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given) to fill out the field width. For the S conversion the count is in cells.

.precision An optional precision, in the form of a period '.' followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, o, x, and X conversions, the maximum number of bytes to be printed from a string for s conversions, or the maximum number of cells to be printed from a string for S conversions. For floating point it is the number of digits after the decimal point.

type A character that specifies the type of conversion to be applied, see below.

A field width or precision, or both, may be indicated by an asterisk '*' instead of a digit string. In this case, a Number argument supplies the field width or precision. A negative field width is treated as a left adjustment flag followed by a positive field width; a negative precision is treated as though it were missing. Example:

:echo printf("%d: %.*s", nr, width, line)

This limits the length of the text used from "line" to "width" bytes.

If the argument to be formatted is specified using a positional argument specifier, and a '*' is used to indicate that a number argument is to be used to specify the width or precision, the argument(s) to be used must also be specified using a {n$} positional argument specifier. See printf-$.

The conversion specifiers and their meanings are:

dbBoxX The Number argument is converted to signed decimal (d), unsigned binary (b and B), unsigned octal (o), or unsigned hexadecimal (x and X) notation. The letters "abcdef" are used for x conversions; the letters "ABCDEF" are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. In no case does a non-existent or small field width cause truncation of a numeric field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result. The 'h' modifier indicates the argument is 16 bits. The 'l' modifier indicates the argument is a long integer. The size will be 32 bits or 64 bits depending on your platform. The "ll" modifier indicates the argument is 64 bits. The b and B conversion specifiers never take a width modifier and always assume their argument is a 64 bit integer. Generally, these modifiers are not useful. They are ignored when type is known from the argument.

i alias for d D alias for ld U alias for lu O alias for lo

c The Number argument is converted to a byte, and the resulting character is written.

s The text of the String argument is used. If a precision is specified, no more bytes than the number specified are used. If the argument is not a String type, it is automatically converted to text with the same format as ":echo".

S The text of the String argument is used. If a precision is specified, no more display cells than the number specified are used.

f F The Float argument is converted into a string of the form 123.456. The precision specifies the number of digits after the decimal point. When the precision is zero the decimal point is omitted. When the precision is not specified 6 is used. A really big number (out of range or dividing by zero) results in "inf" or "-inf" with %f (INF or -INF with %F). "0.0 / 0.0" results in "nan" with %f (NAN with %F). Example:

        echo printf("%.2f", 12.115)

            12.12
    Note that roundoff depends on the system libraries.
    Use `round()` when in doubt.

e E The Float argument is converted into a string of the form 1.234e+03 or 1.234E+03 when using 'E'. The precision specifies the number of digits after the decimal point, like with 'f'.

g G The Float argument is converted like with 'f' if the value is between 0.001 (inclusive) and 10000000.0 (exclusive). Otherwise 'e' is used for 'g' and 'E' for 'G'. When no precision is specified superfluous zeroes and '+' signs are removed, except for the zero immediately after the decimal point. Thus 10000000.0 results in 1.0e7.

% A '%' is written. No argument is converted. The complete conversion specification is "%%".

When a Number argument is expected a String argument is also accepted and automatically converted. When a Float or String argument is expected a Number argument is also accepted and automatically converted. Any other argument type results in an error message.

The number of {exprN} arguments must exactly match the number of "%" items. If there are not sufficient or too many arguments an error is given. Up to 18 arguments can be used.

In certain languages, error and informative messages are more readable when the order of words is different from the corresponding message in English. To accommodate translations having a different word order, positional arguments may be used to indicate this. For instance:

#, c-format
msgid "%s returning %s"
msgstr "waarde %2$s komt terug van %1$s"

In this example, the sentence has its 2 string arguments reversed in the output.

echo printf(
    "In The Netherlands, vim's creator's name is: %1$s %2$s",
    "Bram", "Moolenaar")

In The Netherlands, vim's creator's name is: Bram Moolenaar

echo printf(
    "In Belgium, vim's creator's name is: %2$s %1$s",
    "Bram", "Moolenaar")

In Belgium, vim's creator's name is: Moolenaar Bram

Width (and precision) can be specified using the '*' specifier. In this case, you must specify the field width position in the argument list.

echo printf("%1$*2$.*3$d", 1, 2, 3)

001

echo printf("%2$*3$.*1$d", 1, 2, 3)

  2

echo printf("%3$*1$.*2$d", 1, 2, 3)

03

echo printf("%1$*2$.*3$g", 1.4142, 2, 3)

1.414

You can mix specifying the width and/or precision directly and via positional arguments:

echo printf("%1$4.*2$f", 1.4142135, 6)

1.414214

echo printf("%1$*2$.4f", 1.4142135, 6)

1.4142

echo printf("%1$*2$.*3$f", 1.4142135, 6, 2)

  1.41

You cannot mix positional and non-positional arguments:

echo printf("%s%1$s", "One", "Two")

E1500: Cannot mix positional and non-positional arguments:
%s%1$s

You cannot skip a positional argument in a format string:

echo printf("%3$s%1$s", "One", "Two", "Three")

E1501: format argument 2 unused in $-style format:
%3$s%1$s

You can re-use a [field-width] (or [precision]) argument:

echo printf("%1$d at width %2$d is: %01$*2$d", 1, 2)

1 at width 2 is: 01

However, you can't use it as a different type:

echo printf("%1$d at width %2$ld is: %01$*2$d", 1, 2)

E1502: Positional argument 2 used as field width reused as
different type: long int/int

When a positional argument is used, but not the correct number or arguments is given, an error is raised:

echo printf("%1$d at width %2$d is: %01$*2$.*3$d", 1, 2)

E1503: Positional argument 3 out of bounds: %1$d at width
%2$d is: %01$*2$.*3$d

Only the first error is reported:

echo printf("%01$*2$.*3$d %4$d", 1, 2)

E1503: Positional argument 3 out of bounds: %01$*2$.*3$d
%4$d

A positional argument can be used more than once:

echo printf("%1$s %2$s %1$s", "One", "Two")

One Two One

However, you can't use a different type the second time:

echo printf("%1$s %2$s %1$d", "One", "Two")

E1504: Positional argument 1 type used inconsistently:
int/string

Various other errors that lead to a format string being wrongly formatted lead to:

echo printf("%1$d at width %2$d is: %01$*2$.3$d", 1, 2)

E1505: Invalid format specifier: %1$d at width %2$d is:
%01$*2$.3$d

This internal error indicates that the logic to parse a positional format argument ran into a problem that couldn't be otherwise reported. Please file a bug against Vim if you run into this, copying the exact format string and parameters that were used.

Parameters

denops: Denops
fmt: unknown
expr1: unknown

Returns

Promise<string>