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-_ undoes your last edit on the bash command line

Bash
~/tips/tip-bash-ctrl-underscore-undo

Ctrl-_ # undo the last edit

Takes back your last change on the command line, one step per press.

Everybody knows Ctrl-Z as undo, and in a terminal that one pauses your program instead. So most people assume the bash prompt has no undo at all. It does, it is just on a key nobody would guess.

Readline, the line editor bash uses, has an incremental undo bound to Ctrl-_ (Control and underscore). It is also on Ctrl-x Ctrl-u, which is easier to reach on some keyboards. Every press takes back one more edit on the line you are typing.

Ctrl-_          # undo the last edit
Ctrl-x Ctrl-u   # the same, other key
Alt-r           # undo every change to this line at once

Where this shines is after a cut that went too far. Say you typed

rsync -av --delete /mnt/user/documents/ /mnt/disks/backup/documents/

and pressed Ctrl-W a few times to remove the last path, but went one word too far and lost the source folder as well. Press Ctrl-_ and the words come back, one step at a time. The same goes for a Tab completion that filled in the wrong file name. One undo removes it again.

Alt-r (revert-line) goes back to how the line looked before you started changing it, which helps a lot when you pulled an old command from history and edited it beyond recognition.

A few caveats. Undo works per line, so once you press Enter that history is gone. On many keyboard layouts the underscore needs Shift, so it becomes a three-key press, and not every terminal app passes it on. If yours doesn’t, use Ctrl-x Ctrl-u. And for Alt-r on a Mac, either set Option to act as Meta in the Terminal profile or press Esc and then r.

Further reading: Some miscellaneous readline commands (GNU Bash manual), bash(1) manual page

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' #rsync -av --delete /srv/media/ /mnt/backup/media/ APP_HOST=example.com All Bash tips (52) All topics

Link