Blog | Admin | Archives

Mac OS X, Terminal, Screen, and Ctrl-Arrow Keys

I have had a number of frustrations since I started at Facebook in adapting to the quirks of Mac OS X. I have been in Windows-land for so long that I consider its quirks normal, so figuring out the quirks of OS X has at times been frustrating. Nevertheless, I think I am making good progress.

I am using GNU screen while logged in to linux machines extensively during work, so I invested a little time in setting up a good screenrc. Afterwards, I noticed that I was unable to get the screens to switch using ctrl-left and ctrl-right the same way I would be able to from a Windows machine.

After being frustrated with trying to get Terminal to send the right control characters, and getting screen to recognize them using bindkey, I took a step back and found a solution that actually works. Despite its failings at behaving like PuTTY in every respect, Terminal does offer a lot of customizations, including the ability to change what control characters get sent when you press control-left and control-right, for example. So instead of trying to divine the right bindkey option for Mac OS X (which no one seems to have succeeded at as far as I can tell), I took another approach:

I told Terminal to send “ctrl-a p” (\001ap) for ctrl-left, and “ctrl-a n” (\001n) for ctrl-right. This makes hitting those keys the equivalent of going to the previous and next screens manually, but without the manual part. Of course, if you use a different control character for your screen, you will want to use that instead of ctrl-a as I did, but this finally got screen to behave the way I wanted, which makes me quite happy.

For reference, my screenrc looks like this right now:

vbell off
autodetach on
startup_message off
defscrollback 10000

hardstatus alwayslastline
hardstatus string '%{= kG}%-Lw%{= kW}%50> %n*%f %t%{= kG}%+Lw%< %{= kG}'

8 Responses to “Mac OS X, Terminal, Screen, and Ctrl-Arrow Keys”

  1. Vince Says:

    Oh Ryan. You and your use of screen….

  2. Spencer Says:

    Use iTerm ( It is >>Terminal.

    Also, a good idea is to update the X11 system, which has to be done manually at While apple develops and maintains the project, for some reason X11 isn’t updated with the rest of the apple system.

  3. Spencer Says:

    Btw, if you’re on snow leopard you have the latest version of x11 already. Nevermind~

  4. RJ Ryan Says:

    This is what I had to do to my screenrc to get it working without your trick:

    bindkey “33[5D” prev
    bindkey “33[5C” next
    bindkey “33[2D” prev
    bindkey “33[2C” next

  5. RJ Ryan Says:

    (Doh! my previous comment had its backslash-0’s eaten — trying again with double-backslashes)

    This is what I had to do to my screenrc to get it working without your trick:

    bindkey “\33[5D” prev
    bindkey “\33[5C” next
    bindkey “\33[2D” prev
    bindkey “\33[2C” next

  6. RJ Ryan Says:

    Well, ok I’m having escaping trouble here:

    Prefix the 33 in all cases above with backslash zero.

  7. Ryan McElroy Says:

    Oh, silly WordPress can’t handle \0 in comments? Sad.

  8. Rensa Says:

    Thanks so much—I’m setting up a .screenrc for my work servers, where I don’t think I can use tmux, and this is a blessing.

Leave a Reply