@@ -50,6 +50,8 @@ type KeyMap struct {
50
50
UppercaseWordForward key.Binding
51
51
LowercaseWordForward key.Binding
52
52
CapitalizeWordForward key.Binding
53
+
54
+ TransposeCharacterBackward key.Binding
53
55
}
54
56
55
57
// DefaultKeyMap is the default set of key bindings for navigating and acting
@@ -75,6 +77,8 @@ var DefaultKeyMap = KeyMap{
75
77
CapitalizeWordForward : key .NewBinding (key .WithKeys ("alt+c" )),
76
78
LowercaseWordForward : key .NewBinding (key .WithKeys ("alt+l" )),
77
79
UppercaseWordForward : key .NewBinding (key .WithKeys ("alt+u" )),
80
+
81
+ TransposeCharacterBackward : key .NewBinding (key .WithKeys ("ctrl+t" )),
78
82
}
79
83
80
84
// LineInfo is a helper for keeping track of line information regarding
@@ -481,6 +485,24 @@ func (m *Model) deleteAfterCursor() {
481
485
m .SetCursor (len (m .value [m .row ]))
482
486
}
483
487
488
+ // transposeLeft exchanges the runes at the cursor and immediately
489
+ // before. No-op if the cursor is at the beginning of the line. If
490
+ // the cursor is not at the end of the line yet, moves the cursor to
491
+ // the right.
492
+ func (m * Model ) transposeLeft () {
493
+ if m .col == 0 || len (m .value [m .row ]) < 2 {
494
+ return
495
+ }
496
+ if m .col >= len (m .value [m .row ]) {
497
+ m .SetCursor (m .col - 1 )
498
+ }
499
+ m .value [m .row ][m .col - 1 ], m.value [m.row ][m.col ] =
500
+ m.value [m.row ][m.col ], m .value [m .row ][m .col - 1 ]
501
+ if m .col < len (m .value [m .row ]) {
502
+ m .SetCursor (m .col + 1 )
503
+ }
504
+ }
505
+
484
506
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
485
507
// the cursor blink should be reset.
486
508
func (m * Model ) deleteWordLeft () {
@@ -847,6 +869,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
847
869
m .uppercaseRight ()
848
870
case key .Matches (msg , m .KeyMap .CapitalizeWordForward ):
849
871
m .capitalizeRight ()
872
+ case key .Matches (msg , m .KeyMap .TransposeCharacterBackward ):
873
+ m .transposeLeft ()
850
874
851
875
default :
852
876
if m .CharLimit > 0 && rw .StringWidth (m .Value ()) >= m .CharLimit {
0 commit comments