Skip to content

Selection is not done at the correct offset when the text contains some tabs #6454

@paulcacheux

Description

@paulcacheux

Have you checked closed issues? (https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed)
yes

Have you checked against the most recent version of Textual? (https://pypi.org/search/?q=textual)
yes

The bug

When some text is selected and the text contains some tabs, the selected text is not at the correct offset.
Here is a video showing the issue

Enregistrement.de.l.ecran.2026-03-28.a.21.44.52.mov

here is also an example unit test showing the failure:

async def test_tab_selection():
    """Test that selection works correctly with tab characters.

    The text ``\\thello\\tworld`` expands (tab_size=8) to::

        "        hello   world"
         col 8---^   ^---col 12

    Dragging from column 8 to 12 should select "hello", not shifted text.
    """

    TEXT = "\thello\tworld"

    class TabSelectApp(App):
        def compose(self) -> ComposeResult:
            yield Static(TEXT)

    app = TabSelectApp()
    async with app.run_test() as pilot:
        await pilot.pause()
        assert await pilot.mouse_down(offset=(8, 0))
        await pilot.pause()
        assert await pilot.mouse_up(offset=(12, 0))
        selected_text = app.screen.get_selected_text()

    assert selected_text == "hello"

I think the main issue is that tab expansion is done after the selection style application. Here is a suggested fix https://github.com/paulcacheux/textual/tree/fix/selection-tab-expansion

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions