2

When I start up neovim with a directory as argument, :pwd is still set to the directory from where I ran neovim. I would expect it to have cded to the argument's directory. I wouldn't expect that to happen for file arguments, but for directories, yes. NERDTree does open with the provided directory argument.

I've tried nvim --clean some/project/path too, it gives the same result.

This question is closely related Set current working directory when opening vim

However, running :cd $PWD on VimEnter does not seem to work for me. I've put in init.lua:

local aug_cd_working_dir = vim.api.nvim_create_augroup("__cd_working_dir__", { clear = true }) vim.api.nvim_create_autocmd("VimEnter", { group = aug_cd_working_dir, command = ":cd $PWD", }) 

It does not change the working directory, though. That makes sense, because if neovim cd's to the current directory of the shell on startup, $PWD should be exactly the same and not produce a different result.

Can anyone solve this?

I'm running neovim v0.9.5

8
  • Remark: I can reproduce the behavior for Neovim but not for Vim. Can you tell us which version of Vim you are using? Commented Jan 18, 2024 at 1:45
  • 1
    sure, I'm running nvim 0.9.5. Post edited Commented Jan 18, 2024 at 1:49
  • I'm not sure either program is intended to change directory to a path argument. Perhaps you forgot to mention that you use autochdir? Commented Jan 18, 2024 at 15:51
  • No, I don't use autochdir @D.BenKnoble. I'm wondering why it wouldn't be intended to change directory to a path argument. What's the point of a directory as path argument otherwise? Commented Jan 18, 2024 at 16:37
  • Directories are editable via netrw (and are “just special files”). What would the behavior be if I passed multiple directories? Commented Jan 18, 2024 at 20:50

3 Answers 3

0

I would add the following line to init.vim

let s:hasfolder=1 let g:foldertogo = '.' function s:parse_arguments() let i = 1 while i < len(v:argv) if v:argv[i][0] == '-' let i = i + 2 else let path = v:argv[i] if path[len(path)-1] == '\' || path[len(path)-1] == '/' let path = path[:-2] endif if finddir(escape(fnamemodify(path, ':t'), ' '), escape(fnamemodify(path, ':h'), ' ')) != '' let s:hasfolder = 1 let g:foldertogo = v:argv[i] break else let s:hasfolder = 0 endif let i = i + 1 endif endwhile endfunction call s:parse_arguments() if s:hasfolder augroup FolderToGo autocmd! autocmd VimEnter * :execute "cd " . g:foldertogo augroup END endif 

Here are my observation about the behavior of Vim and Neovim concerning that aspect:

  • Vim 9.1 on Windows change to directory
  • gVim 9.1 on Windows change to directory
  • Vim 9.1 on Linux don-t change to directory
  • gVim 9.1 on Linux don't change to directory
  • Vim 8.2 on Windows change to directory
  • gVim 8.2 on Windows change to directory
  • Neovim 0.95 on Windows don't change to directory
  • Neovim 0.95 on Linux don't change to directory

Summary:

  • It seems it change to folder only for Vim under Windows
8
  • 2
    Thank you for that script! Right, parsing the arguments and changing to a directory, if it was provided. I keep wondering though, isn't that an expected behavior for passing directories anyway and shouldn't vim behave that way by default? Commented Jan 18, 2024 at 12:10
  • Thanks for the feedback :-). If the solution solves your problem maybe you could accept it using the v button next to the arrow voting buttons. It let readers know that the solution is worth reading and it help the question to rest :-) Commented Jan 18, 2024 at 12:41
  • 1
    Yes, it is one solution to the problem. Nevertheless it's really confusing that it's not the default behavior and then I wouldn't need an entire script to achieve that. But probably that's better asked on the respective github repos. Commented Jan 18, 2024 at 13:30
  • 1
    Indeed since the behavior of the original Vim is as you expect from Vim :-) Commented Jan 18, 2024 at 14:07
  • Wait @Vivian, it doesn't work in Vim for me either. I mean calling vim --clean /some/path doesn't change directory. Does it for you? I'm on Vim 9.0. Commented Jan 18, 2024 at 16:40
3

To address this expectation, albeit a bit bluntly:

When I start up neovim with a directory as argument, :pwd is still set to the directory from where I ran neovim. I would expect it to have cded to the argument's directory. I wouldn't expect that to happen for file arguments, but for directories, yes. NERDTree does open with the provided directory argument.

That expectation is wrong, and I can’t think of any Vim documentation that would support it or lead you to that expectation. The editor process’s initial pwd is based on where it is started, like all programs.

Some options to change that based on comments:

  • use 'autochdir' (I don't like it, but you might)
  • (cd the-dir && vim …) to set the directory when launching vim, but keep your shell's directory
  • :cd the-dir as needed
1
  • Thanks for your suggestions! I've come to realize that it's easy to cd to the desired directory once and then save a session. The session can be easily restored with an argument, and then I don't need to cd again to that directory. Commented Jan 19, 2024 at 19:01
2

I use this hack to change Nvim's working directory to the command line argument when provided:

vim.o.autochdir = true vim.schedule(function() vim.o.autochdir = false end) 

However, when the argument points to a directory instead of a file, the working directory will be set to its parent.

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Nov 13 at 14:01
  • Welcome to Vi and Vim and thank you for posting an answer! Commented Nov 13 at 14:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.