Skip to main content

stty.py

A Python library for manipulating terminal settings in the style of POSIX stty(1).


Overview

stty provides a high-level, Pythonic interface to terminal I/O settings, including control characters, baud rates, and window size, using the termios and related modules. It allows you to get, set, save, and restore terminal attributes, and to apply them to file descriptors or pseudo-terminals.


Features

  • Read and modify terminal attributes (iflag, oflag, cflag, lflag, control characters, speeds, window size)
  • Save and load settings to/from JSON files
  • Apply settings to file descriptors or pseudo-terminals
  • Symbolic and string-based access to all settings
  • Emulates many stty(1) features and modes (e.g., raw, evenp, oddp, nl, ek)
  • Cross-platform support (where termios is available)

Requirements and Installation

Python 3.13 or above is required. To install this package, run:

pip install stty

Alternatively, since this library does not have any dependencies outside of the Python standard library, simply place stty.py in your project.


Examples

1. Reading and Printing Terminal Settings

from stty import Stty

# Open terminal and get current settings from stdin (fd=0)
tty = Stty(fd=0)
print(tty)  # Print all settings in a compact form

2. Setting Individual Attributes

from stty import Stty

tty = Stty(fd=0)

# Turn off echo
tty.echo = False

# Set erase character to Ctrl-H
tty.erase = "^H"

# Set input baud rate to 9600
tty.ispeed = 9600

# Set number of rows in the terminal window (if supported)
tty.rows = 40

3. Setting Multiple Attributes at Once

from stty import Stty

tty = Stty(fd=0)

# Set several attributes in one call
tty.set(
    echo=False,
    icanon=False,
    erase="^H",
    ispeed=19200,
    ospeed=19200,
    rows=30,
    cols=100
)

4. Saving and Loading Settings

from stty import Stty

tty = Stty(fd=0)

# Save current settings to a file
tty.save("my_tty_settings.json")

# Later, restore settings from the file
tty2 = Stty(path="my_tty_settings.json")
tty2.tofd(0)  # Apply to stdin

5. Using Raw Mode

from stty import Stty

tty = Stty(fd=0)
tty.raw()         # Set raw mode
tty.tofd(0)       # Apply to stdin

6. Working with Pseudo-terminals

from stty import Stty

tty = Stty(fd=0)
m, s, sname = tty.openpty()  # Open a new pty pair and apply settings to slave
print(f"Master fd: {m}, Slave fd: {s}, Slave name: {sname}")

7. Setting Control Characters

from stty import Stty

tty = Stty(fd=0)

# Set interrupt character to Ctrl-C
tty.intr = "^C"

# Set end-of-file character to Ctrl-D
tty.eof = "^D"

# Set suspend character to DEL
tty.susp = "^?"

8. Querying Settings as a Dictionary

from stty import Stty

tty = Stty(fd=0)
settings = tty.get()
print(settings["echo"])   # True or False
print(settings["erase"])  # e.g., '^H'

9. Using Symbolic Constants

from stty import Stty, cs8, tab0

tty = Stty(fd=0)

# Set character size to 8 bits using symbolic constant
tty.csize = cs8

# Set tab delay to tab0
tty.tabdly = tab0

10. Fork new process with pseudo-terminal

x = stty.Stty(0)
x.intr = "^p"
pid, m, sname  = x.forkpty()

if pid == 0: # Child process
    with open("out", "w") as f:
        f.write(str(stty.Stty(0)))
else: # Parent process
    print(sname)
    print("")
    s = os.open(sname, os.O_RDWR)
    print("Parent:", stty.Stty(s))
    os.close(s)

11. Check equality of (all termios and winsize attributes of) 2 Stty objects

x = stty.Stty(0)
y = stty.Stty(0)

if x.get() == y.get():
    print("equal")
else:
    print("not equal")

12. Check equality of (some termios and winsize attributes of) 2 Stty objects

x = stty.Stty(0)

if x.eq(echo=True, eof="^D")
    print("echo is True and eof is ^D")
else:
    print("echo is False or eof is not ^D")

API Reference

Classes

Stty

Manipulate terminal settings in the style of stty(1).

Constructor:

Stty(fd: int = None, path: str = None, **opts)
  • fd: File descriptor to read settings from.
  • path: Path to JSON file to load settings from.
  • **opts: Any supported terminal attribute as a keyword argument.

Methods:

  • get() -> dict Return dictionary of termios and winsize attributes available on the system mapped to their respective values.

  • set(**opts) Set multiple attributes as named arguments.

  • eq(**opts) Return True if all attributes, which are specified as named arguments, have values equal to those of the corresponding named arguments; return False otherwise.

  • save(path: str = None) Return deep copy of self or save JSON. This mimics "stty -g".

  • load(path: str) Load termios and winsize from JSON file.

  • fromfd(fd: int) Get settings from terminal.

  • tofd(fd: int, when=TCSANOW, apply_termios=True, apply_winsize=True) Apply settings to terminal.

  • evenp(plus=True) Set/unset evenp combination mode.

  • oddp(plus=True) Set/unset oddp combination mode.

  • raw() Set raw combination mode.

  • nl(plus=True) Set/unset nl combination mode.

  • ek() Set ek combination mode.

  • openpty(apply_termios=True, apply_winsize=True) Open a new pty pair and apply settings to slave end.

  • forkpty(apply_termios=True, apply_winsize=True) Call os.forkpty() and apply settings to slave end.

Attribute Access:

  • All terminal attributes (e.g., echo, icanon, erase, ispeed, rows, etc.) are accessible as properties.
  • Setting an attribute updates the internal state and validates the value.

Constants

  • TCSANOW, TCSADRAIN, TCSAFLUSH: the values accepted by the when named argument of Stty.tofd(). Compare with termios.tcsetattr().
  • Symbolic constants for masks and values (e.g., cs8, tab0, etc.) are available as module attributes.

Data

  • settings: A dictionary describing all available Stty attributes and their possible values on the current platform.

Functions

  • settings_help_str: Return help string about all available Stty attributes and their possible values on the current platform.
  • settings_help: Print help message about all available Stty attributes and their possible values on the current platform.

Compatibility

  • Requires Python 3.x and a POSIX-like system with the termios module.
  • Some features depend on platform support (e.g., window size).

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.


Author

Soumendra Ganguly, 2025


See Also

Metadata

Release files for stty 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stty 0.0.6
File Size Uploaded Provenance
stty-0.0.6.tar.gz 51.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stty 0.0.6
File Interpreter ABI Platform
stty-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 90.5 kB

Release files / stty-0.0.6.tar.gz

Download URL stty-0.0.6.tar.gz
Size 51.7 kB
Tags Source
SHA-256 checksum
How to use checksums
de2408cc27a4133e128d704b0b78eee039fe8ddc9592a157beb8d8c34d3b685a
BLAKE2b-256 checksum
How to use checksums
42d7d50c5030160a33071f73cec604560c56cd949f7459672db0f4d2fdd13543
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 12, 2026.

Transparency log

Release files / stty-0.0.6-py3-none-any.whl

Download URL stty-0.0.6-py3-none-any.whl
Size 38.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a166d7a45b70ed6b9385738db6ceec53ad6822e94794ce83a809ad5c497480c
BLAKE2b-256 checksum
How to use checksums
88013a76d32dce4d39100453d9a26f35ffaa74b7248b967a21235703e9e7ac00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 12, 2026.

Transparency log

Security for stty 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stty-0.0.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page