# Is there an easy way to get at the history of REPL input?

**URL:** <https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025>\
**Category:** General Usage\
**Tags:** repl\
**Created:** [November 26, 2018, 6:28pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025 "2018-11-26T18:28:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 26, 2018, 6:28pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/1 "2018-11-26T18:28:48Z")

</div>

How does one get easily at the history of the input provided at the prompt?  
The \*nix shell has a function `history` which spits out numbered list of commands.  
Is there something like this in Julia?

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 26, 2018, 6:57pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/2 "2018-11-26T18:57:28Z")

</div>

Do you know this?

```julia
$ less ~/.julia_history

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 26, 2018, 7:00pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/3 "2018-11-26T19:00:32Z")

</div>

> [@Liso](#):
>
> less ~/.julia\_history

Thanks for the suggestion. However:

/home/pkrysl/.julia\_history: No such file or directory

Is it perhaps in a different folder based on the version?

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 26, 2018, 7:02pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/4 "2018-11-26T19:02:28Z")

</div>

I have also this:

```julia
less ~/.julia/logs/repl_history.jl

```

It is interesting julia\_history’s last command is (edit:) ~~one day~~ one month older than repl\_history’s…

So maybe it is actual one?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 26, 2018, 7:04pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/5 "2018-11-26T19:04:23Z")

</div>

The location of the history file moved in 0.7, thus `~/.julia_history.jl` is for versions 0.6 and prior, whereas `~/.julia/logs/repl_history.jl` is for 0.7+. Apparently you last used version 0.6 a month ago.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 26, 2018, 7:50pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/6 "2018-11-26T19:50:22Z")

</div>

I think this will serve nicely: `REPL.find_hist_file()`. Then all that is required is a little macro (or function) to spit out the contents of the history file… Perhaps like this, except a lot more succinct:

```julia
InteractiveUtils.less(REPL.find_hist_file())

```

Maybe this?

```julia
macro history() InteractiveUtils.less(REPL.find_hist_file()); end

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 27, 2018, 9:26am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/7 "2018-11-27T09:26:18Z")

</div>

I again tried to do something easy with Julia and it seems I am not very compatible with this language yet. ☹

This is just ugly (but it seems to work).

For examle I was impatient and used PyCall to implement `findall` function so you need to have `PyCall` installed.

I **did not test it thoroughly** so take it just as … (inspiration? I am not sure 😛 ) !!

````julia
julia> macro history2(a)
       run(pipeline(```$(Sys.BINDIR)/julia -e """import PyCall
         re = PyCall.pyimport(\"re\")
         open(\"$(REPL.find_hist_file())\") do f
          data = read(f)
          aa = re[:findall]( \"(^\\\\# time.*?)(?=# time)\", String(data), re[:S] | re[:M])
          for i in aa 
            if match(Regex(\"$(a)\"), 
               i[findfirst(r\".*?\n.*?\n\", i).stop+1:end] # ignore first two lines
               ) != nothing
               println(i)
            end
          end 
         end"""```, `less -`));
       end

````

You could try

```julia
julia> @history2 som.thing; # this regex work

julia> @history2 som.*thing; # this regex doesn't work! 

julia> @history2 "som.*thing"; # this regex work

julia> @history2 "(?i)pager"; # case insensitive example

```

Another warning: I used `less -` instead of something like `InteractiveUtils.shell_split(get(ENV, "PAGER", "less"))` because I am not sure if other pagers could work with output redirection and I don’t know how to nicely check this functionality…

It would be better to split it into two functionalities 1-searcher for “elements” 2-macro for pushing string (vector of strings) into “less”. But I spent too much time to this “self-homework”…

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [November 27, 2018, 11:02am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/8 "2018-11-27T11:02:19Z")

</div>

I tried a simpler way of searching the history file.

```julia
open(raw"C:\\Users\chatura\\.julia\\logs\\repl_history.jl") do file
    for ln in eachline(file)
# if endswith(ln, "jl")==true
        if occursin(r"(CSV)", ln) ==true   
            println("$(length(ln)), $(ln)")
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 27, 2018, 11:36am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/9 "2018-11-27T11:36:59Z")

</div>

> [@How can I view the whole command history?](https://discourse.julialang.org/t/how-can-i-view-the-whole-command-history/17987/8):
>
> I don’t—that’s how I view it. I also don’t use an IDE.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 27, 2018, 11:50am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/10 "2018-11-27T11:50:50Z")

</div>

> [@PetrKryslUCSD](#):
>
> all that is required is a little macro (or function) to spit out the contents of the history file…

Wouldn’t opening in the editor, eg with

```julia
edit(REPL.find_hist_file())

