Skip to content

Commit 144deb8

Browse files
authored
Merge pull request #4284 from davep/ctrl-k-kill-empty-line
Change `TextArea` to delete empty line on ctrl+k
2 parents 18aa36b + 611ae5d commit 144deb8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4848
- BREAKING: `AppFocus` and `AppBlur` are now posted when the terminal window gains or loses focus, if the terminal supports this https://github.com/Textualize/textual/pull/4265
4949
- When the terminal window loses focus, the currently-focused widget will also lose focus.
5050
- When the terminal window regains focus, the previously-focused widget will regain focus.
51+
- TextArea binding for <kbd>ctrl</kbd>+<kbd>k</kbd> will now delete the line if the line is empty https://github.com/Textualize/textual/issues/4277
5152

5253
## [0.52.1] - 2024-02-20
5354

src/textual/widgets/_text_area.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ class TextArea(ScrollView):
219219
Binding(
220220
"ctrl+u", "delete_to_start_of_line", "delete to line start", show=False
221221
),
222-
Binding("ctrl+k", "delete_to_end_of_line", "delete to line end", show=False),
222+
Binding(
223+
"ctrl+k",
224+
"delete_to_end_of_line_or_delete_line",
225+
"delete to line end",
226+
show=False,
227+
),
223228
Binding("ctrl+z", "undo", "Undo", show=False),
224229
Binding("ctrl+y", "redo", "Redo", show=False),
225230
]
@@ -2112,6 +2117,26 @@ def action_delete_to_end_of_line(self) -> None:
21122117
to_location = self.get_cursor_line_end_location()
21132118
self._delete_via_keyboard(from_location, to_location)
21142119

2120+
async def action_delete_to_end_of_line_or_delete_line(self) -> None:
2121+
"""Deletes from the cursor location to the end of the line, or deletes the line.
2122+
2123+
The line will be deleted if the line is empty.
2124+
"""
2125+
# Assume we're just going to delete to the end of the line.
2126+
action = "delete_to_end_of_line"
2127+
if self.get_cursor_line_start_location() == self.get_cursor_line_end_location():
2128+
# The line is empty, so we'll simply remove the line itself.
2129+
action = "delete_line"
2130+
elif (
2131+
self.selection.start
2132+
== self.selection.end
2133+
== self.get_cursor_line_end_location()
2134+
):
2135+
# We're at the end of the line, so the kill delete operation
2136+
# should join the next line to this.
2137+
action = "delete_right"
2138+
await self.run_action(action)
2139+
21152140
def action_delete_word_left(self) -> None:
21162141
"""Deletes the word to the left of the cursor and updates the cursor location."""
21172142
if self.cursor_at_start_of_text:

0 commit comments

Comments
 (0)