42

I searched and couldn't find a solution for this anywhere. I'm using PuTTY from Windows to connect to various servers where I run bash and screen. It seems bash works fine with ctrl-arrow keys to jump word-to-word on the command line but within screen it's not working. Not in screen, ctrl-left sends "^[OC and ctrl-right is "^[OD". Within screen I instead get "^[[C" and "^[[D", which appears to be the codes for just the left/right arrow keys. Is there any way to get screen to recognize ctrl-arrow keys when using PuTTY? (FYI, I don't remember having this problem when using gnu-terminal in linux instead of PuTTY).

UPDATE: It appears PuTTY is the problem as it is not sending the escape codes that are necessary for this to work. I'm giving up for now and using Cygwin+mintty.

2
  • Wow, mintty is great - have been looking for a decent terminal emulator for Windows for some time now. Thanks for that.
    – blueyed
    Jan 13, 2011 at 17:37
  • However, I am getting ^[[1;5A for Ctrl-Up, which appears to be more correct, according to code.google.com/p/mintty/wiki/Keycodes#Cursor_keys
    – blueyed
    Jan 13, 2011 at 18:25

9 Answers 9

15

Edit:

Here's what worked for me:

  • My ~/.bashrc sets TERM=xterm (mine happens to be TERM=xterm-256colors) overriding screen's TERM=screen
  • I set PuTTY > Terminal > Features > Disable Application cursor keys mode to checked
  • I enter tput smkx at the Bash prompt
  • Now Ctrl-Arrow keys jump word-by-word

  • For informational purposes, if I do:
    tput smkx | hexdump -c
    I get
    0000000 033 [ ? 1 h 033 =
    and
    tput rmkx | hexdump -c
    gives me
    0000000 033 [ ? 1 l 033 >

I think there's a way to rework this into something a little better, but it's what I've got so far and it seems to work.

Original answer:

From info screen:

Each window in a `screen' session emulates a VT100 terminal, with some extra functions added. The VT100 emulator is hard-coded, no other terminal types can be emulated.

And VT100s don't have Ctrl-Arrow keycodes.

9
  • set -o vi (if you're that type of person :)
    – MikeyB
    Apr 7, 2010 at 21:58
  • 1
    @MikeyB: How does that help (it doesn't, by the way)? Apr 7, 2010 at 22:23
  • Thanks. Not the answer I was hoping for but now I can move on and live with this limitation or use another key binding.
    – kbosak
    Apr 8, 2010 at 11:44
  • 1
    @Dennis: Well, it gives you another way of handling the forward/backwards if you're the typoe of person who can wrap their head around it. set -o vi turns on vi command-line mode so you can hit ESC and use the vi keys to move around and edit your command line.
    – MikeyB
    Apr 8, 2010 at 14:47
  • 1
    @kbosak: I'm not happy with that answer, either. That's why I fixed it. :-) Apr 8, 2010 at 22:24
34

As mentioned in here, u can put in ~/.inputrc:

"\eOD": backward-word
"\eOC": forward-word

It worked for me.

8
  • 6
    This worked for me and was far easier than the accepted answer. Sep 1, 2013 at 14:53
  • This worked for me too, while the accepted answer didn't
    – Schiavini
    May 2, 2014 at 11:16
  • +1 Succinct answer that just works. Oct 19, 2014 at 3:27
  • 4
    Make sure to have $include /etc/inputrc at the top of your .inputrc file, otherwise readline won't read the system-wide bindings, and keys like Home and End will break.
    – James
    Jan 27, 2016 at 10:27
  • 1
    This should be the accepted answer!
    – Shailen
    Mar 22, 2018 at 16:25
4

I'm somewhat surprised that you got Putty to send ctrl-arrowkeys correctly even without using screen, as that has never worked for me. My solution was to switch to using alt-arrowkeys (meta-arrowkeys), which I believe have the same forward-word / backward-word binding in bash by default, though I had to issue a couple extra commands for zsh to recognize them:

bindkey "^[^[[C" forward-word      # Meta-RightArrow
bindkey "^[^[[D" backward-word     # Meta-LeftArrow
1
  • I may go this route as well. The left ctrl key is so much easier to hit than the meta or alt keys (at least on my keyboard). Oh well.
    – kbosak
    Apr 8, 2010 at 11:47
4

In case others find this old question like I did, a more recent solution:

Ctrl and arrow keys doesn't work in putty! What do I do?

putty inverts the sense of the cursor key mode on ctrl, which is a bit hard for tmux to detect properly. To get ctrl keys right, change the terminfo settings so kUP5 (Ctrl-Up etc) are the adjusted versions, and disable smkx/rmkx so tmux doesn't change the mode. For example with this line in .tmux.conf (assuming you have TERM set to xterm):

set -g terminal-overrides "xterm*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@"

Note that this will only work in tmux 1.2 and above.

http://stuff.mit.edu/afs/athena/project/bsd/src/tmux-1.3/FAQ

2

I'm using the Terminal > Keyboard > Function Keys > Xterm R6 That helps with htop (f4 search etc ) function keys working as expected.

What I've found out is that, instead of using CTRL + arrow, ALT + arrow jumps words instead.

1

I just experienced the opposite! For me, ctrl-left and ctrl-right did not have the desired effects in the command line, but they still worked in screen (and emacs). I think it has to do with the way your program inputs special characters. (I use MobaXterm now.)

My fix was simply adding these two lines to the .inputrc file:

";5D": backward-word
";5C": forward-word

(The ;5C and ;5D were what appeared in the command line when I used them before this fix.)

0

You should look into the documentation of terminfo, ncurses, screen and putty if you want to debug/configure properly. Also bash shortcuts can be configured with inputrc. Look into the man pages of bash and search for inputrc.

1
  • I don't think the prob is what's in inputrc. I've already tried several possible solutions that involved putting entries in inputrc with no success. Again, it appears that the incorrect code is being sent to screen for ctrl-arrow keys. I also don't want to spend much more time on this than I already have as it's really just a minor inconvenience.
    – kbosak
    Apr 7, 2010 at 16:57
0

Do you have the right character encoding in Putty ? If you connect to a server in UTF-8 and your putty is in standard charset, in vim, the arrow keys are written. If Putty is in UTF-8, you can move your cursor. Maybe it is the same in your case...

3
  • It's not a vi/vim problem at all and I only saw one option in PuTTY for character set, which didn't help.
    – kbosak
    Apr 7, 2010 at 17:00
  • Try the different options in Terminal->Keyboard->The Function keys and keypad. BTW, for me Ctrl-Arrow doesn't work in bash either, it does the same as without pressing Ctrl. Apr 7, 2010 at 17:09
  • Yeah, I've tried as many options and combination of options without any luck. FYI, there are some entries you can put in inputrc to help with the ctrl-arrow keys in bash, but they don't help when using screen.
    – kbosak
    Apr 7, 2010 at 17:34
0

In your putty configuration under Terminal -> Features, there's a few "disable" checkboxes. You may want to try disabling application cursor keys or application keypad mode -- sometimes putty tries to be "too smart" and sends control characters that aren't compatible with the server.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .