Skip to content

Terminal escape sequences displayed on screen when running Neovim in SSH session #76

@inureyes

Description

@inureyes

Problem / Background

When running Neovim after connecting via SSH, strange text appears at the bottom of the screen:

:159c/16e0/18490+r54631+r524742=380+r736574726762660+r736574726762621$r0;48:2:1:1:2:3m1+r4D73=1B5D35323B25703125733B257032257307

This occurs because terminal escape sequence responses are not being properly handled.

Root Cause Analysis

The displayed text consists of raw terminal escape sequence responses:

  • +r pattern: XTGETTCAP response (terminal capability query response)
  • 736574726762 -> hex decoded: "setrgb" (true color support query)
  • 48:2:1:1:2:3m: SGR true color escape sequence
  • 1B5D35323B...: OSC (Operating System Command) sequence

When Neovim starts, it sends several queries to detect terminal capabilities:

  1. XTGETTCAP (terminfo capability query)
  2. DA1/DA2/DA3 (Device Attributes)
  3. Kitty keyboard protocol support
  4. True color support

When PTY is not properly configured in SSH sessions, or terminal responses are mixed with stdout, raw sequences appear on screen.

Proposed Solution

  1. Properly configure terminal modes during PTY allocation (raw mode, echo settings, etc.)
  2. Correctly pass terminal type (TERM environment variable)
  3. Properly handle terminal query/response cycles in SSH channel
  4. Improve PTY settings for interactive sessions

Acceptance Criteria

  • Neovim launches cleanly without displaying raw escape sequences
  • Terminal capability queries are properly handled in the PTY layer
  • TERM environment variable is correctly propagated to remote session
  • Interactive applications (vim, htop, etc.) work without visual artifacts
  • Raw terminal responses do not leak into visible output

Technical Considerations

  • Review PTY allocation in src/ssh/ modules
  • Ensure proper terminal mode flags (ECHO, ICANON, etc.) are set
  • Consider handling XTGETTCAP and DA responses at the SSH client level
  • May need to buffer and filter terminal responses before display

Implementation (PR #77)

The fix adds an escape sequence filter module (escape_filter.rs) that uses a state machine to:

  • Filter XTGETTCAP responses (\x1bP+r...)
  • Filter DA1/DA2/DA3 responses (\x1b[?...c)
  • Filter OSC responses (\x1b]...)
  • Filter DCS responses (\x1bP...)
  • Preserve valid escape sequences for colors, cursor movement, etc.

Additionally, TERM and COLORTERM environment variables are now set before PTY allocation.

Additional Context

Related closed issues:

This issue may require revisiting the PTY implementation to ensure proper terminal emulation.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions