zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.79k stars 1.16k forks source link

Command+Arrows on mac #1898

Open Pandawan opened 3 years ago

Pandawan commented 3 years ago

Description of the problem or steps to reproduce

When I use zsh or nano, I can do command+left or command+right to place the cursor at the start or end of the line respectively (this works in all observable cases). I believe this is the "normal" behavior for macOS text editing as it seems to be the case for all apps.

Yet this does not seem to work in micro, when I do command+left, the cursor will go to the start of the line, but it will also select that text. While when I do command+right, it opens the micro command input (as if I pressed control+e).

I know micro remaps command to control on macOS as stated in the readme, but I can't use control-left and control-right instead because those are set (by default) to switching between different desktops/full screen windows through mission control. So I'm wondering if I'm missing the way to fix this issue (perhaps by letting micro take whatever key combination the terminal is giving it), or if this isn't supported at all by micro.

Specifications

I'm using iTerm 2 with the "Natural Text Editing" key mapping, which sets

Commit hash: 5044ccf (v2.0.7) OS: macOS Catalina 10.15.7 Terminal: iTerm2 v3.3.12

totoroot commented 3 years ago

Since Ctrl+Right and Ctrl+Left are used for switching between desktops on macOS, they cannot be used inside micro and are therefore remapped to Fn+Right and Fn+Left. This works for me in all installed terminal emulators including iTerm2. If you are using an external keyboard without the function key, you have to remap some keys within iTerm2 or modify the keybindings for micro itself. With an external keyboard you could also use Home and End keys if they are present.

Can you confirm that?

Pandawan commented 3 years ago

Yes the Fn+Right and Fn+Left do work, but I'm saying that in typical macOS text inputs, you use Cmd+Right and Cmd+Left, which works fine in terminal and in terminal-based editors like nano, yet when I use it on micro this is not the case.

So I'm wondering if there's some kind of setting (or perhaps this might need a PR) to allow micro to read Cmd+Right and Cmd+Left as the current "Ctrl+Right" and "Ctrl+Left" behavior. That is, I'd like to have the same behavior as typical macOS text inputs and even other terminal-based editors.

Here's a video demonstrating the different behavior.

zyedidia commented 3 years ago

When you use the natural editing present, this causes iTerm2 to send 0x5 for command+right and 0x1 for command+1 (as you noted in the original post). These hex codes have the following meanings:

In nano and bash (and probably other applications), ctrl+a and ctrl+e are bound to beginning and end of line which is what you want. In micro, these keys do other things. You probably want to remap command+right and command+left to send the same hex codes as ctrl+right and ctrl+left rather than ctrl+e and ctrl+a:

Alternatively, you can remap ctrl+a and ctrl+e in micro to jump to the beginning and end of the line.

memeplex commented 3 years ago

Since Ctrl+Right and Ctrl+Left are used for switching between desktops on macOS, they cannot be used inside micro and are therefore remapped to Fn+Right and Fn+Left.

This is not correctly documented in defaultkeys:

| Alt(Ctrl on Mac)-LeftArrow | Move to the beginning of the current line | | Alt(Ctrl on Mac)-RightArrow | Move to the end of the current line |

zyedidia commented 3 years ago

It is correctly documented. On Mac by default Ctrl-Left and Ctrl-Right are bound to those actions. Depending on the settings in your terminal emulator it may send those events when you press Fn-Left or Fn-Right, but that isn't necessarily the case. If you want it to work properly you can just disable the switch desktops via ctrl-arrow keys or rebind it. See system preferences->keyboard->shortcuts->mission control.

battaglia01 commented 2 months ago

In iTerm2, this can be solved by mapping Cmd-Left to ^[ [H (home) and Cmd-Right to ^[ [F (end), rather than the defaults of Ctrl-A and Ctrl-E.

andrievsky commented 2 months ago

This do the trick:

"Ctrl-a": "StartOfLine",
"Ctrl-e": "EndOfLine"

I would really prefer this behaviour by default. Thanks for the help, folks.