Prevent command from stopping when pressing CTRL+C
So, I was playing some Zork 1 and I went to copy some text to show a friend of mine, but I accidentally forgot to hold the shift key when pressing CTRL+C. This stopped the command and I lost my Zork data...
Is there some sort of "helpful" tip out their that I can use to prevent stopping the command when press the keys CTRL+C?
Note that Zork is run inside of the terminal. It's a terminal-based game in which you give commands to perform different actions.
Solution 1:
CTRL + C sends an interrupt signal (SIGINT
, which is signal number 2) to the job in the foreground. You can disable this by "trapping" the signal using the trap '' 2
command before starting Zork.
Re-enable CTRL + C (untrap SIGINT
) with trap 2
.
Tip: you could add something like this to your ~/.bashrc
:
alias zork="(trap '' 2 && cd ~/path/to/zork/ && frotz ZORK1.DAT)"
This way you will never forget to disable and re-enable the signal and it's easier to start the game by just typing zork
in the terminal.
Note: you can use the signal name instead of the number if it's more convenient for you, i.e. trap '' sigint
or trap '' int
. I'm just used to using the numbers, e.g. in kill -9
and such.
Solution 2:
The key combination Ctrl+C sends the character ^C
(byte value 3). This causes the terminal to send the SIGINT signal to the program running in the foreground¹. The conventional action for this signal is to interrupt the current command: programs designed to process successive commands go back to their toplevel prompt, while programs designed as a single batch command or a continuous interaction exit. Evidently the program you're using was designed according to the second model.
This signal-sending key is a feature of the general terminal interface in the kernel, shared by all terminal emulators and real physical terminals. You can configure which key sends this signal, as well as other keys (most notably CtrlZ sending SIGSTOP to suspend the foreground program) with the stty
command. To switch the key for SIGINT to Ctrl+_ (in the current terminal):
stty intr '^_'
To disable it altogether:
stty intr ''
To reset all settings to the default:
stty sane
The key cannot be an arbitrary key combination, it has to be a single byte value. The stty
setting can be overridden by programs — some programs (especially full-screen text mode programs) do their own keyboard shortcut processing.
¹ More precisely, to all the processes in the foreground process group for which the terminal is the controlling terminal.