# How to get a handle to the Terminal

**URL:** <https://discourse.julialang.org/t/how-to-get-a-handle-to-the-terminal/99592>\
**Category:** General Usage\
**Tags:** repl\
**Created:** [May 30, 2023, 8:46am UTC](https://discourse.julialang.org/t/how-to-get-a-handle-to-the-terminal/99592 "2023-05-30T08:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<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, 8:46am UTC](https://discourse.julialang.org/t/how-to-get-a-handle-to-the-terminal/99592/1 "2023-05-30T08:46:15Z")

</div>

Let’s say I want to clear my terminal programmatically, i.e. not with Ctrl+l, but by running Julia code. I can do the following:

```julia
import REPL
julia> t = REPL.Terminals.TTYTerminal(string(), stdin, stdout, stderr)
REPL.Terminals.clear(t); # ; to supress some integer return value

```

Also,

```julia
julia> REPL.Terminals.TTYTerminal <: REPL.Terminals.UnixTerminal
true

```

, which makes me wonder - would this not work on windows?

My question is: Are there no simpler ways to get a handle to the terminal, the one I bind to `t`, than to construct a `TTYTerminal`, which I don’t even know what is? I imagine a function like `REPL.current_repl()`, or something to that effect. It is possible this exists, because there is a lot of undocumented stuff going on inside the REPL module.

---

<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:** [June 8, 2023, 7:22am UTC](https://discourse.julialang.org/t/how-to-get-a-handle-to-the-terminal/99592/2 "2023-06-08T07:22:05Z")

</div>

A solution is to define something like the following

```julia
import REPL
function current_terminal()
    if Sys.isunix()
        return REPL.Terminals.TTYTerminal("TerminalType (what should go here?)", stdin, stdout, stderr)
    #elseif Sys.isapple()
        # Please help
    #elseif Sys.iswindows()

    else
        error("`current_terminal` is not implemented for your operating system (=$(Base.BinaryPlatforms.os())).")
    end
end

```

Would a function like this (with better messages and more platform support) be welcome into the REPL stdlib? I for one would love to have it, and it would also make it simpler to add docstrings to show usage of e.g. the family of functions starting with `REPL.Terminals.cmove`, which I would like to take a crack at documenting.

The function name is akin to `current_figure` and `current_axis`, inspired by Makie. Naming suggestions are also welcome.

I am also quite unsure about non-unix systems, because of the type tree below:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/b/2b03fa3cd5e7c8e8179d7b4ed61fdd6798e8a833.png)  
AbstractTerminal only has TextTerminal as subtype, which only has UnixTerminal as subtype. What about other operating systems?

---

<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:** [June 13, 2023, 11:05am UTC](https://discourse.julialang.org/t/how-to-get-a-handle-to-the-terminal/99592/3 "2023-06-13T11:05:50Z")

</div>

I just discovered, by unrelatedly digging into the internals of `RemoteREPL.jl`, that there is a variable `Base.active_repl`, which _almost_ is what I want. Specifically, `Base.active_repl` is a subtype of `REPL.AbstractREPL`, while I need a subtype of `REPL.Terminals.AbstractTerminal`. Fortunately, `Base.activeREPL` has a field `t`, which fits the bill:

```julia
julia> Base.active_repl.t isa REPL.Terminals.AbstractTerminal
true

```

So to clear the terminal, one can use

```julia
REPL.Terminals.clear(Base.active_repl.t);

```

, where the semi-colon supresses the 7 returned from `write`.
