Nothing Undone

A weblog, by Peter Jaros.

Let's Talk About Jobs.

Get ready for hotness in a tiny package. This one’s a habenero.

Sometimes you’re in the terminal, editing a file. Oh, your editor doesn’t run in the terminal? Okay, maybe you’re looking at a man page. Or playing Nethack. Whatever. You’re doing something that’s taking up your terminal, but you want to use the shell to go check something out, like look up a man page or look at a file or run a test.

You could quit, do your thing, and come back. You could open a whole new terminal and get your new shell in the state the old one had. Or maybe the app you’re running will even let you shell out to run something. But I’m going to tell you about something that’s often even neater, something that’s been a part of your shell since perhaps before you knew what a shell was, but which most people I work with have no idea exists.

I’m going to tell you about job control.

What the what?

Job control. Sounds boring, doesn’t it? That unassuming name belies a deeper glory, a shining jewel of a feature lost in the morass that is man bash. Let’s explore it together, shall we?

You probably know that you can run a program in the background by using & at the end of a command line. Did you know that you can also suspend a program so it stops running altogether until you tell it to start again? Remember Red Light Green Light? Your shell does.

While a program is running, press Ctrl-Z.

Go ahead, try it. Launch vim and press Ctrl-Z. You’re back in the shell!

1
2
[1]  + 93572 suspended  vim
peeja:~/ $

See that [1]? That’s the job number of that vim process. The shell maintains a list of jobs it’s running, and each one gets a number. You can bring vim back by saying:

1
peeja:~/ $ fg %1

That’s “foreground job 1”. Or if you want to be rude you can even:

1
peeja:~/ $ kill -9 %1

Bet you can guess what that does.

You can also use bg to background a job, as if you had run it with & from the beginning. That’s useful when you realize a job is going to take longer than you’d thought.

The most recently foregrounded job is called the current job, and it’s the default for fg and bg, so you can just say:

1
peeja:~/ $ fg

And save some typing. But that’s still a handful. Can it get easier?

Yes, it can.

Presenting Ctrl-Zsh. If you’re using Zsh (and why wouldn’t you be), you can use this tiny, simple plugin to add a keybinding for fg. And that keybinding is: Ctrl-Z! That means that the same key that takes you out of an application takes you back in. It sounds tiny, but cognitively it feels completely different.

And no, it won’t work in Bash, because you can’t bind to Ctrl-Z in Bash. Oddly enough.

Installation

Antigen

Antigen is awesome, and if you aren’t using it yet, you should give it a try. To install Ctrl-Zsh just add

1
antigen-bundle Peeja/ctrl-zsh

to your .zshrc. Or, to try before you buy, just run that command to enable Ctrl-Zsh for the current shell session.

Oh My Zsh!

Add it to your plugins directory, like so:

1
2
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/Peeja/ctrl-zsh

Other

Just source ctrl-zsh.plugin.zsh from your .zshrc. Or, heck, copy it into your .zshrc. It’s only three lines! (But I’ve got devious plans for more.)

Comments