Terminal Commands
Troubleshooting
Programming
Command Line
System Administration

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.

Practice system design

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:

ShortcutAction
Ctrl+UDelete from cursor to beginning of line
Ctrl+KDelete from cursor to end of line
Ctrl+WDelete the previous word
Alt+DDelete the next word (forward)
Ctrl+CCancel the current line or interrupt a running process
Ctrl+YYank (paste) the last deleted text

For a quick example, suppose you typed this by mistake:

text
docker run --name web --rm -p 8080:8080 -v /data:/data my-image:latest

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:

text
kubectl get pods --namespace=production --output=wide
                                                    ^ cursor here

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.

SituationUse Ctrl+UUse Ctrl+C
You mistyped a command and want to start overYesPossible but heavier
A running process needs to be interruptedNoYes
You want to restore the deleted text with Ctrl+YYesNo (text is gone)
You want to keep the shell in normal editing stateYesResets 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:

ShortcutAction
Ctrl+AMove to the beginning of the line
Ctrl+EMove to the end of the line
Alt+BMove backward one word
Alt+FMove forward one word
Ctrl+BMove backward one character
Ctrl+FMove 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:

  1. Type a long command with many flags
  2. Press Ctrl+U to clear the line
  3. Realize you actually needed that command
  4. Press Ctrl+Y to 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.

bash
1# This clears the screen, not the input
2clear
3
4# Ctrl+L does the same thing but is faster
5# Your partially typed command stays intact

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.

ShellDefault ModeCtrl+U BehaviorNotes
BashEmacsKills to beginning of lineStandard readline
ZshEmacsKills entire line (regardless of cursor)Different from Bash
FishCustomKills to beginning of lineOwn key handling
PowerShellPSReadLineKills to beginning of lineConfigurable

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:

bash
1# In Zsh
2bindkey -L | grep kill
3
4# In Bash
5bind -P | grep -E 'kill-line|unix-line-discard|backward-kill-word'

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:

python
1import sys
2import time
3
4for i in range(1, 101):
5    sys.stdout.write(f"\rDownloading: {i}%")
6    sys.stdout.flush()
7    time.sleep(0.02)
8
9print("\nDone")

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+U to delete from the cursor back to the start of the line (the most common "clear the line" action).
  • Use Ctrl+K to delete from the cursor to the end of the line.
  • Use Ctrl+W and Alt+D for word-level deletion.
  • Use Ctrl+C to cancel or interrupt, not just to edit the input.
  • Use Ctrl+Y to restore recently killed text from the readline kill ring.
  • Use Ctrl+L to clear the screen without affecting your current input.
  • Check shell-specific bindings, especially when switching between Bash and Zsh, since Ctrl+U behaves differently in each.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.