Tips for the Julia startup.jl file

Some small glints and tips for the startup.jl that I find useful when using Julia interactively in the REPL.

If you spend your day hours switching between an editor, paper and the Julia REPL you surely know about the startup.jl file. Most people use it for loading packages that they use often enough that they value of having the package functionality available at startup is higher than the price of the REPL startuptime. The library packages that meet this criterium for me are BenchmarkTools and Makie. It is always a good idea when loading package for interactive use to include them conditionally only when the session is interactive by

try
    if isinteractive()
        using Makie
    end
catch e
    @warn "Error initializing Makie" exception = (e, catch_backtrace())
end

This way you are only paying the expense when you actually need it and not when running scripts or generating documentation for example.

Non-library packages that may improve your experience with the Julia REPL that I use are

It is also useful to define some functions that you can use for exploration and debugging purposes. For example, I have a subtypetree function which allows me to see all of the subtypes of a particular abstract type to decide on what interfaces I should use for my own implementations.

It works like this

julia> subtypetree(Integer)
Integer
  Bool
  GeometryBasics.OffsetInteger
  Signed
    BigInt
    Int128
    Int16
    Int32
    Int64
    Int8
  Unsigned
    UInt128
    UInt16
    UInt32
    UInt64
    UInt8
Definition of subtypetree
function subtypetree(roottype, level=1, indent=2)
    level == 1 && println(roottype)
    for s in subtypes(roottype)
        println(join(fill(" ", level * indent)) * string(s))
        subtypetree(s, level + 1, indent)
    end
end

Also you might want to integrate some tools that you use in your system and use them instead of the defaults. One nice enhancement that I have in this category is using bat instead of less for the default in the InteractiveUtils.less function. You can achieve this by including the code

try
    if isinteractive()
        using InteractiveUtils
        function InteractiveUtils.less(file::AbstractString, line::Integer)
            run(`bat --paging=always --line-range $(line): $file`)
            nothing
        end
    end
catch e
    @warn "Error initializing InteractiveUtils" exception = (e, catch_backtrace())
end

in your startup.jl file. This is a game changer for me allowing to switch context between reading code that is run and running the code while staying in the REPL using @less instead of @edit which opens the code in the terminal. This is before less using less as a $PAGER by default on a GNU/Unix system and this is after bat using bat.

I hope you enjoyed this non-exhaustive view into my startup.jl and that you found something useful that could make your experience a little less frustrating… See you in the next post!

Go to top File an issue