0

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? Jan 18 at 1:45
  • 1
    sure, I'm running nvim 0.9.5. Post edited
    – Jost
    Jan 18 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?
    – D. Ben Knoble
    Jan 18 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?
    – Jost
    Jan 18 at 16:37
  • Directories are editable via netrw (and are “just special files”). What would the behavior be if I passed multiple directories?
    – D. Ben Knoble
    Jan 18 at 20:50

2 Answers 2

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
  • 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?
    – Jost
    Jan 18 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 :-) Jan 18 at 12:41
  • 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.
    – Jost
    Jan 18 at 13:30
  • Indeed since the behavior of the original Vim is as you expect from Vim :-) Jan 18 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.
    – Jost
    Jan 18 at 16:40
0

Some options 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.
    – Jost
    Jan 19 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.