# Erasing a line in the REPL

**URL:** <https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244>\
**Category:** General Usage\
**Tags:** repl\
**Created:** [March 6, 2024, 2:39pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244 "2024-03-06T14:39:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [March 6, 2024, 2:39pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/1 "2024-03-06T14:39:56Z")

</div>

Hi, is there a way to erase a line from the REPL ?  
I’m using ProgressMeter but I’d also like to occasionally print a line when an event occurs.  
I made the following MWE. Basically, I’d like the “I reached…” statement to erase the progress line, and in subsequent iterations, the progress will appear at the last line.

```julia
julia> using ProgressMeter

julia> prog = ProgressUnknown()
ProgressUnknown(false, false, 0, ProgressMeter.ProgressCore(:green, "Progress: ", 0.1, true, 0, Base.TTY(Base.Libc.WindowsRawSocket(0x000000000000027c) open, 0 bytes waiting), false, 1, 0, ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (8, 1, 142273203075)), 0, 1, false, Int64[], 1.709735239111e9, 1.709735239111e9, 1.709735239111e9))

julia> for i in 1:100
       next!(prog)
       sleep(0.1)
       if i%25==0
       println("I reached $i")
       end
       end
Progress: 25 Time: 0:01:16I reached 25
Progress: 50 Time: 0:01:18I reached 50
Progress: 75 Time: 0:01:21I reached 75
Progress: 100 Time: 0:01:24I reached 100

```

`println(rpad("\rI reached $i",displaysize(stdout)[2]))` works a little better for the first line:

```julia
I reached 25
Progress: 50 Time: 0:00:06I reached 50                                                                                                                                                                       

Progress: 75 Time: 0:00:09I reached 75                                                                                                                                                                       

Progress: 100 Time: 0:00:12I reached 100 

```

But it creates unwanted blank lines, in addition to not working for the other lines.

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [March 6, 2024, 3:41pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/2 "2024-03-06T15:41:40Z")

</div>

To do so, you’ll want to print out a control sequence which instructs the terminal to delete the current line. The sequence for doing so is `"\e[A\e[2K"` which you can see in action in the following function:

```julia
julia> function hello()
           println("hello")
           sleep(1)
           print("\e[A\e[2K")
           println("goodbye!")
       end
hello (generic function with 1 method)

julia> hello()
# displays "hello"
# then clears the line and displays "goodbye!"

```

I tried altering your MWE:

```julia
julia> for i in 1:100
       next!(prog)
       sleep(0.1)
       if i%25==0
       print("\e[A\e[2K")
       println("I reached $i")
       end

```

It’s got some alignment issues, I’m not sure if it’s exactly what you’re going for, but it should at least get you pointed in the right direction.

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [March 6, 2024, 3:46pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/3 "2024-03-06T15:46:08Z")

</div>

Looks good! I fixed the alignment issue by adding a \r. However, now, the “I reached” lines erase one another. I’m not sure why this is erasing two lines.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [March 6, 2024, 9:40pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/4 "2024-03-06T21:40:40Z")

</div>

> [@HenriDeh](#):
>
> I’m not sure why this is erasing two lines.

`print("\e[A\e[2K")` translates as “go up one line and erase that line”. To erase the line you’re currently on is just `print("\e[2K")`. It’s not 100% clear to me the exact effect you’re going for, but hopefully this should help you achieve it.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 7, 2024, 6:44am UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/5 "2024-03-07T06:44:12Z")

</div>

A `\r` could be used for erasing a line as well.

```julia-repl
julia> begin
           print("Hello")
           sleep(1)
           print("\rWorld")
       end

```

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [March 7, 2024, 7:37am UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/6 "2024-03-07T07:37:12Z")

</div>

See also `set_multiline_postfix` in alternative package ProgressBars.jl which supports this as a built in feature: [GitHub - cloud-oak/ProgressBars.jl: A Julia clone of https://pypi.python.org/pypi/tqdm](https://github.com/cloud-oak/ProgressBars.jl?tab=readme-ov-file#usage)

---

<div class="post-metadata">

**Author:** ![piechologist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piechologist/32/27662_2.png) [@piechologist](https://discourse.julialang.org/u/piechologist)\
**Post date:** [March 7, 2024, 9:09am UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/7 "2024-03-07T09:09:43Z")

</div>

`\r` takes you back to the first column and you overwrite the old content. This can be dangerous if the new content is shorter than the old one:

```Julia
begin
    print(100)
    sleep(1)
    print("\r", 2) # better: print("\r\e[2K", 2)
end

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [March 7, 2024, 4:45pm UTC](https://discourse.julialang.org/t/erasing-a-line-in-the-repl/111244/8 "2024-03-07T16:45:11Z")

</div>

> [@mkitti](#):
>
> A `\r` could be used for erasing a line as well.

That’s not _quite_ what’s happening here, try this one:

```julia
julia> begin
           print("Hello of Warcraft")
           sleep(1)
           print("\rWorld")
       end

```

But one can use `"\e[K"` to erase what’s left of the line, if desired:

```julia
julia> begin
           print("Hello of Warcraft")
           sleep(1)
           print("\rWorld\e[K")
       end

```
