0

I use NERDTree and try to apply the recipe that ensures no one replaces its buffer.

I do the following:

  • Start vim
  • Execute :NERDTreeFocus
  • Open some file
  • Execute :q for file buffer
  • (At this point there is one window with NERDTree only)
  • Open some other file

There I have NERDTree and former file buffers swapped.

While trying to track down the issue I found the following. Suppose we have this autocmd:

autocmd BufEnter * echomsg "Alternate: " . bufnr('#') . " " . bufname('#') | echomsg "Current: " . bufnr('%') . " " . bufname('%')

Then I try:

  1. Start vim
  2. :vs
  3. :e 123
  4. <C-W> j
  5. <C-W> l

After step 3 I see the following in messages:

"123" [New]
Alternate: 1
Current: 2 123

After step 4:

Alternate: -1
Current: 1

After step 5:

Alternate: 1
Current: 2 123

My question is: why do I have Alternate: -1 after step 4 instead of Alternate: 2 123?

1 Answer 1

1

vim stores an alternate file for each window.

  1. when vim starts up a new empty bufnr 1 is initialized
  2. split, now bufnr 1 is in both windows
  3. edit, since bufnr 1 is in both windows, vim must create a new bufnr 2 for the file 123
  4. switching to the initial window, where there was no alternate alternate file (# = -1)
  5. switching back to second window, where alternate file was bufnr = 1

Your Answer

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

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