Automatically set the cwd (without rooter plugin)

· Nano Tips for Vim


Nvim 0.10 introduced vim.fs.root, which basically replaced the need for a rooter-plugin for me. Automatically setting the root is now trivial: The snippet below sets the cwd to the parent of the buffer that has a .git or Makefile.

1vim.api.nvim_create_autocmd("BufEnter", {
2	callback = function(ctx.buf)
3		local root = vim.fs.root(ctx.buf, {".git", "Makefile"})
4		if root then vim.uv.chdir(root) end
5	end,
6})

Edit (2024-09-05):
I've been told that for some people, vim.uv.chdir does not work reliably, but vim.fn.chdir does. I have no idea why that is the case though, but if you are having trouble, it's worth a try to change it.