Automatically set the cwd (without rooter plugin)

· Nano Tips for Vim


nvim 0.10 introduces 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,