Home Explore Blog CI



neovim

96th chunk of `runtime/doc/vimfn.txt`
dfd7a1988102dc92cc153003c2bb2101d378daee1561e5230000000100000fb0
	Examples: >vim
			echo range(4)		" [0, 1, 2, 3]
			echo range(2, 4)	" [2, 3, 4]
			echo range(2, 9, 3)	" [2, 5, 8]
			echo range(2, -2, -1)	" [2, 1, 0, -1, -2]
			echo range(0)		" []
			echo range(2, 0)	" error!
<

                Parameters: ~
                  • {expr} (`any`)
                  • {max} (`integer?`)
                  • {stride} (`integer?`)

                Return: ~
                  (`any`)

readblob({fname} [, {offset} [, {size}]])                           *readblob()*
		Read file {fname} in binary mode and return a |Blob|.
		If {offset} is specified, read the file from the specified
		offset.  If it is a negative value, it is used as an offset
		from the end of the file.  E.g., to read the last 12 bytes: >vim
			echo readblob('file.bin', -12)
<		If {size} is specified, only the specified size will be read.
		E.g. to read the first 100 bytes of a file: >vim
			echo readblob('file.bin', 0, 100)
<		If {size} is -1 or omitted, the whole data starting from
		{offset} will be read.
		This can be also used to read the data from a character device
		on Unix when {size} is explicitly set.  Only if the device
		supports seeking {offset} can be used.  Otherwise it should be
		zero.  E.g. to read 10 bytes from a serial console: >vim
			echo readblob('/dev/ttyS0', 0, 10)
<		When the file can't be opened an error message is given and
		the result is an empty |Blob|.
		When the offset is beyond the end of the file the result is an
		empty blob.
		When trying to read more bytes than are available the result
		is truncated.
		Also see |readfile()| and |writefile()|.

                Parameters: ~
                  • {fname} (`string`)
                  • {offset} (`integer?`)
                  • {size} (`integer?`)

                Return: ~
                  (`any`)

readdir({directory} [, {expr}])                                      *readdir()*
		Return a list with file and directory names in {directory}.
		You can also use |glob()| if you don't need to do complicated
		things, such as limiting the number of matches.

		When {expr} is omitted all entries are included.
		When {expr} is given, it is evaluated to check what to do:
			If {expr} results in -1 then no further entries will
			be handled.
			If {expr} results in 0 then this entry will not be
			added to the list.
			If {expr} results in 1 then this entry will be added
			to the list.
		Each time {expr} is evaluated |v:val| is set to the entry name.
		When {expr} is a function the name is passed as the argument.
		For example, to get a list of files ending in ".txt": >vim
		  echo readdir(dirname, {n -> n =~ '.txt$'})
<		To skip hidden and backup files: >vim
		  echo readdir(dirname, {n -> n !~ '^\.\|\~$'})

<		If you want to get a directory tree: >vim
		  function! s:tree(dir)
		      return {a:dir : map(readdir(a:dir),
		      \ {_, x -> isdirectory(x) ?
		      \          {x : s:tree(a:dir .. '/' .. x)} : x})}
		  endfunction
		  echo s:tree(".")
<
		Returns an empty List on error.

                Parameters: ~
                  • {directory} (`string`)
                  • {expr} (`integer?`)

                Return: ~
                  (`any`)

readfile({fname} [, {type} [, {max}]])                              *readfile()*
		Read file {fname} and return a |List|, each line of the file
		as an item.  Lines are broken at NL characters.  Macintosh
		files separated with CR will result in a single long line
		(unless a NL appears somewhere).
		All NUL characters are replaced with a NL character.
		When {type} contains "b" binary mode is used:
		- When the last line ends in a NL an extra empty list item is
		  added.
		- No CR characters are removed.
		Otherwise:
		- CR characters that appear before a NL are removed.
		- Whether the last line ends in a NL or not does not matter.
		- Any UTF-8 byte order mark is removed from the text.
		When {max} is given this specifies the maximum number of lines
		to be read.  Useful if you only want to check the first ten

Title: Reading Binary Files, Directory Contents, and Text Files
Summary
This section details the `readblob()`, `readdir()`, and `readfile()` functions. `readblob()` reads a binary file and returns a Blob, allowing specification of offset and size. `readdir()` returns a List of file and directory names within a directory, with optional filtering via an expression. `readfile()` reads a file and returns a List of lines, with options for binary mode and limiting the number of lines read.