Obvious and not-so-obvious Bash/Zsh terminal shortcuts
Tags: linux, bash, zsh, productivity
Published: 2020-06-18 22:34:47

---
title: Obvious and not-so-obvious Bash/Zsh terminal shortcuts
published: true
date: 2020-05-21 06:43:15 UTC
tags: linux,bash,zsh,productivity
canonical_url: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ncu31hc6g7vv0yd4mkud.png
---

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.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-tab.gif)

## 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.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-ctrl-C-ctrl-D.gif)

## 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.

![](https://konkit.tech/wp-content/uploads/2020/05/ctrl-z.gif)

## 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 **s** top). To resume it back, use the `Ctrl-Q`.

![](https://konkit.tech/wp-content/uploads/2020/05/ctrl-s-ctrl-q.gif)

## 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 **p** revious command) and Ctrl-N (like in **n** ext 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.

## Ctrl-R

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.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-ctrl-R.gif)

To display whole saved history, we can also use a `history` command.

## 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.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-ctrl-A-ctrl-E.gif)

`Ctrl-E` is like the End button. It moves the caret to the end of a line.

## Ctrl-L

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, using the `Ctrl-L` shortcut. You know – c **L** ear (not very intuitive, but it’s something …)

![](https://konkit.tech/wp-content/uploads/2020/05/ctrl-L.gif)

## Ctrl-W

If we make a mistake in a last written word, we can use this shortcut to quickly delete the last word.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-ctrl-W.gif)

## Ctrl-U

When we would like to remove a whole line, we can use `Ctrl-U` shortcut (like in **u** ndo) to clear the whole line.

![](https://konkit.tech/wp-content/uploads/2020/05/terminal-shortcuts-ctrl-U.gif)

## Ctrl-K

`Ctrl-K` shortcut deletes characters from the cursor to the end of the line.

## Ctrl-Y

Now, if we remove a command or its part by either of `Ctrl-K`, `Ctrl-U` or `Ctrl-W` shortcuts, we can paste it via `Ctrl-Y` shortcut (like in **y** ank). It’s very useful when you write a very long command, but then you realize, that you have to run another one first.

![](https://konkit.tech/wp-content/uploads/2020/05/ctrl-y.gif)

## That’s it

Did I miss any shortcut that should be on that list? Let me know in the comment!

The post [Obvious and not-so-obvious Bash/Zsh terminal shortcuts](https://konkit.tech/2020/05/21/obvious-and-not-obvious-bash-zsh-terminal-shortcuts/) appeared first on [Konkit's Tech Blog](https://konkit.tech).