Acknowledge Failure
Context:errorbells
The best part about having a blog is being able to express my thoughts about a topic that no one would ever sit down and let me go over with them in person. With that in mind, let’s talk about how error bells work in Vim.
By default in Neovim (not our beloved Vim though) the error bell is off. If it had been up to me I would have left it on instead, because I like the idea of blowing an airhorn at someone whenever they make a mistake. If you are using Neovim, you will need to turn it back on by unmuting the bell with :set errorbells and then enable them with :set belloff= which adds error messages for all error types.
Hint: Because I am in the tips business, I should probably share with you that to see the current value of an option you can run
:set foobar?
Okay, now go to normal mode and spam that escape key and listen to the glorious ringing. Do you hear it? No? Well, it is possible you are experiencing the same technical difficulties that I was when I went through my commands to verify. My issues had many layers, like an onion. First, tmux can eat up your bells and escape sequences like it’s the Cookie Monster. Second, my laptop was muted. And third, for some reason i3-sensible-terminal was giving me GNOME Terminal, which unbeknownst to me has the terminal bell off by default. So I booted up Ghostty and tried loading in a custom .wav file for an error bell which immediately caused my terminal to crash.
sudo apt install kitty and now back to Vim.
If you don’t want the errorbell monkey on your back for every little blunder, consider reading through some of the flags you can pass in :h belloff like set belloff+=esc to not get the ding of shame for being in the wrong mode and hitting escape (you should really leave it on though, because if you do that you deserve to hear it).
Now that we have started our December off in the right spirit by dinging our bells, let’s have some fun with escape sequences. For the audibly impaired and easily annoyed we can instead swap out our ding for a flash, some colour changes, or even some cursor shenanigans if we are feeling wild. Run :set visualbell, trigger an error, and watch as your screen flashes a pretty colour. Unlike when I failed at customizing my error bell earlier, I at least know how to write a few escape sequences.
Hint: Check out
man 4 console_codesto look up the following codes and find some other goodies you can employ.
By running :set t_vb? we find out that the default escape sequence is ^[[?5h$<100/>^[[?5l. Using our handy dandy man pages we can look it up and see that ^[[?5h is a DEC Private Mode (DECSET/DECRST) sequence for reverse video mode and the ^[[?5l is its counterpart for resetting it back to its default. Buried in man 5 terminfo is the explanation of $<100/> which is a mandatory 100ms wait. So our default is flip video negative for 100ms. That’s all well and good, but what if we don’t want the default because we are a special snowflake and need to make sure others know?! Let’s prepend the default values with an OSC 2 Title Changer:
let &t_vb = "\e]2;YOU STINK!\x07\e[?5h$<100/>\e[?5l"
Note: We can use &t_vb with let so that it sets an option instead of a variable named t_vb while still taking advantage of the expression evaluation within the double quotes, as opposed to set which would just read it literally.
Note 2: If the title did not change for you, it may be because you are using Neovim, which doesn’t support term codes (
:h t_xxfor more info), or you may have tmux the Cookie Monster around swallowing the sequence before it reaches your terminal, or heck it may be your terminal itself, because support varies drastically from terminal to terminal for some less common sequences.
Escape sequences can do a whole heck of a lot of neat things (how do you think your terminal’s state is magically restored after quitting Vim!) and they probably deserve to be written about more than they are, so don’t be surprised to come back one day and find a deep dive on them.