# libtmux.neo._best_winlink

- **Module:** libtmux.neo
- **Package:** libtmux
- **Language:** Python
- **Kind:** function
- **Source:** https://github.com/tmux-python/libtmux/blob/38e368c11117fb4aeb2f082d552cd4f210eae06a/src/libtmux/neo.py#L826
- **Page:** https://libtmux.org/en/py/stable/reference/libtmux-neo-_best_winlink/

```
libtmux.neo._best_winlink(rows: OutputsRaw) -> OutputRaw
```

Pick the winlink row tmux would select.

A [`list-windows`](/en/tmux/latest/manual/list-windows/) listing enumerates :term:`winlinks <winlink>` --
``(session, index, window)`` edges -- not windows. [`link-window`](/en/tmux/latest/manual/link-window/) can attach
one window to a session at several indexes at once, so the same
[`window_id`](/en/tmux/latest/catalog/formats/window_id/) may appear on several rows, each with a different
[`window_index`](/en/tmux/latest/catalog/formats/window_index/).

tmux selects the current winlink when it contains the window, otherwise the
first. [`#{window_active}`](/en/tmux/latest/catalog/formats/window_active/) identifies the current row, and the lowest
[`window_index`](/en/tmux/latest/catalog/formats/window_index/) is tmux's first -- chosen explicitly here, so the caller
need not pre-sort the rows.

## Examples

One row is the whole answer:

```python
>>> from libtmux.neo import _best_winlink
>>> _best_winlink([{"window_id": "@0", "window_index": "1"}])["window_index"]
'1'
```

A window linked into one session twice gives two rows. When the session is sitting on the higher-indexed link, that is the one tmux acts on:

```python
>>> _best_winlink([
...     {"window_id": "@0", "window_index": "1", "window_active": "0"},
...     {"window_id": "@0", "window_index": "5", "window_active": "1"},
... ])["window_index"]
'5'
```

When the session is sitting on some *other* window, neither link is current, and tmux falls back to the first:

```python
>>> _best_winlink([
...     {"window_id": "@0", "window_index": "1", "window_active": "0"},
...     {"window_id": "@0", "window_index": "5", "window_active": "0"},
... ])["window_index"]
'1'
```

The fallback reads the lowest index, not the first row, so a listing that happened to arrive high-index-first still answers tmux's first:

```python
>>> _best_winlink([
...     {"window_id": "@0", "window_index": "5", "window_active": "0"},
...     {"window_id": "@0", "window_index": "1", "window_active": "0"},
... ])["window_index"]
'1'
```

## Parameters

- `rows` (`OutputsRaw`): Non-empty rows for one object id in one session. Order does not
matter: the current winlink wins, otherwise the lowest
``window_index``.

## Returns

OutputRaw
    The row naming the winlink tmux would act on.
