How to use iTerm2 keyboard shortcuts with linux screen

Screen is a great tool if you spend a lot of time in the terminal and need to keep a bunch of connections open (here's a good screen tutorial). However, it can be quite hard to master and some people prefer tmux for some more advanced and user-friendly features (go here for a tmux primer). For my own purposes screen is enough so I'll stick with it. I use it in combination with iTerm2 which is a great terminal app replacement for Mac OS X and since I like to make my life easier I decided to some of the keyboard shortcuts.

One of the main purposes of screen is to allow you to create "virtual" windows that can remain connected even if you close your terminal and shutdown your computer (they live in the remote server). If you know screen you must also know how annoying it sometimes is to switch between windows using the escape sequence Ctrl+A+n (next) or +p (previous), especially if you want to do it fast. The alternative Control + a + " to see a list of all open windows is not always very handy either.

iTerm2 allows you to customize keyboard shortcuts to do whatever you want, so let's dig into it. I wanted to have a very easy keyboard sequence that would allow me to jump between windows quickly so I chose Command + Option + →/←.

The default screen escape sequence for entering commands is Control + a and the hex code for that is 1 which means that in iterm2 we need to send 0x01 (in this page you'll find a list of all the ascii control codes).

Then we need to send either a "n" for next or "p" for previous. That's easy, we just need to find the hex code for each one of those letters. In this ascii table we can check all of those codes. So, n = 06E and p = 070 (these are lowercase letters, that's important). So the final codes are:

  • Next: 0x01 0x6E
  • Previous: 0x01 0x70

Here's what you'll be able to do:

iTerm2 with screen

Note: I'm using a custom hard status line in screen's config file so that I can see all open windows in a fixed horizontal bar at the bottom.

Here's how to do it:

  1. Open iTerm's preferences (Command + ,)
  2. Go to Profiles and select the Keys tab (see screenshot below)
  3. Click the + to add a new shortcut
  4. In the Keyboard Shortcut box type the key sequence you want (I'm using Command + Option + → or ←)
  5. In the Action box select Send Hex Code
  6. In the box below type the respective code for next (0x01 0x6E) or previous (0x01 0x70)

Here's an example to make it more visual:

iTerm2 keyboard customization

You can use the same technique to customize other keyboard sequences, even if you don't use screen.

I hope this is useful to you, let me know by dropping a comment below.

1 comment to How to use iTerm2 keyboard shortcuts with linux screen

  • Amin

    Hello André!

    You just saved at least an hour of my life every day :) Now I don’t need to press three keys every time I want to go to next screen.

    Thanks man, great share!

    Cheers,
    Amin

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>