vim.text
Methods4
function M.diff(...) -> string | integer[][] | nil
Run diff on strings {a} and {b}. Any indices returned by this function, either directly or via callback arguments, are 1-based.
Examples:
vim.text.diff('a\n', 'b\nc\n')
-- =>
-- @@ -1 +1,2 @@
-- -a
-- +b
-- +c
vim.text.diff('a\n', 'b\nc\n', {result_type = 'indices'})
-- =>
-- {
-- {1, 1, 1, 2}
-- }
...
- string | integer[][] | nil
See {opts.resulttype}.
nilif {opts.onhunk} is given.
function M.hexencode(str: string) -> string
Hex encode a string.
strstringString to encode
- string
: Hex encoded string
function M.hexdecode(enc: string) -> (nil | string, nil | string)
Hex decode a string.
encstringString to decode
- nil | string
: Decoded string
- nil | string
: Error message, if any
function M.indent(size: integer, text: string, opts: { expandtab: integer? } | nil) -> (string, integer)
Sets the indent (i.e. the common leading whitespace) of non-empty lines in text to size spaces/tabs.
Indent is calculated by number of consecutive indent chars.
- The first indented, non-empty line decides the indent char (space/tab):
SPC SPC TAB …= two-space indent.TAB SPC …= one-tab indent.- Set
opts.expandtabto treat tabs as spaces.
To "dedent" (remove the common indent), pass size=0:
vim.print(vim.text.indent(0, ' a\n b\n'))
To adjust relative-to an existing indent, call indent() twice:
local indented, old_indent = vim.text.indent(0, ' a\n b\n')
indented = vim.text.indent(old_indent + 2, indented)
vim.print(indented)
To ignore the final, blank line when calculating the indent, use gsub() before calling indent():
local text = ' a\n b\n '
vim.print(vim.text.indent(0, (text:gsub('\n[\t ]+\n?$', '\n'))))
sizeintegerNumber of spaces.
textstringText to indent.
opts{ expandtab: integer? } | nil
- string
Indented text.
- integer
Indent size before modification.