Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option-left / alt-left should skip between words #59

Closed
slingamn opened this issue May 5, 2024 · 18 comments
Closed

option-left / alt-left should skip between words #59

slingamn opened this issue May 5, 2024 · 18 comments

Comments

@slingamn
Copy link
Member

slingamn commented May 5, 2024

As discussed here: wader/fq#940 this is a normal readline feature. I checked our v0.0.1 (last upstream release) and it does not work there, so it's not a regression, it was never implemented.

@wader
Copy link
Collaborator

wader commented May 6, 2024

Sorry i was confused about keys, it't not cmd+left/right, it's alt+left/right. For me it stopped working after 727da2a, maybe we're testing differently? i'm using iterm2 on macOS.
Looking at the default profile key setting it says:
alt+left sends ^[b
alt+right send ^[f

Maps quite well to what was removed here 727da2a#diff-8b7f25bac2ae04c2ce2abb628c6aa28103fd113103a62f3e9d232fa995684915L174 got lost in the refactor somehow?

@slingamn
Copy link
Member Author

slingamn commented May 6, 2024

Hmm, that makes sense. I was testing on Linux, not MacOS --- I'll try it in iterm2 when I get a chance. (Alt-Left and Alt-Right work for me with GNU Readline in the same Linux terminal, e.g. with the CPython REPL, but they do not work with v0.0.1.)

@wader
Copy link
Collaborator

wader commented May 7, 2024

Tried hackily to add this in consumeANSIEscape

	switch initial {
	case 'f':
		return readResult{r: MetaForward, ok: true}, nil
	case 'b':
		return readResult{r: MetaBackward, ok: true}, nil
	}

seems to work but i'm a bit unsure how to refactor/restructure things to do it in a sane way

@slingamn
Copy link
Member Author

slingamn commented May 8, 2024

Just clarifying, the sequence is the ANSI escape \x1b followed immediately by b or f, and that's the whole sequence?

@wader
Copy link
Collaborator

wader commented May 8, 2024

Yeap i think so

@wader
Copy link
Collaborator

wader commented Jun 26, 2024

Got annoyed with this again today 😬 any suggestion how to fix? my change felt a bit too hacky

slingamn added a commit that referenced this issue Jun 30, 2024
enable option-left and option-right on MacOS (maybe)
@slingamn
Copy link
Member Author

Sorry about the delay. Your implementation strategy looks fine to me :-) I pushed a version of it here:

It did not work for me in iTerm2 3.5.2 with default settings. Here are the keybindings I see with default settings:

screenshot

According to this, option-right would actually send \x1b\x1b[C, which is consistent with what I see in iTerm2 before and after this patch (option-right prints [C).

I implemented a hack to read sequences of this form and it works for me in iTerm2 and also on Linux with urxvt, although I haven't tested other Linux terminals yet.

What I would really like is some documentation about these sequences and also more clarity about sequences that start with a double \x1b. As it is, the patch linked above is safe to merge (it shouldn't break any existing functionality) but I don't feel like I can changelog it as "option-left now skips between words on MacOS", since it didn't actually work in my iTerm2 :-)

@slingamn
Copy link
Member Author

I found a project dealing with a similar issue, although it didn't clarify things to my satisfaction: c-bata/go-prompt#63 (comment)

@slingamn
Copy link
Member Author

It looks like the "Natural text editing" presets in iTerm2 enable the short sequences \x1bb and \x1bf? So e9aabe5 seems OK to merge then, although it might be nice to fix this on Linux as well for parity with GNU Readline.

@slingamn
Copy link
Member Author

GNU Readline also accepts Alt-b and Alt-f for this, apparently.

@slingamn
Copy link
Member Author

slingamn commented Jul 1, 2024

Alt-b and Alt-f in xterm/urxvt produce \x1bb and \x1bf respectively.

@slingamn
Copy link
Member Author

slingamn commented Jul 1, 2024

Someone told me that in general with xterm, combining alt with a key will produce an additional \x1b in front of whatever sequence that key would normally produce. This explains the alt-right and alt-left behaviors.

slingamn added a commit that referenced this issue Jul 1, 2024
Enable "next word" and "previous word" in many terminals:

* Alt-LeftArrow and Alt-RightArrow in xterm and iTerm2 with default settings,
  where they translate to \x1b\x1b[D and \x1b\x1b[C respectively
* Alt-LeftArrow and Alt-RightArrow in iTerm2 in "Natural text editing mode",
  where they translate to \x1bb and \x1bf
* Alt-b and Alt-f in xterm, where they translate to \x1bb and \x1bf
@wader
Copy link
Collaborator

wader commented Jul 1, 2024

Sorry for delay also, out traveling a bit. Think i'm using "Natural text editing" with iterm2 also. Nice research and it sounds like you have figure out how to make opt|alt+left/right work in most popular terminals?

@wader
Copy link
Collaborator

wader commented Jul 4, 2024

🥳 planning a new release soonish?

@slingamn
Copy link
Member Author

slingamn commented Jul 4, 2024

Yeah I tagged this as 0.1.2-rc1, I'll probably tag 0.1.2 in a day or two but you can also pick up the rc1 in fq and dogfood it :-)

@wader
Copy link
Collaborator

wader commented Jul 4, 2024

rc1 seems to work fine and all tests pass, but the readline-parts are not exercised much

@slingamn
Copy link
Member Author

slingamn commented Jul 4, 2024

Nice, I felt pretty confident so I went ahead and tagged v0.1.2 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants