Neovim Get Current File Path Link to heading

Vim has the following filename-modifiers

  • % - Relative path
  • %:p - Absolute path
  • %:h - Relative path of parent folder
  • %:p:h - Absolute path of parent folder

:help filename-modifiers

Lua Link to heading

Get current file path into Lua inside Neovim

local fullpath = vim.fn.expand("%:p")
local relPath = vim.fn.expand("%")

This is useful for determining path based logic for any Lua function

-- example
is_template = function()
  local fullpath = vim.fn.expand("%:p:h")
  return string.match(fullpath, "templates")
end,