How can I start neovim in the directory from the command line?

When I start neovim from my home directory, but pass an argument to a project folder, such as nvim ~/dev/my_project, the working directory is still in my home directory, and netrw shows all my home directory files.

How could I change this behavior, so neovim cd’s into the directory specified when starting neovim?

It’s the same as this question here: neovim - Set current working directory when opening vim - Vi and Vim Stack Exchange
But I am using lua for my configs, and am not sure how to convert this vimscript to lua.

local group_cdpwd = vim.api.nvim_create_augroup("group_cdpwd", { clear = true })
vim.api.nvim_create_autocmd("VimEnter", {
  group = group_cdpwd,
  pattern = "*",
  callback = function()
    vim.api.nvim_set_current_dir(vim.fn.expand("%:p:h"))
  end,
})

Perhaps the use of special plugins is more convenient than this chunk

1 Like

I’m using neovim-session-manager and Windows.

I have some scripts that gives a dir as argument to neovim/neovide.

It may be good to treat this in the autocmd to avoid always changing the cwd.

It can cause conflicts with sessions.

-- Assume that is a directory on the fs gived as argument
if vim.fn.argc() == 1 then
	vim.api.nvim_create_autocmd("VimEnter", {
		pattern = "*",
		callback = function()
			vim.api.nvim_set_current_dir(vim.fn.expand("%:p:h"))
		end
	})
end

Maybe there’s a better way to do this.

What we need to know is not to always change the value of cwd, it can cause problems with plugins that use this internal variable value.

When i run neovim/neovide inside or outside the terminal without arguments, it’s loading automatically my last session, regardless of the current dir that’s being executed. If i give the . operator or any path, the autocmd will be loaded as expected. The session plugin does not load the session as an argument to the executable, which is really cool. It can coexist with this autocmd for now…