1

I'm using GNU Nano 8.2 in ZSH on macOS Sonoma and I'm trying to figure out why the standard keybinding to move the cursor back or forward one word, ^ + arrow keys, doesn't work.

This happens both in the default Terminal app and iTerm2. I'm using a French physical keyboard but I've tried setting it both to French and US keyboard.

Thanks

7
  • 1
    What does Ctrl-Cursor Key do instead? Commented Nov 16, 2024 at 20:58
  • Hey, they just do nothing Commented Nov 16, 2024 at 21:18
  • Hey, I'm a bit confused with your key combo! So you want your cursor to go back or forward for one word using ^ + 'left or right' arrow keys? To go back I use cmd + the arrows! Commented Nov 16, 2024 at 21:35
  • It seems to be the default shortcut in Nano to move back or forward one word, according to the manual and online resources. As an alternative, I'd like to rebind this keybinding to opt+arrow keys, the default keybinding to do that in macOS. Setting modifier keys to work properly in iTerm or Terminal is confusing though, and I'm not sure what I should write in my .nanorc file. Commented Nov 16, 2024 at 22:09
  • What does the list of keys found by pressing F1 or help show? They are OK in my setup - they also show M-space and ^Space for moving by word. Commented Nov 17, 2024 at 12:05

2 Answers 2

1

Older versions of Nano didn't support Ctrl+arrow bindings out of the box, or not on all terminals. Recent versions should work, though. The version shipped with macOS is an antique 2.0.0 that doesn't support those keys and doesn't support configurable key bindings, so you're out of luck. But according to the package list, Ventura (13.0) stopped shipping Nano.

If you've installed Nano through Brew/Macports/Fink/…, make sure that you're invoking that one and not the one bundled with macOS. Run nano --version to check the version of the instance that you're calling. In zsh, run

type -a nano 

to see where the nano executable(s) are. If this outputs multiple lines, the one that takes effect is the first line. If the output is something like

nano is /bin/nano nano is /opt/homebrew/bin/nano 

then you need to arrange your command search path differently. Run

print -lr $path 

to list your command search path. You can change it by modifying PATH or path in your ~/.zprofile.

If your Nano is recent enough (at least 2.1.0) but doesn't recognize Ctrl+arrow, you can set them up in your ~/.nanorc (that's the file called .nanorc, with a leading dot, in your home directory). Find out what escape sequences those key chords send in your terminal. (See https://unix.stackexchange.com/questions/47312/control-and-up-down-keys-in-terminal-for-use-by-emacs/47402#47402 and https://superuser.com/questions/357355/how-can-i-get-controlleft-arrow-to-go-back-one-word-in-iterm2 for similar answers and https://unix.stackexchange.com/questions/116629/how-do-keyboard-input-and-text-output-work/116630#116630 for a more thorough explanation of escape sequences.) At the zsh prompt, press Ctrl+V then Ctrl+Left then Ctrl+C. This inserts multiple characters, where the first character is the control character Escape, represented visually as ^[ (often in a different color). In your .nanorc, you'll need to use the two characters ^[. For example, if you see

^[[1;5D 

(which is a common escape sequence for Ctrl+Left, and AFAIK is the one that both Terminal and iTerm2 send out of the box), you'll need to pass ^[[1;5D to the bind command in .nanorc. Repeat for Ctrl+Right and any other cursor key you want to bind. You can find command names in the nanorc manual (man nanorc on your system). Thus your nanorc should contain:

bind ^[[1;5D prevword bind ^[[1;5C nextword 
1
  • Hey, thanks for the very thorough answer! Unfortunately these bind instructions do not seem to be working in my .nanorc file: they show up as red and when i reopen Nano I'm getting an error message that says there's something wrong in the .nanorc file. Anyways, in the meantime I came upon this, which probably means that these arrow keys can't be rebound : "Rebinding any of the dedicated cursor-moving keys (the arrows, Home, End, PageUp and PageDown) is not possible" Commented Nov 18, 2024 at 13:26
0

Ctrl + arrow keys are bound to change a workspace by default on macOS via shortcuts.

You will need to disable/change the shortcut:

System settings > Keyboard > Keyboard Shortcuts > Mission Control (Move left / right a space)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.