Vim was designed for a terminal when it first got released. Even to this date, Vim’s primary interface is still a terminal, though there are some versions of it like Gvim that give rise to a graphical interface. Nevertheless, there were some good ol’ folks who had a problem with the original terminal Vim.
Most programs that involve some sort of manual data-writing from a user, have a common keybind across all of them. The famous Ctrl + S. It is no less than a celebrity if you ask me, it just does not get called in the grammy awards! Most people are habituated with the use of Ctrl + S to save their files, and so is the case for Vim users just starting out. Many people have reported issues that when they press Ctrl + S inside Vim, their interface gets frozen and they’re no longer able to give any inputs.
However, it all starts getting weirder from here. because not everyone has the same issue!
History of Ctrl + S in Vim
There’s nothing particularly historical about it, except for a bit of weirdness going in. What does this all mean, ya ask? In Vim version 7.4 and below, the freezing of Vim when pressing Ctrl + S was absolutely a problem. But not everyone had to face it, because only certain types of terminal shells take Ctrl + S as a flow control input, which freezes the terminal in the first place. As older versions of Vim relied more on the terminal I/O to process all its commands, any problem that had to occur in the terminal would appear in Vim as well.
This goes as weirder as it could. In Vim version 8.0 and above, the freezing no longer persists, and if you still happen to press Ctrl + S somehow, you may only see “^S” stuff like this being printed on the screen instead of freezing your interface. When not inside of Vim and only inside the terminal shell, the freezing may still occur if you press Ctrl + S, because it is still not fixed. Actually, it isn’t meant to be fixed in the first place as this isn’t a bug or anything, but rather an intended feature for the terminal. But wait, the terminal still has this problem, why does Vim not freeze then?
While the correct answer is not yet obvious to us, but as far as we have come to tell, it could be something related to the release of the asynchronous I/O support in Vim version 8.0.
What is Software Flow Control?
In the simplest of English terms, software flow control is basically a method of controlling how much information will be processed in any given task and time. If you push more data to a computer than it supposedly can handle, the data may overflow and you may not receive all of the data you were supposed to, sometimes leading to overall data loss. In our case, the terminal has a function to freeze a large flow of information, to help you better interpret what’s going on. To help you understand it’s use, here’s a simple example:
When I use the du -h command, the data is flowing so fast no human eye could feasibly be able to read. Therefore, when I press Ctrl + S, the data flow stops at that very point. Notice that processing of the data also stops, not only does the output. If the data processing had not stopped, then when I had freezed the output, and unfreezed it again after a few seconds, all the data would’ve been processed by then and I’d have reached the very end of the process’s output instead of where I stopped it last time.
How to unfreeze Vim
It should be as easy as pie, as it isn’t a bug anyway. If you’ve properly noticed the gif in the last section, you should be able to see that I pressed Ctrl + Q and the processing unfreezed. It’s the same inside Vim as well. If your screen gets stuck or it feels like it freezed, simply press Ctrl + Q to get into control once again.
How to remap Ctrl + S in Vim
Before you can map Ctrl + S to some other command so that you don’t unintentionally freeze Vim again, you first need to bypass the keybinding from your terminal shell. What this means is that you’ll first need to disable the command which freezes the flow control in your terminal. to do this, append the following lines in your .bashrc file located in your home directory, if you happen to use bash (you’ll need to figure out which shell you’re using and which command to use to make it work):
bind -r '\C-s' stty -ixon
Henceforth, you can now map Ctrl + S to anything you want in Vim. A common use would be to save the current file while you’re in Insert Mode, for which you’d use:
inoremap <C-s> <C-o>:w<CR>
A better fix (switch to Vim 8+)
The best way to fix this problem is to obviously switch from the old version of Vim you’re using to version 8.0 and above. The problem no longer persists in the newer version, according to our testing. However, if you still face the issue yourself, try the method as shown in the previous section. It should work as intended.
Therefore, you should have no troubles now with freezing in Vim, as long as you’ve got everything I’ve said so far. Remember the fact that it is not a bug, but rather a feature. You may actually find it handy sometime along, now that you know about it. Or maybe even so, you could intentionally want to freeze Vim, let’s say, to stop your lil’ bro or naughty nephew from thumping your keyboard and messing up all your code!