Buffer API Functions
Buffer functions operate on Neovim buffers. They are prefixed withnvim_buf_*.
Buffer Information
nvim_buf_line_count()
Returns the number of lines in the given buffer.Buffer handle, or 0 for current buffer
Line count, or 0 for unloaded buffer
nvim_buf_is_loaded()
Checks if a buffer is valid and loaded.Buffer handle
true if the buffer is valid and loaded
nvim_buf_is_valid()
Checks if a buffer is valid.Buffer handle
true if the buffer is valid
nvim_buf_get_name()
Gets the full file name for the buffer.Buffer handle, or 0 for current buffer
Buffer name (full path)
nvim_buf_set_name()
Sets the full file name for a buffer.Buffer handle, or 0 for current buffer
Buffer name
Line Operations
nvim_buf_get_lines()
Gets a line-range from the buffer. Indexing is zero-based, end-exclusive.Buffer handle, or 0 for current buffer
First line index
Last line index (exclusive)
Whether out-of-bounds should be an error
Array of lines
nvim_buf_set_lines()
Sets (replaces) a line-range in the buffer. Indexing is zero-based, end-exclusive.Buffer handle, or 0 for current buffer
First line index
Last line index (exclusive)
Whether out-of-bounds should be an error
Array of lines to use as replacement
nvim_buf_get_text()
Gets a range from the buffer (may be partial lines). Indexing is zero-based, row indices are end-inclusive, column indices are end-exclusive.Buffer handle, or 0 for current buffer
First line index
Starting column (byte offset) on first line
Last line index (inclusive)
Ending column (byte offset) on last line (exclusive)
Optional parameters (reserved)
Array of lines
nvim_buf_set_text()
Sets (replaces) a range in the buffer. This is recommended overnvim_buf_set_lines() when only modifying parts of a line, as extmarks will be preserved.
Buffer handle, or 0 for current buffer
First line index
Starting column (byte offset) on first line
Last line index (inclusive)
Ending column (byte offset) on last line (exclusive)
Array of lines to use as replacement
Buffer Variables
nvim_buf_get_var()
Gets a buffer-scoped (b:) variable.Buffer handle, or 0 for current buffer
Variable name
Variable value
nvim_buf_set_var()
Sets a buffer-scoped (b:) variable.Buffer handle, or 0 for current buffer
Variable name
Variable value
nvim_buf_del_var()
Removes a buffer-scoped (b:) variable.Buffer handle, or 0 for current buffer
Variable name
Buffer Marks
nvim_buf_get_mark()
Returns a (row, col) tuple representing the position of the named mark. Marks are (1,0)-indexed.Buffer handle, or 0 for current buffer
Mark name (must be a single char)
(row, col) tuple, (0, 0) if the mark is not set
nvim_buf_set_mark()
Sets a named mark in the given buffer. Marks are (1,0)-indexed.Buffer handle, or 0 for current buffer
Mark name (must be a single char)
Line number (1-indexed)
Column number (0-indexed)
Optional parameters (reserved)
true if the mark was set
nvim_buf_del_mark()
Deletes a named mark in the buffer.Buffer handle, or 0 for current buffer
Mark name (must be a single char)
true if the mark was deleted
Buffer Metadata
nvim_buf_get_changedtick()
Gets a changed tick of a buffer (b:changedtick).Buffer handle, or 0 for current buffer
b:changedtick value
nvim_buf_get_offset()
Returns the byte offset of a line (0-indexed).Buffer handle, or 0 for current buffer
Line index
Integer byte offset, or -1 for unloaded buffer
Buffer Management
nvim_buf_delete()
Deletes a buffer and its metadata (like:bwipeout).
Buffer handle, or 0 for current buffer
Optional parameters:
force: Force deletion, ignore unsaved changesunload: Unload only (:bunload), do not delete
nvim_buf_call()
Calls a function with buffer as temporary current buffer.Buffer handle, or 0 for current buffer
Function to call inside the buffer (Lua only)
Return value of function
Buffer Updates
nvim_buf_attach()
Activates buffer update events on a channel, or as Lua callbacks.Buffer handle, or 0 for current buffer
True if the initial notification should contain the whole buffer
Optional parameters:
on_lines: Callback for line changeson_bytes: Callback for byte changeson_changedtick: Callback for changedtick updateson_detach: Callback for detachon_reload: Callback for buffer reloadutf_sizes: Include UTF-32 and UTF-16 sizespreview: Also attach to command preview
False if attach failed; otherwise true
nvim_buf_detach()
Deactivates buffer-update events on the channel.Buffer handle, or 0 for current buffer
False if detach failed; otherwise true
Buffer Keymaps
nvim_buf_get_keymap()
Gets a list of buffer-local mapping definitions.Buffer handle, or 0 for current buffer
Mode short-name (“n”, “i”, “v”, …)
Array of maparg()-like dictionaries
nvim_buf_set_keymap()
Sets a buffer-local mapping for the given mode.Buffer handle, or 0 for current buffer
Mode short-name
Left-hand-side of the mapping
Right-hand-side of the mapping
Optional parameters (noremap, silent, expr, etc.)
nvim_buf_del_keymap()
Unmaps a buffer-local mapping for the given mode.Buffer handle, or 0 for current buffer
Mode short-name
Left-hand-side of the mapping
See Also
- Core API Functions - Global operations
- Window Functions - Window-specific operations
- Tabpage Functions - Tabpage-specific operations