5

The background for this question: I currently have to do a lot of my work in terminal over ssh, and I use screen quite a bit. Because I found the ctrl-a key binding for screen commands so annoying since I'm accustomed to using ctrl-a to go to the beginning of a line, I changed it to ctrl-z. The only problem with this is that when I'm in Matlab, think I am in Screen but am not, pressing ctrl-z will instantly kill my Matlab session, because ctrl-z is the key binding for suspending processes in *nix.

So the question is: can I remove the key binding for ctrl-z in my shell so that it does no longer suspend a process?

My shell is terminal.app on OSX.

2
  • 9
    Does Ctrl-Z really kill Matlab? The way suspension works is that you get back your shell prompt (at least on the command line). If you're done suspending, you can continue the suspended program with fg (place it in the foreground again).
    – Jens
    Commented Sep 18, 2012 at 12:52
  • 1
    Thanks, Jens! You are absolutely right. I thought it was killed since I didn't see Matlab in top, but fg brings it back. That solves my problem :)
    – Nagel
    Commented Sep 19, 2012 at 13:59

3 Answers 3

13

The ^Z binding is not configured in the shell, but in your TTY, the pseudo-device that lets you talk to the physical terminal or, as is typical nowadays, to a terminal emulator program. The TTY carries settings that tell the kernel how to react to keyboard input reported by the device or emulator.

Settings are changed using the stty command:

stty susp "^P"

stty accepts a setting name and its value; in this case the setting name is susp for key that suspends the process, and the value is a key combination, such as ^P. Type stty -a or refer to man stty for a list of available settings.

3

You can wrap your matlab in a script named matlab.sh like this:

#!/bin/sh
trap "" TSTP
/path/to/matlab-executable

That will ignore the TSTP signal usually sent by Ctrl-Z.

0
2

Another possibility is to choose a different escape character for screen.

I use the null character. I have the following in my $HOME/.screenrc:

escape ^@^@

The null character can (usually) be entered as Ctrl-Space, which is very easy to type. And of course if I need to send an actual null character to a program (for example, it's bound to set-mark-command in Emacs), I just type Ctrl-Space twice.

(This sometimes causes problems when I'm using a device or terminal emulator that can't send the null character, but that's rare.)

Another possibility is Ctrl-]:

escape ^]^]

which is also easy to type on a US keyboard. (I actually use that in a screen session that runs nested inside my primary screen session, but that's probably more than you want to know.) The only conflict I typically run into with that is that it's the default escape character for the telnet command.

2
  • Thanks for your reply. I use ctrl-space for something else, though, and ] is not so easily accessible on my Scandinavian keyboard.
    – Nagel
    Commented Sep 19, 2012 at 14:02
  • For Emacs users, if you want to disable suspending when inside Emacs, this may be of help: (define-key global-map (kbd "C-z") 'undo) and (define-key global-map (kbd "C-x C-z") 'undo). Commented May 19, 2021 at 15:10

You must log in to answer this question.

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