Terminal Commands
Troubleshooting
Programming
Command Line
System Administration

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):

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

Suppose you typed this by mistake:

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

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:

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

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.

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

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:

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

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:

  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 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.

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

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

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 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:

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

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:

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) give finer control for progress bars and status displays. They are unrelated to Ctrl+U or shell input editing.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design