Skip to content

History functionality in the Julia REPL similar to what bash provides

License

Notifications You must be signed in to change notification settings

GHTaarn/History.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

History.jl

A Julia package for getting REPL history functionality similar to what Bash provides.

Installation

In the Julia REPL type:

] add https://github.org/GHTaarn/History.jl

(If you are using an unstable version of Julia, do this instead)

Use

In the Julia REPL type:

using History

Hereafter, typing an '!' character at the beginning of a line in the Julia REPL will activate History mode.

In History mode, a line starting with a '/' character will return the history entries whose text contains the string on the rest of the line (using '/' was inspired by less and vi). A line starting with a '^' character will return the history entries whose text starts with the string on the rest of the line (using '^' was inspired by regular expressions).

In History mode, a line consisting of only an '!' character will print out exactly enough recent REPL history to fill your screen. Any '!' characters immediately followed by a positive integer will substitute the text from the corresponding historic input line into the rest of the current line immediately before the line is executed. A negative integer after an '!' character will be substituted with the text of the input line the given number of lines ago.

Tab completion: If the current input line contains an '!' character followed by an integer before the cursor, then the above substitution will be performed. In all other cases, normal julia mode tab completion will be performed. In tab completion, !0 can be used to refer to the current input line as it looks when tab is pressed.

Examples

The following examples are performed without tab completion, but using tab completion will often avoid a lot of confusion and be more useful.

julia> using History
REPL mode history initialized. Press ! to enter and backspace to exit.

History> sin(8)
0.9893582466233818

History> !-1
0.9893582466233818

History> println("!-2")
sin(8)

History> println("!-2 ")
!-1 

History> true
true

History> !!-1
false

History> println("!-1 and !-2")
!!-1 and true

History> 
History> !
10×3 Matrix{Any}:
 4315  :julia    "exit()"
 4316  :julia    "using History"
 4317  :history  "sin(8)"
 4318  :history  "!-1"
 4319  :history  "println(\"!-2\")"
 4320  :history  "println(\"!-2 \")"
 4321  :history  "true"
 4322  :history  "!!-1"
 4323  :history  "println(\"!-1 and !-2\")"
 4324  :history  "!"

History> !!4321 ? !4317 : 2*!4317
1.9787164932467636

History> ^!
3×3 Matrix{Any}:
 4318  :history  "!-1"
 4322  :history  "!!-1"
 4324  :history  "!"
 4325  :history  "!!4321 ? !4317 : 2*!4317"

History> /tln
4×3 Matrix{Any}:
 4319  :history  "println(\"!-2\")"
 4320  :history  "println(\"!-2 \")"
 4323  :history  "println(\"!-1 and !-2\")"
 4327  :history  "/tln"

History> 

Exported functions

There are 4 exported functions: history, histexec histsearch and histsave. You can read more about these in their docstrings by typing the following in the Julia mode REPL:

?history
?histexec
?histsearch
?histsave

Activation at startup

From a shell script or on the OS shell commandline:

julia -i -e 'atreplinit(x->eval(Meta.parse("using History")))'

OR

In ~/.julia/config/startup.jl:

atreplinit() do repl
    @eval using History
end

Other usage tips

The History package is designed to be used in combination with Julias existing history capabilities such as Ctrl-R, Ctrl-S, Up arrow and Down arrow (documented here).

The TerminalPager package can be a useful companion to History.jls exported functions. TerminalPager also has a little known commandline mode that can be entered with a | as the first key press from the Julia mode commandline.

The OhMyREPL package also has some history functionality and seems to work alongside History.jl provided that OhMyREPL is loaded before History.jl, e.g.:

using OhMyREPL
using History

Be aware that the numbered prompts introduced in Julia 1.9 use a different numbering convention than History.jl does.

Unsupported Julia versions

Because this package makes use of undocumented features in Julia, compatibility has been restricted to stable Julia 1.x releases. Use of this package with unstable Julia releases is not recommended and not supported, but is often possible if it is installed with Pkg.add(url="https://github.com/GHTaarn/History.jl", rev="nocompat").

Known bugs

  1. Commands entered in History mode produce errors when Revise needs to recompile
  2. In History mode, incomplete lines produce a stack trace instead of a line change when the Return key is pressed

For the first bug, if possible and acceptable, the workaround is to tab complete the line and then exit History mode (with the Home key followed by Backspace) just before the Return key is pressed. Alternatively, a reload must be triggered before the History mode command is executed e.g. by prepending the command with Revise.retry();.

For the second bug, a workaround is to use the Meta+Return key combination instead of only Return. Exiting History mode as above is also an option and if necessary, History mode can be reentered (by pressing the Home key followed by the ! key) subsequently.

Feedback

Firstly, if you like this package and find it useful, please give it a star on the Github page. If you find any bugs, please report them here or submit a pull request. Before doing this, please read the CONTRIBUTING.md file.

If what you want to write is neither an issue nor a pull request, feel free to use the discussions page.

About

History functionality in the Julia REPL similar to what bash provides

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages