How to unfreeze after accidentally pressing Ctrl-S in a terminal?

517,717

Solution 1

Press Ctrl-Q to unfreeze.

This stop/start scheme is software flow control, which is implemented by the OS's terminal device driver rather than the shell or terminal emulator. It can be configured with the stty command.

To disable it altogether, put stty -ixon in a shell startup script such as ~/.bashrc or ~/.zshrc. To instead just allow any key to get things flowing again, use stty ixany.

Solution 2

Ctrl-Q is indeed the answer. I thought I'd toss in a little history of this that is too long to fit in the margins of ak2's correct answer.

Back in the dark ages, a terminal was a large piece of equipment that connected to a remote device (originally another terminal because teletypes were so much easier to learn to operate than a telegraph key) over a long wire or via phone lines with modems. By the time Unix was developing, the ASCII code was already well established (although the competing EBCDIC code from IBM was still a force to be reckoned with).

The earliest terminals kept a printed record of every character received. As long as the characters arrived no faster than the print head could type them, at least. But as soon as CRT based terminals were possible, the problem arose that only about 25 lines fit on the CRT, and 25 lines of 80 characters represented enough RAM that no one thought seriously about providing more RAM for characters that had scrolled off the top of the screen.

So some convention was needed to signal that the sending end should pause to let the reader catch up.

The 7-bit ASCII code has 33 code points devoted to control characters (0 to 31 and 127). Some of those had really well established purposes, such as NUL (blank paper tape leader for threading, gaps, and splices), DEL ("crossed out" characters on paper tape indicated by punching all seven holes), BEL (ding!), CR, LF, and TAB. But four were defined explicitly for controlling the terminal device itself (DC1 to DC4 aka Ctrl+Q, Ctrl+R, Ctrl+S and Ctrl+T).

My best guess is that some engineer thought that (as mnemonics go), "S" for "Stop" and "Q" for "Continue" weren't too bad, and assigned DC3 to mean "please stop sending" and DC1 to mean "ok, continue sending now".

Even that convention was already well established by the time Unix was leaving nest at Bell Labs to go out into the world.

The convention is known as software flow control, and is extremely common in real serial devices. It is not easy to implement correctly, as it prevents the use of either of those characters for any other purpose in the communications channel, and the Stop signal has to be handled ahead of any pending received characters to avoid sending more than the receiving end can handle.

If practical, using additional signals out of band from the serial data stream for flow control is vastly preferred. On directly wired connections that can afford the additional signal wires, you will find hardware handshake in use, which frees up those characters for other uses.

Of course, today's terminal window is not using an actual physical serial port, has scroll bars, and doesn't really need software handshaking at all. But the convention persists.

I recall the claim that Richard Stallman received complaints about his mapping Ctrl+S to incremental-search in the first releases of emacs, and that he was rather unsympathetic to any user that had to depend on a 7-bit, software flow controlled connection.

Solution 3

Control Keys: perfrom special functions on Shell

  • Ctrl-S : Pause Display
  • Ctrl-Q : Restart Display
  • Ctrl-C : Cancel Operation
  • Ctrl-U : Cancel Line
  • Ctrl-D : Signal End of File
Share:
517,717

Related videos on Youtube

imz -- Ivan Zakharyaschev
Author by

imz -- Ivan Zakharyaschev

My name is Ivan Zakharyaschev, Иван Захарьящев, imz.

Updated on September 18, 2022

