Fixing Ctrl+Arrow Key Behavior in PuTTY with GNU Screen: Terminal Control Sequence Analysis


2 views

When working with PuTTY to connect to Linux servers running GNU Screen, many developers encounter broken Ctrl+Arrow key functionality. The terminal emulator sends different escape sequences than what Screen expects:

Non-Screen Mode:
Ctrl+Left:  ^[OD
Ctrl+Right: ^[OC

Screen Mode:
Ctrl+Left:  ^[[D
Ctrl+Right: ^[[C

PuTTY's default configuration doesn't properly advertise its terminal capabilities to Screen. We need to verify the terminal type being used:

$ echo $TERM
xterm

For proper Screen handling, we should use either screen or screen-256color as the terminal type.

Option 1: PuTTY Configuration

Navigate to PuTTY Configuration > Connection > Data and set the terminal-type string to:

screen-256color

Option 2: Server-side .screenrc

Add these mappings to your ~/.screenrc file:

# Fix Ctrl+arrow keys
bindkey "^[[1;5D" backward-word
bindkey "^[[1;5C" forward-word

For users who can't modify server configurations, these bash alternatives work within Screen:

# Use Alt instead of Ctrl
bind '"\e[D": backward-word'
bind '"\e[C": forward-word'

# Or use Esc sequences
bind '"\e[1;5D": backward-word'
bind '"\e[1;5C": forward-word'

To debug terminal sequences, use these commands:

# Show what keys actually send
$ showkey -a

# Alternative method
$ od -c

Press Ctrl+Arrow keys and observe the output sequences.

The most reliable approach combines both client and server configuration:

  1. Set PuTTY terminal type to screen-256color
  2. Add proper bindkey entries to ~/.screenrc
  3. Configure bash to handle both cases:
# In ~/.bashrc
if [[ -n "$STY" ]]; then
    bind '"\e[1;5D": backward-word'
    bind '"\e[1;5C": forward-word'
else
    bind '"\eOD": backward-word'
    bind '"\eOC": forward-word'
fi

When working with PuTTY to connect to Linux servers running GNU Screen, many developers encounter inconsistent behavior with Ctrl+arrow key combinations. While these shortcuts work normally in bash (moving word-by-word), they fail inside screen sessions, producing unexpected escape sequences instead.

The key difference appears in the escape sequences:

Outside screen:
Ctrl+Left: ^[OC
Ctrl+Right: ^[OD

Inside screen:
Ctrl+Left: ^[[D (regular left arrow)
Ctrl+Right: ^[[C (regular right arrow)

This indicates screen isn't properly interpreting PuTTY's modified key sequences.

For proper Ctrl+arrow functionality, we need:

  • Correct terminfo/termcap settings
  • Proper $TERM variable
  • Matching keybindings in both PuTTY and screen

Add these bindings to your ~/.screenrc:

bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[OC" forward-word
bindkey "^[OD" backward-word

This covers both the inside-screen and outside-screen key sequences.

In PuTTY's settings (Connection → Data):

Terminal-type string: xterm-256color

Then ensure your remote system has this terminfo:

$ infocmp xterm-256color > /tmp/xterm-256color.terminfo
$ tic /tmp/xterm-256color.terminfo

To check what codes your keys actually send:

$ cat -v

Then press Ctrl+arrow combinations and observe the output.

If the issue persists, consider these alternatives:

  • Cygwin with mintty (as mentioned in original post)
  • Windows Terminal with WSL
  • MobaXterm for Windows

These often handle terminal emulation more consistently than PuTTY.

For complete control, create a custom terminfo entry:

$ infocmp > ~/.terminfo/myputty
# Edit the kLFT5 and kRIT5 capabilities
$ tic ~/.terminfo/myputty

Then set TERM=myputty in your environment.