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: Ctrl-U cuts the line you are typing and Ctrl-Y pastes it back

Shell
~/tips/tip-bash-ctrl-u-ctrl-y-kill-yank

Ctrl-U # cut from the cursor to the start of the line

Parks a half-typed command so you can run another one first and paste it back.

Halfway through a long command you notice you have to check something first. Maybe a path, or whether a container is running. I used to hold Backspace and type it all again afterwards. Bash has a clipboard of its own for exactly this, and it’s called the kill ring.

Ctrl-U cuts everything from the cursor back to the start of the line and saves it on the kill ring. With the cursor at the end, that is the whole line. Now you have an empty prompt. Run whatever you needed, then press Ctrl-Y to paste the saved text back in.

Ctrl-U   # cut from the cursor to the start of the line
Ctrl-K   # cut from the cursor to the end of the line
Ctrl-W   # cut the word before the cursor
Ctrl-Y   # paste (yank) the last thing you cut

An example. You are typing

docker exec -it jellyfin ls /config/log

and you are not sure the container is called jellyfin. Press Ctrl-U, run docker ps, see the name, press Ctrl-Y and the half-typed command is back, cursor at the end, ready for Enter.

The kill ring remembers more than one cut. After Ctrl-Y, Alt-Y swaps the pasted text for the one you cut before that, and you can keep pressing it to go further back.

A caveat: this has nothing to do with the system clipboard. Text you cut with Ctrl-U cannot be pasted in your browser, and Cmd-V on a Mac pastes the system clipboard, not the kill ring. On a Mac the Alt-Y part also needs the Option key set to act as Meta in Terminal’s profile settings, or you press Esc and then Y.

Further reading: Killing and yanking (GNU Bash manual), bash(1) manual page

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link