Comments

  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev over 1 year

    It's a situation that has happened quite often to me: after I press (with a different intention) Ctrl-S in a terminal, the interaction (input or output) with it is frozen. It's probably a kind of "scroll lock" or whatever.

    How do I unfreeze the terminal after this?

    (This time, I have been working with apt-shell inside a bash inside urxvt--not sure which of them is responsible for the special handling of Ctrl-S: I was searching the history of commands backwards with C-r, as usual for readline, but then I wanted to go "back" forwards through the history with the usual--at least in Emacs--C-s (1, 2, 3), but that caused the terminal to freeze. Well, scrolling/paging to view past things still works in the terminal, but no interaction with the processes run there.)

    • Admin
      Admin almost 8 years
      I was working in vim and I pressed Ctrl-S to save my edits. Uh-oh XD
    • Admin
      Admin over 5 years
      Why does that exist in first place?
    • Admin
      Admin over 3 years
      This sometimes happens to me: accidentally freezing Vim, in a xfce4-terminal (it's actually the terminal that is frozen). Various xon-xoff solutions (Ctrl-Q ...) had no effect. Although no keyboard shortcut was evident, I noticed that from the terminal menu that the terminal had become set to "Read-Only." Unchecking that restored terminal responsiveness.
    • Admin
      Admin over 3 years
      Just in case the terminal froze after ssh lost connection: tilde - period, see unix.stackexchange.com/a/196724/296434 . Cross linking because that post is harder to find when just googling on terminal freeze
    • Admin
      Admin almost 3 years
      Thank you @VictoriaStuart - this has been driving me insane. Now I just need to figure out the keystroke that causes the RO mode...
    • Admin
      Admin almost 3 years
      Enter ↵, ~, . is what I needed: stackoverflow.com/a/43189095/470749
    • Admin
      Admin almost 3 years
      @Ryan that's to break SSH sesion.
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev about 13 years
    Thank you! BTW, there they suggested Ctrl-C; does it work, too? (And at another place, they suggested Ctrl-Q, just as you.)
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev about 13 years
    Yes, both Ctrl-C and Ctrl-Q work (at least, for me).
  • ak2
    ak2 about 13 years
    Ctrl-C does work, but it also sends an interrupt signal, which one generally wouldn't want. (Btw, the keys being used for these things are all configurable through stty.)
  • Peter.O
    Peter.O about 13 years
    Thanke.. very interesting... I've been wondering: Why on earth would anyone choose to Continue a process with "Q" which is commonly used (now) for Quit... The QRST grouping explains it .....
  • RandomNickName42
    RandomNickName42 about 13 years
    This one took me a long time to figure out too, but after I found Q, I was like "Man their so close on the keyboard how did I not hit it before!!!"
  • ak2
    ak2 about 13 years
    Thanks very much for that bit of history. I recently disabled flow control by default in a terminal emulator I maintain, but had to reinstate it rather quickly after vocal protests from Unix traditionalists who do still use it. I set the ixany bit instead, so at least people who press ^S without knowing about ^Q don't get stuck.
  • penguin359
    penguin359 about 13 years
    @RBerteig I ran into something similar learning about backspace and delete. Backspace is officially ^H and delete is ^? Some people like the Emacs developers (Stallman again?) wanted ^H available for general purpose like shortcuts like Help. The escape sequence ^[[3~ or something like that was created to replace ^? and backspace now became the old delete character ^?. In fact, I've seen ncurses specifically patched on Linux distros to remap thoses keys in terminfo whereas ncurses on FreeBSD is unpatched causing some of the annoying confusion causing backspace to not work.
  • Chris
    Chris about 13 years
    @penguin359 - Actually, I believe you're talking about the VT control codes which began with <esc>[[ (sometimes called ANSI codes which were often used for color and other purposes). VT52 and VT100 are among the most common versions of that.
  • RBerteig
    RBerteig about 13 years
    I used to wonder why DEL had the code 127 rather than being grouped with the other control characters, until I first played with some paper tape and an ASR33. Once I realized that it had the effect of punching all the holes, which meant it could be overstruck on any previously punched character to delete it, it made sense.
  • penguin359
    penguin359 about 13 years
    @Mystere Your right, it's not an ANSI Escape sequence, but an ANSI Control Sequence. Escape (^[) followed by [ is an Escape sequence that represents the CSI ANSI Control charater. CSI starts an ANSI Control sequence. The DEC Terminals as far as I can tell never used ^[[3~ for Delete, I think that was added in XTerm.
  • ak2
    ak2 about 13 years
    @penguin359 The VT220 sends ^[[3~ for the Remove key that's part of the six-key editing keypad. Seems to make plenty of sense to equate that with the 'Delete' key on PC keyboards. See also quadibloc.com/comp/images/vt220.gif for a picture.
  • penguin359
    penguin359 about 13 years
    @ak2 Looks like you might be right. Odd, I can't find mention of the remove and similar function keys on the VT-200 Programmer Reference Manual from vt100.net. Must just be incomplete. I'd love to get my hands on a real DEC Terminal someday.
  • ak2
    ak2 about 13 years
    @penguin359 The editing keypad appears on the page you linked to actually, with a pointer to a table listing its keys.
  • penguin359
    penguin359 about 13 years
    @ak2 you're right, I guess I was taking too quick a glance. I didn't understand what <X] (Delete) which sends DEL meant. Looking at the keyboard, I realize they were trying to be clever and post the symbol for the Backspace (Delete) key in ASCII and I assumed any actual [Forward] Delete key would be called Delete and not Remove. It looks like the VT-220 only sends the ASCII Backspace character from F12 when operated in VT-100 compatibility mode. I've spent much more time studying the VT-102 and naturally assume the VT-220 had the same Backspace and Delete keys which send ^H and ^? respectively
  • matchew
    matchew about 11 years
    Wow, I am so happy to stumble upon this. tabs would freeze a lot for me in urxvt. I guess because I hit ctrl+s by mistake. I use ctrl+a,ctrl+e,and ctrl+o a lot. Thanks!
  • sashoalm
    sashoalm over 10 years
    I remember trying out this combination on my Apple ][ clone, and it worked there too - Ctrl-S and then Ctrl-Q to resume.
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev almost 10 years
    Interestingly, when an Emacs is running in the terminal, C-s doesn't freeze it. So, this means that Emacs changes this driver setting and then restores it on exit (or lets it be restored somehow automatically). So sometimes this setting seems to be under an intentional control of a program running in the terminal (which could be the shell or Emacs).
  • Kraang Prime
    Kraang Prime about 9 years
    Interesting article on History, however since then, VIM has undergone many re-writes, none of which take into account modern computing shortcuts. This would be fantastic if working with text in the early 80's however I do believe we have moved on, and it's time for VIM (including windows compile) to move forward as well. Please send the memo as I believe the core dev's are stuck in a time-loop back in the 80's somewhere.
  • Upe
    Upe almost 9 years
    I got an IBM 3161 ASCII terminal a while back - this answer helped me make sense of it... It's working now! Thanks. Any idea why terminal emulators even bother with the DC1 and DC3 instructions though?
  • gerlos
    gerlos over 8 years
    @SanuelJackson Ctrl-S "save shortcut" is applicable in nearly all DESKTOP application EXCEPT vim. And Except Emacs. And Nano. And every other "application" you can run on a terminal, exactly because it's already used by the terminal for flow control. It's the same reason you won't find a Linux Desktop application using the Ctrl-Alt-FN shortcuts: because they're already used by the system.
  • Drew
    Drew about 8 years
    @SanuelJackson It's been some time since I looked closely, but the default vimrc that ships with windows has mapped <C-s> to ":update" for many years. I'm also fairly certain I've seen it on some distros for gvim in the past as well. If you are using vim in a terminal, though, this history is still very much relevant. Modern terminal emulators still implement the flow control keys, so even if vim mapped them it would never see them unless each user went out of their way to disable flow control as described in the accepted answer.
  • Al.G.
    Al.G. over 7 years
    @SamuelJackson :w is much powerer than a C-s shortcut. You can use it to save to a different command, to pipe the buffer's contents to a program or to write to a different file - all of which can't happen with just a shortcut.
  • xor
    xor over 7 years
    @SamuelJackson there is nothing stopping you from binding ctrl-s to :w command in your .vimrc, see here for example: stackoverflow.com/a/11298171/2380702
  • rien333
    rien333 over 7 years
    On ubuntu 16.04, running stty -ixon disable the freeze effect of ctrl-s, and replaces it with something like "i-search", another feature I don't need at all. As I use emacs shortcuts a lot at my prompt and also switch keyboards up often, I end up hitting ctrl-s by mistake many times a day. Is there any way to let it do nothing at all? (using gnome-terminal btw)
  • CMCDragonkai
    CMCDragonkai over 7 years
    This means that ^Q and ^S doesn't correspond to SIGCONT and SIGSTOP right?
  • RBerteig
    RBerteig over 7 years
    @CMCDragonkai Nope. Nothing at all to do with SIGCONT and SIGSTOP. SIGTSTP is generated by the terminal driver when ^Z is typed, and can be ignored unlike SIGSTOP which cannot. No keystroke sends SIGCONT, it is the signal sent by whatever command in your shell you use to continue a stopped job. The ^S and ^Q keys act directly on the terminal driver stack, and don't generally send a signal.
  • supercat
    supercat almost 7 years
    I don't know when video display terminals first became practical, but I would expect that DC3/DC1 flow control predates them. An ASR-33 may never need to autonomously request that a remote paper tape reader stop and restart, but an operator who needs to swap out a roll of paper for the printer or punch might recognize such a need. Someone who was using a video terminal to inspect a paper tape might need to start or stop the reader, but that could be done with a button on the reader. I would not expect people to frequently start and stop remote tapes to give themselves time to read stuff.
  • supercat
    supercat almost 7 years
    If someone needs to forward a news story to many other people, any time the reader spends waiting for someone is time it can't be sending to anyone else. Better to have the recipient capture the story to paper tape or print paper, and then peruse it at leisure.
  • Ryan B
    Ryan B almost 7 years
    For MSYS2 use Ctrl-Z. Neither Ctrl-C or Ctrl-Q work
  • Nash Bridges
    Nash Bridges over 6 years
    Be aware that reset command will also enable flow control, so you'll need to issue stty -ixon again.
  • Ciro Santilli Путлер Капут 六四事
    Ciro Santilli Путлер Капут 六四事 over 6 years
    If you put this in your .profile, it causes a popup error to appear on login hat reads: "inappropriate ioctl for device" in Ubuntu 17.10: askubuntu.com/questions/918169/…
  • Stan Strum
    Stan Strum over 6 years
    @KraangPrime I remember starting off using Nano/pico — even worse: CTRL+X y
  • Nick T
    Nick T about 6 years
    What sort of startup script, for the shell, e.g. ~/.bashrc?
  • jirislav
    jirislav about 6 years
    Note that when you are searching command with CTRL-R, the side effect of stty -ixon is that you can also do revert search using the mentioned CTRL-S, which is very handy, if you keep pushing CTRL-R too fast :)
  • Thamme Gowda
    Thamme Gowda over 5 years
    I added stty -ixon to ~/.bashrc, then restarted emacs daemon, tmux sessions (a true logout login cycle), then it worked
  • Kjetil S.
    Kjetil S. almost 5 years
    Using Emacs through GNU Screen might also disable ctrl-s and ctrl-x-s for search and save-file, which of course is very annoying when it happens. I can keep Emacs sessions for weeks and months otherwise. To make Emacs see ctrl-s again I found ctrl-a f which toogles the current GNU-Screen "window" between three states: -flow(auto), -flow and +flow where +flow shouldn't be used. (Ctrl-a is the default GNU Screen key, could be different on other setups)
  • John
    John over 4 years
    @gerlos The reason Ctrl S works fine in every desktop application and causes such a mess on Linux (possibly other Unixes) is because of people like you who defend algorithms used 40 years ago when modern computers were invented and output was on a paper instead of a screen. Ctrl S/Q should have been removed 30 years ago and it's a shame such code still dwells in the heart of every linux server.
  • gerlos
    gerlos over 4 years
    @John sorry, I wasn't "defending" anything, just pointing out that exist different ecosystems (terminals, desktops, etc) with different interaction patterns. These ecosystems work they way they work for both historical and practical reasons that are still valid today. I understand that it can be annoying if you're not used to it. But thinking that there should be just one way of doing anything regardless of the context is plainly wrong.
  • John
    John over 4 years
    @gerlos Which partical reason would you provide for this 'feature' to be part of the linux kernel and exposed to tens of millions of users every day ? historic reasons are not a reason to keep a horrible 'feature' alive, they are reason to remove.
  • gerlos
    gerlos over 4 years
    @John You use Ctrl-s (and Ctrl-Q) to pause (and resume) long output scrolling on terminals so you can read what's going on. That's it. It's true, you can use pagers like less or more, but only if you expect a long output coming in, if terminals support them, and if the system can run them (it might not be possible in some emergency situations). Moreover, some (hw and sw) terminals doesn't even support scrolling, so you can't scroll bak to read (serial consoles are often used on embedded devices). So please stop complaining about something you never used and don't understand.