Obvious and not-so-obvious Bash/Zsh terminal shortcuts
For a very long time, I’ve been using just
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.
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
Up and Down Arrows or Ctrl-P and Ctrl-N
To run a previous command, we just need to press the up arrow. With up and down arrows we can navigate between already executed commands.
The same effect can be achieved using Ctrl-P (like in previous command) and Ctrl-N (like in next command). At first, arrow keys may be more intuitive, but as you become more proficient with keyboard, it may be more efficient to use the latter ones, because you can keep your hands on the letter keys.
Arrow keys are not convenient to use if the command was executed some time ago. To search in history for a command, we can use a Ctrl-R shortcut.
To display whole saved history, we can also use a
Ctrl-A i Ctrl-E
Ctrl-A is basically an equivalent of the Home button. It moves the caret to the beginning of the line.
Ctrl-E is like the End button. It moves the caret to the end of a line.
To clear the screen and go back on top of our terminal screen, we can use the
clear command, but we can also do it faster,
You know – cLear (not very intuitive, but it’s something …)
If we make a mistake in a last written word, we can use this shortcut to quickly delete the last word.
When we would like to remove a whole line, we can use
Ctrl-U shortcut (like in undo) to clear the whole line.
Ctrl-K shortcut deletes characters from the cursor to the end of the line.
Now, if we remove a command or its part by either of
we can paste it via
Ctrl-Y shortcut (like in yank).
It’s very useful when you write a very long command, but then you realize, that you have to run another one first.
Did I miss any shortcut that should be on that list? Let me know in the comment!