Skip to content

Is there a way to set the root directory to the directory of the currently focused buffer? #1932

Answered by alex-courtis
dpetka2001 asked this question in Q&A
Discussion options

You must be logged in to vote

You can try something like this:

local function my_startup(data)

  -- is the buffer a real file?
  if vim.fn.filereadable(data.file) ~= 1 then
    return
  end

  -- is it in a real directory
  local dir = vim.fn.fnamemodify(data.file, ":h")
  if vim.fn.isdirectory(dir) == 0 then
    return
  end

  -- change directory
  vim.cmd.cd(dir)
end

vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = my_startup })

See #1669 (comment) for more ideas.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dpetka2001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants