# Current REPL number

**URL:** https://discourse.julialang.org/t/current-repl-number/76240
**Category:** General Usage
**Tags:** repl
**Created:** [February 11, 2022, 11:26am UTC](https://discourse.julialang.org/t/current-repl-number/76240 "2022-02-11T11:26:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 11, 2022, 11:26am UTC](https://discourse.julialang.org/t/current-repl-number/76240/1 "2022-02-11T11:26:50Z")

</div>

Does Julia provide an API for extracting the current REPL number?

By “current REPL number”, I mean the `x` in `REPL[x]` in error messages like the following:

```julia
julia> error()
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:42
 [2] top-level scope
   @ REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 11, 2022, 11:53am UTC](https://discourse.julialang.org/t/current-repl-number/76240/2 "2022-02-11T11:53:37Z")

</div>

`@ __FILE__ ` in the REPL.

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 11, 2022, 11:56am UTC](https://discourse.julialang.org/t/current-repl-number/76240/3 "2022-02-11T11:56:28Z")

</div>

Ok, but I’m looking for something which works also in library code.

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [February 11, 2022, 12:32pm UTC](https://discourse.julialang.org/t/current-repl-number/76240/4 "2022-02-11T12:32:43Z")

</div>

I use this to set my repl prompt in startup.jl:

```julia
function repl_line() 
    hist = Base.active_repl.interface.modes[1].hist
    hist.cur_idx - hist.start_idx
end

```

(or actually, `repl.interface.modes[1].hist` if i get a handle on `repl` from `atreplinit`)

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 11, 2022, 12:59pm UTC](https://discourse.julialang.org/t/current-repl-number/76240/6 "2022-02-11T12:59:18Z")

</div>

This is close to what I want, but it seems to have some quirks:

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' | |
  | | |_| | | | (_| | | Version 1.7.2 (2022-02-06)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> function repl_line() 
           hist = Base.active_repl.interface.modes[1].hist
           hist.cur_idx - hist.start_idx
       end;

julia> repl_line()
0 # Would have expected this to be 2, but fine

julia> repl_line()
2 # Why does it jump by 2 now?

julia> repl_line()
2 # No increment, presumably because same input

julia> exp(1); # Dummy input 

julia> repl_line()
2 # Still same?!

julia> repl_line()
4 # Jumping by 2 again

```

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [February 11, 2022, 1:15pm UTC](https://discourse.julialang.org/t/current-repl-number/76240/7 "2022-02-11T13:15:32Z")

</div>

> [@ettersi](#):
>
> ```julia
> function repl_line() 
> hist = Base.active_repl.interface.modes[1].hist
> hist.cur_idx - hist.start_idx
> end
> 
> ```

Yes this is tricky, this is because you used something like uparrow or Ctrl-p to go back to previous history entries, this is how the REPL works. For example, the first `repl_line()` call shows 0: this is presumably because you used uparrow instead of actually typing `repl_line()`, and then `repl_line()` was the last thing typed in the previous julia session.

The alternative is to use: `length(hist.history) + 1 - hist.start_idx` (which i actually also show in my REPL prompt when it differs from the `repl_line()` above, so e.g. my prompt shows `0/2>` for your first `repl_line()` example if i pressed uparrow).

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 11, 2022, 1:22pm UTC](https://discourse.julialang.org/t/current-repl-number/76240/9 "2022-02-11T13:22:44Z")

</div>

Better, but still not perfect 😢

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' | |
  | | |_| | | | (_| | | Version 1.7.2 (2022-02-06)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> function repl_line() 
           hist = Base.active_repl.interface.modes[1].hist
           length(hist.history) + 1 - hist.start_idx
       end;

julia> repl_line()
2

julia> repl_line()
2 # Still no update for ctrl+up inputs

julia> exp(1); # Dummy input

julia> repl_line()
4 # But at least immediate updates

```

Thanks for your help anyway, much appreciated! ❤
