# Export from REPL history to julia script?

**URL:** <https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399>\
**Category:** General Usage\
**Tags:** question, repl\
**Created:** [April 16, 2021, 10:05am UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399 "2021-04-16T10:05:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 10:05am UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/1 "2021-04-16T10:05:02Z")

</div>

Is it possible to save the REPL history as a valid julia script?

Something like:

```julia
> a = 3
3
> b = 5
5
> a + b 
8
> REPL.save("myscript.jl")

```

myscript.jl:

```julia
# REPL history export
# Date: 16.04.2021
a = 3
# 3
b = 5
# 5
a + b 
# 8

```

something like this is possible in [iPython](https://gist.github.com/awesomebytes/4972b76459822845cc71)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 16, 2021, 10:42am UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/2 "2021-04-16T10:42:49Z")

</div>

Isn’t the history file (`.julia/logs/repl_history.jl`) _already_ a valid julia script?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 16, 2021, 12:06pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/3 "2021-04-16T12:06:32Z")

</div>

It is, but doesn’t contain any outputs.

---

<div class="post-metadata">

**Author:** ![OmarElrefaei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omarelrefaei/32/43002_2.png) [@OmarElrefaei](https://discourse.julialang.org/u/OmarElrefaei)\
**Post date:** [April 16, 2021, 10:23pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/4 "2021-04-16T22:23:00Z")

</div>

Well, if it is not doable from the default repl if OhMyRepl.jl, you can probably just use the _IJulia_ kernel inside iPython.

Edit: yup found it in IJulia docs. `ipython3 console --kernel julia-1.5`

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 24, 2022, 4:56pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/5 "2022-07-24T16:56:10Z")

</div>

In the live stream today another option was presented.

```julia
using DoctorDocstrings
pickandcopy()

```

It can pick lines from the REPL history file and copies them to the clipboard.

> <https://github.com/miguelraz/DoctorDocstrings.jl/blob/3f340243c6adf6316220256f46c7cdd6b799140f/src/DoctorDocstrings.jl#L166>

---

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [July 24, 2022, 5:55pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/6 "2022-07-24T17:55:37Z")

</div>

Thank you. I was in that session and found it very informative. Do you know if there a way to make pickandcopy() create the menu with only the REPL history strings from the current Julia session?

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 24, 2022, 6:13pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/7 "2022-07-24T18:13:55Z")

</div>

I think if we use the history file it is not possible at the moment, as there is no distinction between the sessions. If, however, there is a way to access the current session history somehow it should be easy.

I am not sure if there is an internal history storage/cache in the REPL somewhere.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 24, 2022, 6:43pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/8 "2022-07-24T18:43:49Z")

</div>

I found `Base.active_repl.interface.modes[1].hist.history`, which provides the REPL history.  
It is initialized with the history file, but after those entries, there is no crosstalk between sessions.

In addition, there is `start_idx`, which marks the first new entry.  
So here is a solution that looks good to me.

```julia
"""
Get commands of the current REPL session as an array of strings
"""
function get_REPL_as_history()
          history = Base.active_repl.interface.modes[1].hist
          history.history[history.start_idx+1:end]
end
"""
Save REPL commands of this session to a file

file: Path to a file
"""
function save_REPL_history(file)
           isfile(file) && error("file already exists")
           open(file,"w") do io
                      txt = join(get_REPL_as_history(),"\n")
                      write(io,txt)
           end
end

```

One thing that does **not work** is tracking repeated commands.  
So when you enter `print("123")` and then another `print("123")` the history only tracks one.

> **REPL example**
>
> ```julia
> julia> """
> Get commands of the current REPL session as an array of strings
> """
> function get_REPL_as_history()
> history = Base.active_repl.interface.modes[1].hist
> history.history[history.start_idx+1:end]
> end
> get_REPL_as_history
> 
> julia> """
> Save REPL commands of this session to a file
> 
> file: Path to a file
> """
> function save_REPL_history(file)
> isfile(file) && error("file already exists")
> open(file,"w") do io
> txt = join(get_REPL_as_history(),"\n")
> write(io,txt)
> end
> end
> save_REPL_history
> 
> julia> print("123")
> 123
> julia> print("321")
> 321
> julia> print("123")
> 123
> julia> print("123")
> 123
> julia> print("bye")
> bye
> julia> get_REPL_as_history()
> 8-element Vector{String}:
> "\"\"\"\nGet commands of the current" ⋯ 155 bytes ⋯ "ory[history.start_idx:end]\nend"
> "\"\"\"\nSave REPL commands of this " ⋯ 250 bytes ⋯ "ite(io,txt)\n end\nend"
> "print(\"123\")"
> "print(\"321\")"
> "print(\"123\")"
> "print(\"bye\")"
> "get_REPL_as_history()"
> julia> save_REPL_history("/tmp/record.jl")
> 660
> 
> ```

---

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [July 25, 2022, 7:17pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/9 "2022-07-25T19:17:40Z")

</div>

That’s awesome. Thank you. The ‘next step’ could be to extend that ‘current Julia session’ functionality you just described (plus the ‘saving to a file’ functionality) to pickandcopy(). This would be valuable since pickandcopy() neatly keeps track of what mode one was in the REPL (julian, shell, help, package), which is useful for cleanly documenting what transpired inside a ‘demo’ coding session in the REPL.

But your answer definitely works as stated, so Thanks Again!

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [July 25, 2022, 7:34pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/10 "2022-07-25T19:34:46Z")

</div>

The modes are also present in the history cache. I just did not use them.  
There is `Base.active_repl.interface.modes[1].hist.modes` containing the mode for each history entry and there is `Base.active_repl.interface.modes[1].hist.mode_mapping` containing the different promts.

Each prompt has then a field called `promt` which contains the start of the line.

```julia
julia> print("123")
123

julia> hist = Base.active_repl.interface.modes[1].hist;

julia> hist.history[end-2:end]
3-element Vector{String}:
 "print(\"123\")"
 "hist = Base.active_repl.interface.modes[1].hist;"
 "hist.history[end-2:end]"

julia> hist.modes[end-3:end]
4-element Vector{Symbol}:
 :julia
 :julia
 :julia
 :julia

julia> hist.mode_mapping[hist.modes[end]]
"Prompt(\"julia> \",...)"

julia> hist.mode_mapping[hist.modes[end]].prompt
"julia> "

```

---

<div class="post-metadata">

**Author:** ![mertbozkir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mertbozkir/32/32357_2.png) [@mertbozkir](https://discourse.julialang.org/u/mertbozkir)\
**Post date:** [January 19, 2023, 10:12pm UTC](https://discourse.julialang.org/t/export-from-repl-history-to-julia-script/59399/11 "2023-01-19T22:12:23Z")

</div>

I tried it, and it worked! Thanks for the solution. ✨
