# How clear the printed content in terminal and print to the same line?

**URL:** <https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549>\
**Category:** New to Julia\
**Created:** [January 12, 2019, 3:01am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549 "2019-01-12T03:01:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 12, 2019, 3:01am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/1 "2019-01-12T03:01:59Z")

</div>

I can `print(1)` to print to the terminal. But I want clear the `1` that I just printed and on the same line in the terminal `print(2)`. Is that possible?

---

<div class="post-metadata">

**Author:** ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)\
**Post date:** [January 12, 2019, 4:29am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/2 "2019-01-12T04:29:27Z")

</div>

Printing `'\r'` character will virtually clear the current line in most terminals.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [January 12, 2019, 4:43am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/3 "2019-01-12T04:43:10Z")

</div>

You can try using ANSI codes (to my understanding, an ancient standard of terminal manipulation), here is a code of a function that overwrites the last line and prints something new

```julia
#source: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
# a more complete description of the codes used.

function overprint(str)  
    print("\u1b[1F")
    #Moves cursor to beginning of the line n (default 1) lines up   
    print(str) #prints the new line
   print("\u1b[0K") 
   # clears part of the line.
   #If n is 0 (or missing), clear from cursor to the end of the line. 
   #If n is 1, clear from cursor to beginning of the line. 
   #If n is 2, clear entire line. 
   #Cursor position does not change. 

    println() #prints a new line, i really don't like this arcane codes
end

#testing
println(1)
println(2)
println(3)
println(4)
println(5)
println("this is the number six")
overprint(7)
println(8)

```

Output:

```julia
1
2
3
4
5
7
8

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 12, 2019, 10:13am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/4 "2019-01-12T10:13:00Z")

</div>

The standard library REPL has some terminal support standardized, eg see `REPL.Terminals.clear_line`.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 13, 2019, 12:20am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/5 "2019-01-13T00:20:53Z")

</div>

> [@Tamas\_Papp](#):
>
> `REPL.Terminals.clear_line` .

How you actually use it? `?REPL.Terminals.clear_line` wasn’t very helpful

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [January 13, 2019, 2:05am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/6 "2019-01-13T02:05:57Z")

</div>

Per [https://en.wikipedia.org/wiki/ANSI\_escape\_code#CSI\_sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences):

```julia
print("ayy")
print("\e[2K") # clear whole line
print("\e[1G") # move cursor to column 1
print("lmao")

```

You should now see just “lmao” on the current (or maybe previous, if in the REPL) line. Personally, I find using escape codes manually much easier and more predictable than trying to re-using functionality that wasn’t designed for the use-case that I have in mind.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [January 13, 2019, 2:10am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/7 "2019-01-13T02:10:32Z")

</div>

> [@longemen3000](#):
>
> (to my understanding, an ancient standard of terminal manipulation)

I’d like to take offense to this statement as a terminal lover, but I can understand that these sequences appear cryptic. Note, however, that ANSI escape codes are used in a variety of modern terminal-first programs to great effect, and so the term ~~“archaic” is maybe not accurate.~~ “ancient” is actually accurate, but doesn’t really matter anyway.

EDIT:

> [@longemen3000](#):
>
> print(“\u1b[1F”) #Moves cursor to beginning of the line n (default 1) lines up print(str) #prints the new line

This is also incorrect, based on what the OP requested:

> [@xiaodai](#):
>
> But I want clear the `1` that I just printed and on the same line in the terminal `print(2)` .

So escape code `F` would not be what you want to use, since that assumes you’re already on the next line. Instead, `G` is best used here, as a `print()` will not necessarily advance the terminal by a newline.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 13, 2019, 9:44am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/8 "2019-01-13T09:44:37Z")

</div>

> [@jpsamaroo](#):
>
> I’d like to take offense to this statement as a terminal lover, but I can understand that these sequences appear cryptic. Note, however, that ANSI escape codes are used in a variety of modern terminal-first programs to great effect, and so the term “archaic” is maybe not accurate.

I just want to point out that “ancient” and “archaic” don’t really mean the same thing.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 13, 2019, 11:42am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/9 "2019-01-13T11:42:24Z")

</div>

Yes, the package cannot be accused of being overdocumented 😉 I would try something like

```julia
import REPL.Terminals
terminal = Terminals.TTYTerminal("", stdin, stdout, stderr)

function f(terminal)
    print("this message will self-destruct in 5 seconds")
    sleep(5)
    REPL.Terminals.clear_line(terminal)
    print("bang")
end

f(terminal)

```

but I am not an expert on this.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [May 30, 2023, 10:05am UTC](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549/10 "2023-05-30T10:05:28Z")

</div>

It is also possible to use the functions defined in `REPL.Terminals`, instead of using the escape sequences directly (the functions just print the sequences anyway):

```julia
import REPL
terminal = REPL.Terminals.TTYTerminal(string(), stdin, stdout, stderr)
println() # Dummy line
for i in (1, "two", :III, "||||")
	REPL.Terminals.cmove_left(terminal, length(string(i)))
	REPL.Terminals.cmove_line_up(terminal)
	println(i)
	sleep(0.5)
end
println("Watch the final number dissapear above me")
sleep(0.5)
REPL.Terminals.cmove_line_up(terminal, 2)
REPL.Terminals.clear_line(terminal)
REPL.Terminals.cmove_line_down(terminal, 3) # If not, next input is above previous output, which looks funny

```

[https://global.discourse-cdn.com/julialang/original/3X/e/3/e3462d91b15938cae094755d4ce7b3a92c6f6e7f.mp4](https://global.discourse-cdn.com/julialang/original/3X/e/3/e3462d91b15938cae094755d4ce7b3a92c6f6e7f.mp4)

This was discovered as I was writing up [Overwriting previous terminal output with built-in functions](https://discourse.julialang.org/t/overwriting-previous-terminal-output-with-built-in-functions/99593).
