Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

clear vs CTRL + L in Your Shell

clear-vs-ctrl-l-in-your-shell.jpg

It may look like they do the same thing but there's a subtle difference with how your screen is cleared.

Quick Jump:

I recently received a comment on one of my YouTube videos which made me chuckle because I remember typing clear quite a lot in that video.

ctrl-l.jpg

Sometimes while recording videos or giving live demos your brain is focused on other tasks instead of optimizing how you use hotkeys or shortcuts. I know I sometimes use Vim differently when recording videos because not everything is pure muscle memory.

When editing a video I’ll catch myself thinking “really, why didn’t you just hit ciw to replace that word in Vim?” after watching myself select it with the mouse, delete it and replace it.

CTRL + L vs clear is one of those cases, there’s a subtle difference on what they do:

  • CTRL + L clears the visible output on the screen so your prompt is back on line 1 but you can still scroll up in your buffer and see the previous output
  • clear does the same as CTRL + L except it will also clear your buffer so if you scroll up there won’t be any previous output

One neat tip is you can use CTRL + L to clear your visible screen even if have an unsent command typed out and it won’t clear your command.

Technically CTRL + L is a shortcut for clear -x. In practice I’ve found both ways of clearing your screen works the same in sh, bash and zsh with and without using tmux but you may notice different behavior depending on which terminal you’re using in case it interprets CTRL + L differently.

Some terminals even support CTRL + K which does the same as clear. I know with the Microsoft Terminal it’s not bound by default but you can add a new action for “Clear buffer” and assign it to CTRL + K or whatever you want.

In normal usage (at least for me) using CTRL + L gets the job done which is to reset my screen so I can start at the top and hitting that hotkey is surely going to be faster than typing out clear -x or even clear. I need to remember to use it more.

Using clear still has its uses though. Maybe you have sensitive output that was written to your screen and now you want it removed from your screen and buffer, using clear solves that nicely.

The video below shows the differences in action.

# Demo Video

Timestamps

  • 0:58 – Seeing how CTRL + L works and how clear is different
  • 1:45 – CTRL + L is a shortcut for running clear -x
  • 2:17 – Mapping a full clear to a hotkey
  • 2:38 – When to use CTRL + L vs clear?
  • 3:01 – Binding CTRL + K with the Microsoft Terminal

Which one do you typically use from muscle memory? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments