Posting just because I looked all over and didn’t see an answer. This function expands its arguments to canonical, absolute file paths, and tests whether one is a string prefix of the other. It also works for checking whether a directory is inside of or is identical to another directory.
local is_file_in_directory = function(file_path, directory_path)
local file = vim.fn.fnamemodify(file_path, ':p')
local dir = vim.fn.fnamemodify(directory_path, ':p')
return file ~= nil and dir ~= nil and
-- is dir an initial substring of file?
file:find(dir, 1, true) == 1
end
This came up because I’m setting up obsidian.nvim which looks like a handy way to get the best of both worlds between Obsidian and Neovim. I’m setting up some custom configuration to automatically change the selected Obsidian workspace when I cd
into vault directory, and to set conceallevel = 1
only on files in a vault, and that requires checking whether the working directory or a file path is inside a given vault directory.
You must log in or register to comment.