0

As title. I'm trying to fix my plugin with a problem related to the pattern of autocmd. I know that the event DirChangedPre brings a pattern with it when it happens. To reproduce the strange thing I just observed, you can do these steps:

  1. start Neovim, and call :cd path/to/my/config.
  2. create a new tab by :tabnew and run :tcd ./subfolder/. This creates an event DirChangedPre with a pattern tabpage.
  3. jump back to the first tab with :tabp. This creates an event DirChangedPre with a pattern global.

While I expect that 3. would also have a pattern tabpage. Why it gives global? I didn't call cd again.

I only tested this on Neovim.

3
  • 1
    I suspect the reasoning is that, in the first and original tabpage, the working directory is still the "global" working directory. (That is, that tabpage does not have a tabpage-specific working directory, so it keeps the global one.)
    – D. Ben Knoble
    Dec 15, 2022 at 16:00
  • @D.BenKnoble I think that's the answer :) Sorry that I have no time to see your comment while I was asking so I have posted an answer myself. Can I make a conclusion that: every time I jump to tabpage, which I haven't called tcd(nor lcd on the window my cursor at) on it, then internally (neo)vim will run a cd for it to recover my current-directory? Dec 16, 2022 at 10:49
  • 1
    I'm not precisely certain. You could probably check the relevant source code
    – D. Ben Knoble
    Dec 16, 2022 at 23:05

1 Answer 1

0

I believe the reasoning by @D. Ben Knoble in the comment section of my OP is correct:

I suspect the reasoning is that, in the first and original tabpage, the working directory is still the "global" working directory. (That is, that tabpage does not have a tabpage-specific working directory, so it keeps the global one.)
– D. Ben Knoble ♦ Dec 15, 2022 at 16:00


You can detect this global-cd on :tabn and :tabp by the vim.v.event variable and do your tabpage-cd stuff there: (the second param of nvim_create_autocmd)

{
pattern = 'global',
callback = function()
  if vim.v.event.changed_window -- this will be `false` on both `:tabp`,`:tabn`.
    then
    -- ...
  end
end
}

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.