09 minute read

Terminal tricks I actually use every day

After years of avoiding the command line, Linux got me to embrace it. Here are the tricks that made the difference.

From macOS holdout to terminal person

Ever since moving to Linux, I’ve been spending more and more time at the command line—on purpose. The past me would have complained about this. He actually did! A few years ago, when I tested elementary OS for the first time, I complained about how much it still needed input through a command line.

Most of that complaining came from misplaced expectations. elementary OS positioned itself as a macOS-friendly distro. I was using macOS at the time, so I thought I’d blend right in. That couldn’t be further from the truth.

These days I’m using Omarchy—a much more terminal-focused system that’s heavily reliant on keyboard input, not just for keybindings but for commands themselves.

So the fact that I’m spending more time on the terminal is a good sign. It means I’m using the mouse less, which is a win in my book. It also means I’ve found plenty of ways to make my life easier. Some are built-in, others I’ve added on top.

When you miss sudo

You know when you’re entering a command that requires sudo but you forgot about it?

Bash
1
systemctl restart nginx

You’ll get some kind of Access denied error back. Here’s a quick way to rerun it with sudo:

Bash
12
sudo !!
> sudo systemctl restart nginx

!! expands to the last command you ran, prepending it with sudo.

When you have a typo

Sometimes you just forget how to type entirely and make a typo at the start of a long command:

Bash
1
rsync -avz --deleeete --exclude='node_modules' /home/you/projects/ /mnt/backups/projects/

You realise it a bit too late and now you have to go all the way to the start of the line.

Pressing Ctrl + A takes you to the start. Then Ctrl + → a couple of times lands you right where you need to be. Press Ctrl + Del and deleeete is gone. Just need to type it right this time.

Also, Ctrl + E takes you to the end of the line, which pairs nicely with Ctrl + ←.

When you want to clear the whole command

If instead of fixing typos you just want to completely get rid of the input without hitting backspace for a few seconds, press Ctrl + C. Or, if the cursor is at the end of the line, Ctrl + U will do it too.

When you want to delete all arguments

Say you’re trying to connect to a server via SSH:

Bash

But before hitting Enter you realise that’s the wrong user/server pair. Press Ctrl + K to delete everything after the cursor. Jump to the start with Ctrl + A, move forward with Ctrl + →, then Ctrl + K to delete everything after ssh.

When you delete what you didn’t mean to

Sometimes you delete too much and need to bring back what you just removed. Pressing Ctrl + Y does exactly that. In the example above, it would bring back [email protected].

When you just want a clean slate

At some point your terminal is full of noise you want to get rid of. Ctrl + L clears the visible screen.

This doesn’t actually clear anything—you can still scroll up and your history is still there. If you really want a clean slate, enter clear. No more scroll!

When you need to create a deep folder

You may know you can create a folder with mkdir. But if your target lives deep inside non-existing directories, you’d have to create them one by one:

Bash
1
mkdir /docker/containers/immich

If containers/ doesn’t exist, you can’t create immich like that. But you can with the -p flag:

Bash
1
mkdir -p /docker/containers/immich

This creates any parent directories needed along the way—in this case, containers/ and then immich/.

When you want to reuse the last argument

Let’s say you want to cd into that folder from above. You can quickly do:

Bash
1
cd !$

!$ expands to the last argument of the previous command—/docker/containers/immich.

When you want to quickly go home

Sometimes all we need is to go home. Just enter cd with nothing else.

You may know cd normally takes an argument like cd /home/you or cd ~. But cd on its own does the same and is far quicker.

When you need to go back

Then you need to go back to where you were. cd - takes you to the previous directory.

Using it repeatedly bounces you between your last two folders.

When you want to check your history

You know you entered a complex command at some point but don’t quite remember how it went. Ctrl + R pops up a search prompt where you can type terms.

Personally, I have that mapped to run fzf on the history file via fzf-history-widget. Not only can I fuzzy-find, I also see a list of recent matches.

And by the way, history is also a regular command you can type, which outputs the commands you entered in the past.

When you want just the last few entries

history may output a very long list. For me it’s over 4000 entries. To see just the last 5:

Bash
1
history | tail -5

When you want just the first few

Use head instead of tail:

Bash
1
history | head -5

When you want a scrollable output

If you want the full list with a way to scroll through it, use less:

Bash
1
history | less

You can search by pressing /, typing your terms, and hitting Enter.

When you want to search the output

To find every command containing dotfiles:

Bash
1
history | grep dotfiles

If you don’t have fzf set up, this is a great alternative.

Takeaways

If you value your command history, you can increase the number of entries it keeps—that may come in handy later. Also, if a command is prefixed with a space, it may not be saved to the history file. Useful when you enter sensitive information directly in commands.

Photo of Pedro