Some years ago, I wrote about how to quickly toggle a process between foreground and background in Bash and Zsh. It has actually been a quite popular post on this site.
Since then, I have moved on and mostly use Fish shell, so I thought I would write an update on how to do the same ctrl-z trick here.
First of all, you will have to use Fish shell version 3.2 or newer (most modern distributions have 3.7 or similar). Before that version, it was simply not possible to bind the ctrl-z key combo.
Lo and behold, the binding in all of its simplicity:
bind \cz 'fg 2>/dev/null; commandline -f repaint'
The items are:
bind
– the actual command\cz
– short for ctrl-zfg
– move background process to foreground2>/dev/null;
– don’t print irrelevant info about getting process to foregroundcommandline -f repaint
– repaint the commandline to get rid of weird artifacts that can occur
That’s really it, unless you use fish shell in vim mode (which I do of course). In that case your command would look like
bind -M insert \cz 'fg 2>/dev/null; commandline -f repaint'
This tells fish shell to only catch this in insert mode.
The line can be added to your default file for key bindings (usually ~/.config/fish/functions/fish_user_key_binding.fish).
Enjoy!