Pinyin
Pinyin

Reputation: 576

How to distinguish the first created window in vim script?

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.

Upvotes: 2

Views: 143

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172778

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()

Upvotes: 1

Related Questions