brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Command-K in Mac Terminal clears the scrollback, clear only hides it

Terminal
~/tips/tip-terminal-command-k-clear-scrollback

clear && printf '\033[3J'

Wipes the whole scrollback in Terminal on a Mac instead of just the visible screen.

Typing clear looks like it empties the window. Scroll up and everything is still there, including the token you just printed.

clear pushes the old content up and gives you a blank screen, and many terminals keep the scrollback. In Terminal on a Mac the shortcut that really wipes it is Command-K, listed in the menu as Clear to Start. Everything above the prompt is gone, scrollback included.

From a script, the escape sequence for erasing the saved lines does the same in Terminal, iTerm2 and most xterm-like terminals:

clear && printf '\033[3J'

Its sibling Command-L clears only back to the previous mark, which is handy if you want to drop the output of the last command and keep the rest. Marks are the same ones Command-Up Arrow jumps between.

Command-K is worth pressing, I think, before you share your screen or a screenshot, and after anything printed a password or key. Keep in mind it does nothing to your shell history or to log files. If the secret was part of the command line itself, it is still in ~/.zsh_history, so that needs its own cleanup.

Further reading: Keyboard shortcuts in Terminal on Mac (Apple Support), Xterm control sequences (ED 3)

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link