2

To ask in a more precise way: is there a function that works like winnr(), but returns a non-changed value for each window?

I'm trying to use hotkeys to switch between buffers in my vim. It all worked fine. But when I put my cursor in another window like NERDTree's and press the hotkey, NERDTree just disappear and its window switched to another buffer. To fix this, I think I should enable the hotkeys if only if the cursor is in the first window opened with vim. Is there a function like is_first_window() in vim script?

Thank you.

Pinyin
  • 586
  • 4
  • 19
  • No there is no such function but you could check the number of the buffer instead as it likely won't change. Try `bufnr("%")`. You could also check for the name of the buffer, `bufname("%")`. – romainl Dec 26 '12 at 15:11
  • Possibly useful – just [answered](https://stackoverflow.com/a/65280243/2234013) a related question with help from the [NERDTree source](https://github.com/preservim/nerdtree/blob/14af89743ac1c31ff9bb43682025eda50333a7d5/lib/nerdtree/opener.vim#L53-L68) – micimize Dec 13 '20 at 20:27

1 Answers1

1

A window can display any buffer at any time; buffers are not (permanently) attached to particular windows, so there is no such unique ID.

What you want is a way to distinguish special windows (like those showing NERDTree) from regular buffers. The way to do this is via the buffer name, to be obtained via bufname('%'), as romainl has suggested. Your mapping can contain a list of known names of special buffers (like NERD_tree_1), or you could try to generically check for special windows, as most of them have &buftype == 'nofile'. Many plugins also have their own special marker variables, NERDTree for example allows to check for its window like this:

echo exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) == winnr()
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324