How do I edit keyboard shortcuts in Nano?

Solution 1:

If you install a more up-to-date version of nano, it has improved keyboard navigation. In nano 2.7, the move word shortcut is ctrl+left/right. So you might not even need to do keybindings if you are satisfied with ctrl instead of alt.

  1. Install homebrew: https://brew.sh/
  2. Install nano: brew install nano

If you really want alt, then keep reading.

  1. If using Mac Terminal, you will need to enable the Use Option as Meta key setting. Otherwise the alt key won't work.

  2. /etc/nanorc might be ignored, depending on the flags when your nano was compiled. You can use ~/.nanorc instead, that will work for sure.

  3. Add these lines to ~/.nanorc. (It seems that nanorc does not allow left/right arrows for keybindings, so I chose , and . since they are the same keys as < and >.

    bind M-, prevword main
    bind M-. nextword main
    

Now alt+, and alt+. will move to prev/next words.

You can find the full list of commands here: https://www.nano-editor.org/dist/v2.7/nanorc.5.html#REBINDING%20KEYS . (You may want to find the exact version of your nano if not v2.7).

Solution 2:

You can find a description of commands/settings allowed in /etc/nanorc by running man nanorc.

OS X 10.11-10.15 uses nano 2.0.6 which doesn't seem to support key rebindings.

However if you install Homebrew you can easily install a modern version of Nano with:

# brew install nano
... wait for install ...

# /usr/bin/nano --version
GNU nano version 2.0.6 (compiled 17:24:29, Jun  5 2020)

# /usr/local/bin/nano --version
GNU nano, version 5.3