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}'
September 24th, 2009 at 08:13:23 am
Oh Ryan. You and your use of screen….
September 24th, 2009 at 07:15:37 pm
Use iTerm (http://iterm.sourceforge.net). It is >>Terminal.
Also, a good idea is to update the X11 system, which has to be done manually at http://xquartz.macosforge.org. While apple develops and maintains the project, for some reason X11 isn’t updated with the rest of the apple system.
September 24th, 2009 at 07:20:07 pm
Btw, if you’re on snow leopard you have the latest version of x11 already. Nevermind~
July 12th, 2011 at 12:00:27 pm
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
July 12th, 2011 at 12:02:01 pm
(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
July 12th, 2011 at 12:03:56 pm
Well, ok I’m having escaping trouble here:
Prefix the 33 in all cases above with backslash zero.
September 22nd, 2011 at 12:26:22 pm
Oh, silly WordPress can’t handle \0 in comments? Sad.
October 5th, 2015 at 09:35:13 pm
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.