How do I clear/delete the current line in terminal?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To delete the current line in a terminal, 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. If your cursor is at the end of the line, Ctrl+U clears the entire line in one keystroke. These are shell line-editing shortcuts, not screen-clearing commands, and the distinction matters for daily productivity.
Core Line-Editing Shortcuts
In shells that use readline-style bindings (Bash, Zsh in emacs mode), these are the essential keys for line manipulation:
| 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 |
For a quick example, suppose you typed this by mistake:
Pressing Ctrl+U with the cursor at the end removes the entire line instantly, leaving you with a clean prompt.
Word-Level Deletion
When you only need to fix part of a command, word-level shortcuts are faster than clearing the whole line:
Pressing Ctrl+W three times removes --output=wide, then --namespace=production, then pods, one word at a time. Each deletion goes into the kill ring, so you can restore any of them.
Ctrl+U vs Ctrl+C: They Are Not the Same
These two shortcuts can both leave you with a clean prompt, but they work completely differently.
Ctrl+U edits the input buffer. It removes text you have typed but have not yet submitted. No signal is sent to any process. The shell stays in its normal editing state.
Ctrl+C sends SIGINT to the foreground process group. If you are still typing a command, it discards the current input and gives you a new prompt. If a command is already running, it may terminate that process.
| 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 |
The practical rule: use Ctrl+U when editing, Ctrl+C when canceling.
Movement Shortcuts That Complement Deletion
Line editing becomes much faster when you combine movement with deletion. The movement keys in readline-style shells are:
| 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 |
A common pattern: Ctrl+A then Ctrl+K clears the entire line regardless of cursor position. This is equivalent to Ctrl+E then Ctrl+U. Both approaches work, and experienced users tend to pick whichever matches the direction they were already moving.
The Kill Ring: Undo for Your Command Line
Readline-based shells maintain a kill ring. Every time you use Ctrl+U, Ctrl+K, Ctrl+W, or Alt+D, the deleted text is pushed into this ring. You can recall it with Ctrl+Y.
Example workflow:
- 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 kill ring holds multiple entries. After pressing Ctrl+Y, press Alt+Y repeatedly to cycle through older kills. This makes line editing much less risky, especially when you are working with complex commands.
Clearing the Line vs Clearing the Screen
These are different operations and should not be confused.
Clearing the line (Ctrl+U, Ctrl+K) removes text from the input buffer. It affects what you are typing.
Clearing the screen (Ctrl+L or the clear command) redraws the terminal display. Your current input line is preserved and moved to the top of the screen.
If you want to clear visual clutter without losing your current command, use Ctrl+L. If you want to erase what you have typed, use Ctrl+U.
Shell-Specific Differences
The exact key bindings depend on your shell and its editing mode.
| 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 default behavior for Ctrl+U is notably different from Bash: it kills the entire line, not just from the cursor to the start. This catches people who switch between the two shells.
To inspect your current key bindings:
Vi Mode Differences
If your shell is set to vi mode (set -o vi in Bash, bindkey -v in Zsh), the editing shortcuts change entirely. In vi normal mode, dd clears the current line, D deletes to end of line, and cc clears and enters insert mode. The readline shortcuts like Ctrl+U may still work in insert mode depending on configuration.
Terminal Output Rewriting (Programmatic Use)
Sometimes the question "how do I clear the current line" refers not to shell editing but to rewriting output in a script. That is a different problem entirely.
To overwrite the current output line in a script, 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) provide finer control for progress bars and status displays, but they have nothing to do with Ctrl+U or shell input editing.
Common Pitfalls
Using Ctrl+C when you only wanted to erase typed input is the most common mistake. It can kill a running foreground process instead of just fixing the command line, and the deleted text cannot be recovered with Ctrl+Y.
Assuming every terminal uses the same bindings leads to confusion, especially the Bash vs Zsh difference for Ctrl+U. Always check your shell's current bindings if a shortcut does not behave as expected.
Mixing up screen clearing with line editing is another frequent issue. Ctrl+L and clear redraw the display but do not modify the current input buffer. They are not substitutes for Ctrl+U or Ctrl+K.
Forgetting about vi mode is a subtle trap. If someone set set -o vi in your shell configuration, all the emacs-style shortcuts stop working, and the debugging experience can be confusing.
Summary
- Use
Ctrl+Uto delete from the cursor back to the start of the line (the most common "clear the line" action). - Use
Ctrl+Kto delete from the cursor to the end of the line. - Use
Ctrl+WandAlt+Dfor word-level deletion. - Use
Ctrl+Cto cancel or interrupt, not just to edit the input. - Use
Ctrl+Yto restore recently killed text from the readline kill ring. - Use
Ctrl+Lto clear the screen without affecting your current input. - Check shell-specific bindings, especially when switching between Bash and Zsh, since
Ctrl+Ubehaves differently in each.
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 discover memory usage of my application in Android?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.