From 0bbeae73dfee86381f59717027c6c6318898f20a Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Wed, 4 Mar 2026 23:37:37 +1100 Subject: [PATCH] fix: advance cursor during `cmd-transpose` (#2413) * fix: advance cursor during `cmd-transpose` * Update `CHANGELOG.md` --- CHANGELOG.md | 1 + doc.md | 10 ++++++++-- doc.txt | 12 ++++++++++-- eval.go | 21 +++++++++++++++++---- lf.1 | 14 +++++++++++--- 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 336e18ab..a9a8c164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - The `on-load` hook command now ignores `.git` directories to reduce flicker and repeated `on-load` triggers (#2382). - Preview messages like `empty` or `loading...` have their alignment improved (#2400). - A bug where the `loading...` message was not displayed for volatile previews after the first time is now fixed (#2410). +- The `cmd-transpose` command now advances the cursor correctly after swapping characters (#2413). ## [r41](https://github.com/gokcehan/lf/releases/tag/r41) diff --git a/doc.md b/doc.md index b222a742..85e92c36 100644 --- a/doc.md +++ b/doc.md @@ -811,9 +811,15 @@ Delete the previous Unix word. Paste the buffer content containing the last deleted item. -## cmd-transpose (default ``), cmd-transpose-word (default ``) +## cmd-transpose (default ``) -Transpose the positions of the last two characters/words. +Swap the characters before and after the cursor, then move the cursor forward. +If there is no character after the cursor, swap the previous two characters instead. + +## cmd-transpose-word (default ``) + +Swap the words before and after the cursor, then move the cursor forward. +If there is no word after the cursor, swap the previous two words instead. ## cmd-word (default ``), cmd-word-back (default ``) diff --git a/doc.txt b/doc.txt index 1a22011d..1b7c8184 100644 --- a/doc.txt +++ b/doc.txt @@ -862,9 +862,17 @@ cmd-yank (default ) Paste the buffer content containing the last deleted item. -cmd-transpose (default ), cmd-transpose-word (default ) +cmd-transpose (default ) -Transpose the positions of the last two characters/words. +Swap the characters before and after the cursor, then move the cursor +forward. If there is no character after the cursor, swap the previous +two characters instead. + +cmd-transpose-word (default ) + +Swap the words before and after the cursor, then move the cursor +forward. If there is no word after the cursor, swap the previous two +words instead. cmd-word (default ), cmd-word-back (default ) diff --git a/eval.go b/eval.go index 541f7ab3..15b15157 100644 --- a/eval.go +++ b/eval.go @@ -1978,12 +1978,25 @@ func (e *callExpr) eval(app *app, _ []string) { app.ui.cmdAccLeft += app.ui.cmdYankBuf update(app) case "cmd-transpose": - r := []rune(app.ui.cmdAccLeft) - if len(r) < 2 { + var c []string + gr := uniseg.NewGraphemes(app.ui.cmdAccLeft) + for gr.Next() { + c = append(c, gr.Str()) + } + + first := firstGraphemeCluster(app.ui.cmdAccRight) + if first != "" { + c = append(c, first) + } + + if len(c) < 2 { return } - r[len(r)-1], r[len(r)-2] = r[len(r)-2], r[len(r)-1] - app.ui.cmdAccLeft = string(r) + + app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) + + c[len(c)-1], c[len(c)-2] = c[len(c)-2], c[len(c)-1] + app.ui.cmdAccLeft = strings.Join(c, "") update(app) case "cmd-transpose-word": if app.ui.cmdAccLeft == "" { diff --git a/lf.1 b/lf.1 index bf01f169..2dc1fbb8 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.7.0.2 .\" -.TH "LF" "1" "2026\-02\-28" "r41" "DOCUMENTATION" +.TH "LF" "1" "2026\-03\-04" "r41" "DOCUMENTATION" .SH NAME lf \- terminal file manager .SH SYNOPSIS @@ -783,8 +783,16 @@ Delete everything up to the beginning/end of the line. Delete the previous Unix word. .SS cmd\-yank (default \f[CR]\f[R]) Paste the buffer content containing the last deleted item. -.SS cmd\-transpose (default \f[CR]\f[R]), cmd\-transpose\-word (default \f[CR]\f[R]) -Transpose the positions of the last two characters/words. +.SS cmd\-transpose (default \f[CR]\f[R]) +Swap the characters before and after the cursor, then move the cursor +forward. +If there is no character after the cursor, swap the previous two +characters instead. +.SS cmd\-transpose\-word (default \f[CR]\f[R]) +Swap the words before and after the cursor, then move the cursor +forward. +If there is no word after the cursor, swap the previous two words +instead. .SS cmd\-word (default \f[CR]\f[R]), cmd\-word\-back (default \f[CR]\f[R]) Move the cursor by one word in the forward/backward direction. .SS cmd\-delete\-word (default \f[CR]\f[R])