18

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

I need to get the path of the file loaded in the current buffer before I can execute the function. I have been unable to find how to do this after reading the Neovim Lua API documentation.

So far I've found that vim.api.nvim_get_current_buf() returns the current buffer number, but how do I get the full file path of the current buffer?

Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
Smit Patel
  • 181
  • 1
  • 1
  • 3

3 Answers3

29

You can access the full path to the file in the current buffer using vim.api.nvim_buf_get_name(0), where 0 for buffer id means "the current buffer".

https://neovim.io/doc/user/api.html#nvim_buf_get_name()

Isti115
  • 2,418
  • 3
  • 29
  • 35
20

The function you are looking for is:

vim.fn.expand('%')

% is expanded to the current filename.

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

Anas sheshai
  • 375
  • 1
  • 4
8
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)

Jongwook Choi
  • 8,171
  • 3
  • 25
  • 22