I spend a lot of time in vim (Neovim these days actually) editor and that means that my console is occupied by my editor.

As I do not always want to split my terminal screen (using tmux or the actual terminal software) I instead use ctrl-z to put vim in the background. This gets me back to the terminal, which is great.

When I need to get back into my vim editor, I can just write fg and press enter.

I have never really liked that I have to write a command to get vim back, so I decided to find a way to just use ctrl-z to toggle back and forth between fg and bg.

For zsh shell there is a script for this called fancy-ctrl-z.zsh, but that used zsh specific commands. As I am mostly on systems where I use bash shell, This would not work.

Instead, I came up with the following simple snippet that I have added to my ~/.bashrc:

# use ctrl-z to toggle in and out of bg
if [[ $- == *i* ]]; then 
  stty susp undef
  bind '"\C-z":" fg\015"'

What it does is simply that whenever ctrl-z is pressed, it will check if it is “i” is set in the shell options – meaning “are we in an interactive shell”. If so, it means that we are in bash and hence the fg process (in my case mostly vim) is suspended to background.

Now it just disables susp(end) and binds ctrl-z to the command fg<Enter>.

Thats it really!

It is amazing how such a simple thing can make a difference every day for me.