```

be just as convenient?

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 27, 2018, 5:05pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/11 "2018-11-27T17:05:14Z")

</div>

But the user needs to find the way to properly write all that sentence or remember it.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 27, 2018, 5:07pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/12 "2018-11-27T17:07:26Z")

</div>

I have that set up as a key binding in Sublime Text (and a voice command). Similar device would work in VS Code and elsewhere.

---

<div class="post-metadata">

**Author:** ![mortonjt](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mortonjt](https://discourse.julialang.org/u/mortonjt)\
**Post date:** [August 12, 2019, 7:55pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/13 "2019-08-12T19:55:08Z")

</div>

@Liso, it is reassuring that all of the history is found under `~/.julia/logs/repl_history.jl`

But having an easy command for getting the history in a current session would still be highly desirable (see the [%history](https://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.history.html) command in ipython for an example).

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [August 12, 2019, 9:24pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/14 "2019-08-12T21:24:36Z")

</div>

Note that there is `IJulia.history` as a replacement for python’s `%history`. ([This was one of my very first Julia related PRs](https://github.com/JuliaLang/IJulia.jl/pull/500) ! 😃)

Also you might want to take a look at [https://github.com/crstnbr/SaveREPL.jl/blob/master/src/SaveREPL.jl](https://github.com/crstnbr/SaveREPL.jl/blob/master/src/SaveREPL.jl) which contains a bit of history access logic.

---

<div class="post-metadata">

**Author:** ![mortonjt](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mortonjt](https://discourse.julialang.org/u/mortonjt)\
**Post date:** [August 13, 2019, 2:10pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/15 "2019-08-13T14:10:55Z")

</div>

That is an awesome contribution!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 14, 2019, 6:11pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/16 "2019-08-14T18:11:38Z")

</div>

Another way of looking at the history. I would like to be able to just grab the output and paste it into a script or into the command line.

An example is here:

```julia
julia> include(".\\REPLHistory.jl");                       
                                                            
julia> a = rand(3)                                          
                                                            
3-element Array{Float64,1}:                                 
 0.7650591549676755                                         
 0.37080326489156024                                        
 0.2852039006568494                                         
                                                            
shell> pwd                                                  
                                                            
/c/Users/PetrKrysl/Documents/Work-in-progress/Experiments   
                                                            
julia> b = deepcopy(a)                                      
                                                            
3-element Array{Float64,1}:                                 
 0.7650591549676755                                         
 0.37080326489156024                                        
 0.2852039006568494                                         
                                                            
julia> b[1] = 0                                             
                                                            
0                                                           
                                                            
julia> @show b                                              
b = [0.0, 0.37080326489156024, 0.2852039006568494]          
3-element Array{Float64,1}:                                 
 0.0                                                        
 0.37080326489156024                                        
 0.2852039006568494                                         
                                                            
julia> @history 7                                           
a = rand(3)                                                 
;pwd                                                        
b = deepcopy(a)                                             
b[1] = 0                                                    
@show b                                                     
                                                            
julia> 

```

This was produced with the following little module:

```julia
module REPLHistory

using InteractiveUtils
using REPL

export @history

macro history(n) 
	n = n - 1
	list = []; 
	for l in reverse(readlines(REPL.find_hist_file()))[2:end]
		l = strip(l)
		if n != 0
			(l[1] != '#') && (n = n - 1)
			push!(list, l); 
		else
			break
		end
	end
	next = ""
	for l in reverse(list)
		l = next * l
		if l == "# mode: shell"
			next = ";" 
		else
			printit = false
			if (l[1] != '#')
				if (length(l) >= 8) 
					if (l[1:8] != "@history")
						printit = true
					end
				else
					printit = true
				end
			end
			printit && println(l)
			next = "" 
		end
	end
end

end

using .REPLHistory

```

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [August 22, 2020, 6:41am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/17 "2020-08-22T06:41:35Z")

</div>

Is it ok to use

```julia
Base.active_repl.mistate.current_mode.hist.history

```

to get the history?

---

<div class="post-metadata">

**Author:** ![dcastel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcastel/32/10692_2.png) [@dcastel](https://discourse.julialang.org/u/dcastel)\
**Post date:** [September 17, 2020, 9:21am UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/18 "2020-09-17T09:21:09Z")

</div>

Well, that’s cool!

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [February 6, 2021, 5:13pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/19 "2021-02-06T17:13:14Z")

</div>

That’s awesome; I wonder if a couple more things are possible:

- distinguishing commands in the different modes (normal, pkg, shell)
- separating out commands from different REPL sessions (the command you provided interleaves them)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 6, 2021, 9:34pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025/20 "2021-02-06T21:34:35Z")

</div>

[PetrKryslUCSD/REPLHistory.jl: Gain access to the Julia REPL history. (github.com)](https://github.com/PetrKryslUCSD/REPLHistory.jl)

```julia
$ ~/AppData/Local/Programs/Julia-1.5.3/bin/julia.exe
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.5.3 (2020-11-09)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using REPLHistory

shell> ls
'3D Objects'
 AppData
'Application Data'
 Contacts
 Cookies
 Documents
 Downloads
 Favorites
...

(@v1.5) pkg> status
Status `C:\Users\pkonl\.julia\environments\v1.5\Project.toml`
  [a5566ce3] REPLHistory v0.1.0

julia> pwd()
"C:\\Users\\pkonl"

julia> print(history())
using REPLHistory
;ls
]status
pwd()

julia>

```

I wish I could have more confidence in `Base.active_repl.mistate.current_mode.hist.history`, but it looks alarmingly “internal”.

[Next page](https://discourse.julialang.org/t/is-there-an-easy-way-to-get-at-the-history-of-repl-input/18025.md?page=2)
