How do I make ctrl-arrow keys move forward/backward a word at a time in Cygwin bash?

102

37

In the default Cygwin installation, using CTRL-LEFTARROW or CTRL-RIGHTARROW simply prints 5C or 5D instead of skipping a word at a time as expected.

Home/End keys work properly, so remapping should be possible.

Tom

Posted 2011-08-10T01:17:28.673

Reputation: 3 056

Note that this question addresses Cygwin, but can have general application to Unix/Linux systems. If you are using PuTTY and find that the solutions here don't work for you, take a look at a question specifically about what codes to use for PuTTY.

– palswim – 2017-09-27T21:37:42.760

This also happened to me under a full linux; on some advice online, I added a ~/.inputrc file - which annulled all the linux mint defaults. You can get them back by adding $include /etc/inputrc to the top of your ~/.inputrc (assuming that /etc/inputrc is your system-wide inputrc, which it is on Linux Mint) – user208769 – 2019-10-17T14:44:27.273

Answers

141

Found a solution, posting it here for posterity.

Add the following lines to ~/.inputrc (C:\cygwin\home\<username>\.inputrc):

"\e[1;5C": forward-word   # ctrl + right
"\e[1;5D": backward-word  # ctrl + left 

When done you can press C-x C-r to re-read the .inputrc file for the current session.

Things to note if you want other similar customisations: Use 5A and 5B for up and down arrows, and 2x for shifted versions.

The "official" key mappings are described in the Bash Reference Manual, but for a quick way to find a key mapping, in bash:

  1. type Ctrl+V
  2. type the key sequence you are interested in (e.g., Alt+). This would print ^[[1;3C
  3. remove the leading ^[ and replace it with \e to make \e[1;3C
  4. place this value in your .inputrc file.

Tom

Posted 2011-08-10T01:17:28.673

Reputation: 3 056

Some keyboard codes are not showing at all like <kbd>Shift</kbd> + <kbd>Enter</kbd>. How do I find these keyboard codes via Mintty/Windows/Cygwin? – CMCDragonkai – 2016-04-06T12:06:20.777

15

You can reload file .inputrc via:

bind -f ~/.inputrc

Source

Kdt

Posted 2011-08-10T01:17:28.673

Reputation: 151

2This is useful, but not really an answer to the question - perhaps it would be better as a comment to another answer? – Martin Thompson – 2017-01-12T14:29:33.923

Added to @Tom's answer above – dovetalk – 2017-05-20T15:54:37.680

7

In case you want something that'll work without custom environment settings, for instance when working on a shared account on a server or just to limit the amount of custom configuration being used, Bash has built-ins for this that work in Cygwin.

Namely Alt+f to move forward and Alt+b to move backward.

http://www.gnu.org/software/bash/manual/html_node/Commands-For-Moving.html

Costa

Posted 2011-08-10T01:17:28.673

Reputation: 491

4Thanks, though I am aware of those keys and do use them. I just also use the arrow keys when it's convenient and don't want them spewing escape codes instead of doing the sane thing and moving the cursor. – Tom – 2013-03-20T22:23:29.517

0

This solution also works in msys2 consoles:

"\e[1;5C": forward-word   # ctrl + right
"\e[1;5D": backward-word  # ctrl + left 

Just place these key combinations in the .inputrc file.

f0nzie

Posted 2011-08-10T01:17:28.673

Reputation: 101

0

When using rxvt with cygwin I found the solution at this link worked for me: control_arrow_keys_in_rxvt.

Add the following lines to ~/.inputrc :

"\eOd": backward-word
"\eOc": forward-word

Peter Tran

Posted 2011-08-10T01:17:28.673

Reputation: 101

That's when an additional layer is changing the transmitted keys (but it doesn't answer the question asked). – Nikana Reklawyks – 2012-11-03T20:46:35.437