How to get the current buffer path using neovim Lua API?

Viewed 8129

I am using neovim 0.5 and want to write a custom function to use with telescope.nvim.

I need to get the file path of the current buffer I execute the function. I have been unable to find how to do this in the nvim Lua API docs.

vim.nvim_get_current_buf gets the buffer number, how do I get the full file path of the current buffer?

3 Answers

The function you are looking for is:

vim.fn.expand('%')

% is expanded to the current filename.

See :help expand() for more wildcards like %.

vim.fn.expand('%:p') 

Gives the full path regardless of the current path (CWD). Without the %:p modifier, it might give only the relative path. See :help filename-modifiers (or %:p)

Related