How to clear or delete the current line in the terminal
Codemia Knowledge Hub ·
Press Ctrl+U to erase from the cursor back to the beginning of the line, or Ctrl+K to erase from the cursor to the end. With the cursor at the end of the line, Ctrl+U clears the whole line in one keystroke. These are shell line-editing shortcuts, not screen-clearing commands.
Line-editing shortcuts
In shells with readline-style bindings (Bash, Zsh in emacs mode):
| Shortcut | Action |
Ctrl+U | Delete from cursor to beginning of line |
Ctrl+K | Delete from cursor to end of line |
Ctrl+W | Delete the previous word |
Alt+D | Delete the next word (forward) |
Ctrl+C | Cancel the current line or interrupt a running process |
Ctrl+Y | Yank (paste) the last deleted text |
Suppose you typed this by mistake:
Ctrl+U with the cursor at the end removes the entire line, leaving a clean prompt.
Deleting one word at a time
To fix only part of a command, word-level deletion is faster:
Pressing Ctrl+W three times removes --output=wide, then --namespace=production, then pods. Each deletion goes into the kill ring, so you can restore any of them.
Ctrl+U vs Ctrl+C
Both can leave you at a clean prompt, but they work differently.
Ctrl+U edits the input buffer. It removes text you typed but have not submitted, sends no signal, and leaves the shell in its normal editing state.
Ctrl+C sends SIGINT to the foreground process group. While typing, it discards the input and gives a new prompt. If a command is running, it may terminate it.
| Situation | Use Ctrl+U | Use Ctrl+C |
| You mistyped a command and want to start over | Yes | Possible but heavier |
| A running process needs to be interrupted | No | Yes |
You want to restore the deleted text with Ctrl+Y | Yes | No (text is gone) |
| You want to keep the shell in normal editing state | Yes | Resets some state |
Use Ctrl+U when editing and Ctrl+C when canceling. Reaching for Ctrl+C just to erase input is the most common mistake: it can kill a foreground process, and the text cannot be recovered with Ctrl+Y.
Clearing the whole line from any cursor position
Movement keys in readline-style shells:
| Shortcut | Action |
Ctrl+A | Move to the beginning of the line |
Ctrl+E | Move to the end of the line |
Alt+B | Move backward one word |
Alt+F | Move forward one word |
Ctrl+B | Move backward one character |
Ctrl+F | Move forward one character |
Ctrl+A then Ctrl+K clears the entire line regardless of cursor position, as does Ctrl+E then Ctrl+U.
Restoring deleted text with the kill ring
Every Ctrl+U, Ctrl+K, Ctrl+W or Alt+D pushes the deleted text into the readline kill ring, and Ctrl+Y pastes it back:
- Type a long command with many flags
- Press
Ctrl+Uto clear the line - Realize you actually needed that command
- Press
Ctrl+Yto paste it back
The ring holds multiple entries. After Ctrl+Y, press Alt+Y repeatedly to cycle through older kills.
Clearing the line vs clearing the screen
Clearing the line (Ctrl+U, Ctrl+K) removes text from the input buffer.
Clearing the screen (Ctrl+L or clear) redraws the terminal display. Your current input is preserved and moved to the top.
Use Ctrl+L to remove visual clutter without losing your command, and Ctrl+U to erase what you typed.
Bash, Zsh, Fish and PowerShell differences
| Shell | Default Mode | Ctrl+U Behavior | Notes |
| Bash | Emacs | Kills to beginning of line | Standard readline |
| Zsh | Emacs | Kills entire line (regardless of cursor) | Different from Bash |
| Fish | Custom | Kills to beginning of line | Own key handling |
| PowerShell | PSReadLine | Kills to beginning of line | Configurable |
Zsh's Ctrl+U kills the entire line, not just from the cursor to the start, which catches people who switch between the two shells. If a shortcut misbehaves, inspect your bindings:
Vi mode
In vi mode (set -o vi in Bash, bindkey -v in Zsh), the emacs-style shortcuts stop working. In vi normal mode, dd clears the current line, D deletes to end of line, and cc clears and enters insert mode. Ctrl+U may still work in insert mode depending on configuration. If someone set vi mode in your shell config, this explains shortcuts that suddenly do nothing.
Overwriting the current output line in a script
Sometimes "clear the current line" means rewriting output from a script, which is a different problem. Use a carriage return (\r) without a newline:
ANSI escape sequences like \033[K (clear to end of line) and \033[1A (move up one line) give finer control for progress bars and status displays. They are unrelated to Ctrl+U or shell input editing.
Related reading
- How do I configure Tensorflow Serving to serve models from HDFS?
- How do I create a release build in Xcode?
- How do I define the name of image built with docker-compose
- How do I deploy updated Docker images to Amazon ECS tasks?
- How do I configure spring-kafka to ignore messages in the wrong format?
- How do I debug a Kubernetes validating admission webhook?
- How do I disable log messages from the Requests library?
- How do I do logging in C without using 3rd party libraries?
System design interview problems on this topic
- Design a Metrics Monitoring and Alerting System: solution and walkthrough
- Design a Distributed Tracing System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.