Obvious and not-so-obvious Bash/Zsh terminal shortcuts
For a very long time, I’ve been using just tab
, ctrl-C
, ctrl-D
and ctrl-R
shortcuts. However, there are more useful gems out there. In this post, I’m going to show you Bash/Zsh terminal shortcuts that I find useful.
Tab
It’s the most important shortcut because it’s the autocompletion. It simply saves you a whole lot of typing. If there is just one possible option, then it will be automatically filled for you. If not, possible options will be presented below.
Ctrl-C and Ctrl-D – oversimplified explanation
Ctrl-C terminates the currently running command or a process. Ctrl-D is an equivalent of exit command. You can think about the Python REPL, where Ctrl-C ends the currently running command, but Ctrl-D exits the REPL itself.
Ctrl-C and Ctrl-D – a longer explanation
Ctrl-C
sends a SIGINT
signal, to a process, which is currently in the foreground.
Application usually interpret this as a signal to kill the process, but they don’t have to do so.
This is why Python interpreter doesn’t end the process, but just ends the command that is currently running.
Ctrl-D
sends an End-Of-File
marker. To explain this, imagine that we run a Python script.
The interpreter reads the program from a file and executes commands until the whole file is read.
When it reaches the end of the file, the interpreter terminates.
When we run the interpreter without any script, our keyboard input is treated as a “file”, from which the commands are read.
When we push Ctrl-D
, we inform the interpreter, that it reached the end of the file, all commands are read and thus it can terminate.
Of course it doesn’t have to be a Python interpreter – the same goes for other interpreted languages or shells like bash. Try to run bash inside a bash – you can “go back” with Ctrl-D shortcut.
Ctrl-Z and fg command
When you have some long-running command, you don’t necessarily have to terminate it if you have to do something else.
When you press Ctrl-Z
, the current process goes to the background. You no longer see its output, but the process is still running.
If we want to bring back that process to the foreground, we can use the fg
command to do that.
Ctrl-S and Ctrl-Q
We may also pause the output for a moment. This is useful, when there is a lot of fast-changing output,
like application logs we would like to inspect.
To pause the output we can use the shortcut Ctrl-S
(like stop).
To resume it back, use the Ctrl-Q
.