# Delete user input from terminal?

**URL:** <https://discourse.julialang.org/t/delete-user-input-from-terminal/91619>\
**Category:** General Usage\
**Tags:** repl, io\
**Created:** [December 13, 2022, 9:37pm UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619 "2022-12-13T21:37:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [December 13, 2022, 9:37pm UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619/1 "2022-12-13T21:37:40Z")

</div>

I’m working on a little project that’s a “personal assistant” to me manage my todo list.

I’m using [Term.jl](https://github.com/FedeClaudi/Term.jl) to make it look pretty. I’m trying to emulate sort of a chat box aesthetic.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/7/e724a72a4c08083b0a63524b39a6c18a5db4c5e6.png)

It’s bugging me that I can’t remove the first “check\_email” where I was typing before printing out the chatbox version. I tried some solutions from [here](https://discourse.julialang.org/t/how-clear-the-printed-content-in-terminal-and-print-to-the-same-line/19549), but they don’t seem to work on user text.

Example:

```julia
using REPL
import REPL.Terminals
terminal = Terminals.TTYTerminal("", stdin, stdout, stderr)
function user_input()
    print("how can I help?\n")
    response = readline()
    # I've also tried some other "clear last line" options with escape characters
    REPL.Terminals.clear_line(terminal)
    print("your response was $(response)")
end

```

I’d appreciate any ideas!

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 16, 2022, 10:38am UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619/2 "2022-12-16T10:38:19Z")

</div>

You can try using terminal codes.

```julia
julia> n = 10;

julia> print("."^n * "\n" * "."^(2*n) * "\n" * "."^(3n));
..........
....................
..............................
julia> print("."^n * "\n" * "."^(2*n) * "\n" * "."^(3n));print("\e[1A\e[2K");
..........

julia> 

```

`\e[1A` moves the cursor up. `\e2K` deletes the entire line (although it seems to delete the next one as well). My example was tested in Julia 1.7.0 in the default Ubuntu terminal, different terminals may implement escape codes differently.

[Move Cursor](https://rosettacode.org/wiki/Terminal_control/Cursor_movement#Julia)  
[ANSI Escape Codes (Wikipedia)](https://en.wikipedia.org/wiki/ANSI_escape_code)  
[Terminal Codes](https://wiki.bash-hackers.org/scripting/terminalcodes) (page does load but is very slow)

For more formatting examples try this code in Julia:

```julia
for i=0:1:10
        println("")
        for j=0:1:9
                print("\033[" * string(10*i + j) * "m" * string(10*i + j) * "\033[m\t")
        end
end

```

I would paste the results here but it wouldn’t be properly formatted. 🤷🏻‍♂️

---

<div class="post-metadata">

**Author:** ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)\
**Post date:** [December 20, 2022, 11:20pm UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619/3 "2022-12-20T23:20:47Z")

</div>

That looks amazing, looking forward to see what you’ll build with Term!

Term has some methods that can help with, for example [clearing a line](https://github.com/FedeClaudi/Term.jl/blob/a153b24ab3e26a421ea3a156ee33acd2e2ed2b16/src/console.jl#L100) from the terminal. These make use of ANSI control codes printed to the terminal, allowing you to move the cursor around, delete stuff etc.

For example

```julia
using Term
using Term.Consoles

println("this will show")
println("this will be erased")
up()
erase_line()
println("this will also show")

```

Hope this helps, get in touch if you have any questions!

---

<div class="post-metadata">

**Author:** ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)\
**Post date:** [December 20, 2022, 11:21pm UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619/4 "2022-12-20T23:21:53Z")

</div>

Also, `Term` version `2.0` will have `Prompt` functionality: [Prompt · Term.jl](https://fedeclaudi.github.io/Term.jl/dev/basics/prompt/)

If you’re brave enough you can grab the code from github and start playing around with it.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [December 20, 2022, 11:50pm UTC](https://discourse.julialang.org/t/delete-user-input-from-terminal/91619/5 "2022-12-20T23:50:26Z")

</div>

Dope! Yeah, I’ll take a look!
