With v0.38.0, Textual added a built-in TextArea widget. You probably want to use
that widget instead of this one. This project predated the official widget; versions < v0.8.0
had a completely separate implmentation.
Since v0.8.0, this project uses the built-in TextArea widget, but adds the features outlined below.
Installation
pip install textual-textarea
Features
Full-featured text editor experience with VS-Code-like bindings, in your Textual App:
Syntax highlighting and support for Pygments themes.
Move cursor and scroll with mouse or keys (including ctrl+arrow, PgUp/Dn, ctrl+Home/End).
Open (ctrl+o) and save (ctrl+s) files.
Cut (ctrl+x), copy (ctrl+c), paste (ctrl+u/v), optionally using the system clipboard. On terminals that support the Kitty keyboard protocol, the cmd equivalents work too.
Delete a word with ctrl+backspace, alt+backspace, or alt+delete.
Comment selections with ctrl+/.
Indent and dedent (optionally for a multiline selection) to tab stops with Tab and shift+Tab.
Automatic completions of quotes and brackets.
Select text by double-, triple-, or quadruple-clicking.
Quit with ctrl+q.
Usage
Initializing the Widget
The TextArea is a Textual Widget. You can add it to a Textual
app using compose or mount:
theme (str): Must be name of a Pygments style, e.g., bw, github-dark, solarized-light. Defaults to monokai.
use_system_clipboard (bool): Set to False to make the TextArea's copy and paste operations ignore the system clipboard. Defaults to True. Some Linux users may need to apt-install xclip or xsel to enable the system clipboard features.
read_only (bool): Set to True to prevent the contents from being edited from the keyboard. Defaults to False.
show_cursor (bool): Set to False to hide the cursor (and the shading of the line that contains it), and to scroll the contents like an ordinary container. Defaults to True.
Read-only previews
To show text the user can read, scroll, and copy from, but not edit, pass both
read_only=True and show_cursor=False:
A read_only editor also disables the bindings that open its footer inputs --
save (ctrl+s), open (ctrl+o), find (ctrl+f, F3),
and go to line (ctrl+g) -- and lets escape bubble, so the screen
showing the preview can bind it to dismiss the preview. To keep some of those
bindings, subclass the editor and narrow READ_ONLY_DISABLED_ACTIONS:
class SearchablePreview(TextEditor):
READ_ONLY_DISABLED_ACTIONS = frozenset({"save", "load"})
The TextArea supports many actions and key bindings. For proper binding of ctrl+c to the COPY action,
you must initialize your App with inherit_bindings=False (as shown above), so that ctrl+c does not quit the app. The TextArea implements ctrl+q as quit; you way wish to mimic that in your app so that other in-focus widgets use the same behavior.
Interacting with the Widget
Getting and Setting Text
The TextArea exposes a text property that contains the full text contained in the widget. You can retrieve or set the text by interacting with this property:
The TextEditor exposes a selection property that returns a textual.widgets.text_area.Selection:
editor = self.query_one(TextEditor)
old_selection = editor.selection
editor.selection = Selection((999, 0),(999, 0)) # the cursor will move as close to line 999, pos 0 as possible
cursor_line_number = editor.selection.end[0]
cursor_x_position = editor.selection.end[1]
Getting and Setting The Language
Syntax highlighting and comment insertion depends on the configured language for the TextEditor.
The TextArea exposes a language property that returns None or a string that is equal to the short name of an installed tree-sitter language:
You can subclass TextEditor to add your own behavior. This snippet adds an action that posts a Submitted message containing the text of the TextEditor when the user presses ctrl+j:
from textual.message import Message
from textual_textarea import TextEditor
class CodeEditor(TextEditor):
BINDINGS = [
("ctrl+j", "submit", "Run Query"),
]
class Submitted(Message, bubble=True):
def __init__(self, text: str) -> None:
super().__init__()
self.text = text
async def action_submit(self) -> None:
self.post_message(self.Submitted(self.text))
Works well with
Use together with textual-textarea: extensions, drivers, servers and companion tools.
textualPythie’s Index: 51Modern Text User Interface framework59M downloads/mo
terminaltexteffectsPythie’s Index: 62TerminalTextEffects (TTE) is a terminal visual effects engine.101.8K downloads/mo
textual-devPythie’s Index: 32Development tools for working with Textual526.9K downloads/mo