Standard Library
Nushell ships with a standard library of useful commands written in native Nu. By default, the standard library is loaded into memory (but not automatically imported) when Nushell starts. The exception is the small std/prelude module, which is imported automatically and provides the banner and pwd commands.
Overview
The standard library is split into modules, and you import only the ones you need, as described below. Each module has its own page:
| Module | Recommended import | Contents |
|---|---|---|
std/assert | use std/assert | assert and its subcommands, for tests |
std/bench | use std/bench | bench, which times closures |
std/clip | use std/clip | clip copy52, clip paste52 and clip prefix, for the clipboard through the terminal |
std/config | use std/config | config dark-theme, config light-theme and config env-conversions |
std/dirs | use std/dirs | dirs and its subcommands, the directory stack |
std/dt | use std/dt * | datetime-diff and pretty-print-duration, for calendar differences between dates |
std/formats | use std/formats * | from and to commands for JSONL, NDJSON and NDNUON |
std/help | use std/help | another version of help and its subcommands |
std/input | use std/input | input display, which shows the key presses Nushell receives |
std/iter | use std/iter | iter find, iter scan and other commands for working through lists |
std/log | use std/log | log info, log warning and the other logging commands |
std/math | use std/math | the constants E, PI, TAU, PHI and GAMMA |
std/random | use std/random | random dice |
std/testing | use std/testing * | the @test, @ignore, @before-each, @before-all, @after-each and @after-all attributes |
std/util | use std/util * | path add, repeat, null-device, structure, ellie and the null_device constant |
std/xml | use std/xml * | xaccess, xupdate, xinsert and xtype, for XML data |
Nushell also ships std-rfc, a library of candidate commands for the standard library. See The Standard Library Candidate Module.
To see a complete list of the commands available in the standard library, run the following:
nu -c "
use std
scope commands
| where name =~ '^std '
| select name description extra_description
| wrap 'Standard Library Commands'
| table -e
"Note
The use std command above loads the entire standard library so that you can see all of the commands at once. This is typically not how it will be used (more info below). It is also run in a separate Nu subshell simply so that it is not loaded into scope in the shell you are using.
The list leaves a few things out:
- The
std/clipcommands, whichuse stddoesn't import. - The
std/testingattributes. They are aliases, so they appear inscope aliasesinstead, asstd testing attr testand so on. - The
shells-aliasesofstd/dirs, which onlyuse std/dirs shells-aliases *imports. - The
std/mathconstants andnull_device, which are values. Afteruse std, they are in the$stdrecord.
The prelude
Nushell imports std/prelude when it starts, unless you start it with --no-std-lib. The prelude has two commands.
pwd returns the current directory, $env.PWD. With --physical (-P), it resolves symbolic links first. On macOS, where /tmp is a link to /private/tmp:
cd /tmp
pwd
# => /tmp
pwd --physical
# => /private/tmpbanner returns the welcome message that Nushell shows at startup: Ellie, the version, some links, how long ago Nushell's first commit was, and the startup time. --short returns only the Startup Time line, and --no-startup-time returns everything except that line. Three keys of $env.config.color_config set its colors: banner_foreground for the text, which defaults to attr_normal, and banner_highlight1 and banner_highlight2 for the highlighted words, which default to green and purple. When config use-colors returns false, banner strips all ANSI codes from its text.
$env.config.show_banner decides what the REPL shows when it starts:
| Value | Shows |
|---|---|
true or "full" | the whole banner (the default) |
"short" | only the startup time line, banner --short |
false or "none" | nothing |
The REPL prints the welcome part before it runs the startup hooks, and the startup time line just before the first prompt. The time it shows covers everything up to that prompt. See Remove Welcome Message.
Importing the Standard Library
The Standard Library modules and submodules are imported with the use command, just as any other module. See Using Modules for more information.
Importing the whole library
While working at the commandline, it can be convenient to load the entire standard library using:
use std *However, this form should be avoided in custom commands and scripts since it has the longest load time.
Optimal Startup when Using the Standard Library
See the notes below on how to ensure that your configuration isn't loading the entire Standard Library.
use std and use std * both import every module except std/clip, which you have to import on its own. Both also run the export-env blocks of std/log and std/dirs, which set up the log formats and the directory stack. They differ in how the commands are named:
use stdputs everything under thestdprefix, as instd log info,std dirs addandstd help. Thestd/utilcommands lose their module's name. They arestd path add,std repeat,std null-device,std structureandstd ellie, notstd util path add. The math constants andnull_deviceare in a$stdrecord, as$std.math.PIand$std.null_device.use std *imports each module under its own name, as inlog info,dirs addandassert equal, and thestd/utilcommands without any prefix, such aspath add. The constants are$math.PIand$null_device.
use std * also brings in the help command of std/help, which replaces the built-in help and the page that the --help flag of every command shows. use std doesn't change help, because its version is named std help.
Importing Submodules
Each submodule of the standard library can be loaded separately. Again, for best performance, load only the submodule(s) that you need in your code.
See Importing Modules for general information on using modules. The table above shows the recommended import for each of the Standard Library submodules. It uses one of the first two forms below.
1. Submodules with <command> <subcommand> form
These submodules are normally imported with use std/<submodule> (without a glob/*): std/assert, std/bench, std/clip, std/config, std/dirs, std/help, std/input, std/iter, std/log and std/random. Their commands keep the submodule's name, as in log warning <msg>. std/math also works this way. After use std/math, its constants are $math.E and so on.
2. Import the definitions (contents) of the module directly
Some submodules are easier to use when their definitions (commands, aliases, constants, etc.) are loaded into the current scope. For instance:
use std/formats *
ls | to jsonlThe submodules that are normally imported with use std/<submodule> * (with a glob/*) are std/dt, std/formats, std/testing, std/util and std/xml. The glob matters most for std/testing, whose @test and other attributes only work without a prefix, and for std/formats, whose commands open and save find only under their plain names. std/util also exports a constant, null_device, for places where Nushell needs a value while it parses your code. use std/math * imports the math constants without a prefix, such as $E, though the prefixed form #1 above is usually easier to read.
3. use std <submodule>
It is possible to import Standard Library submodules using a space-separated form:
use std formats *Important
As mentioned in Using Modules, this form (like use std *) first loads the entire Standard Library into scope and then imports the submodules. In contrast, the slash-separated versions in #1 and #2 above only import the submodule and will be much faster as a result.
This form also doesn't work for submodules that have a command of the same name. For example, use std log imports only the log command itself, not log info or the other subcommands. Use use std/log instead.
Logging with std/log
The logging commands, such as log info and log warning, are described in std/log, along with log levels, the format of a log line and custom levels.
The Standard Library Candidate Module
std-rfc, found in the nushell Repository, serves as a staging ground for possible Standard Library additions.
std-rfc ships with Nushell, so its submodules can be imported the same way as the Standard Library's, for example use std-rfc/str. It currently includes the conversions, date, iter, kv, path, pb, random, str, tables, url, and xml submodules. Their commands are described in std-rfc, which also covers the universal variables of std-rfc/kv.
Import std-rfc/date and std-rfc/kv with a glob. Their commands already start with the module's name, so use std-rfc/date gives you date date floor, and use std-rfc/kv gives you kv kv set. Importing on the std-rfc page lists the recommended import for every submodule. std-rfc/kv exists only in a Nushell built with the sqlite feature, which the default build includes.
use std-rfc imports every submodule except std-rfc/date, under the std-rfc prefix. Like use std, it drops the name of some submodules. The commands of conversions, tables and path become std-rfc into list, std-rfc with-extension and so on, while the others keep it, as in std-rfc str dedent.
If you are interested in adding to the Standard Library, please submit your code via PR to the std-rfc module in that repository. We also encourage you to try these candidate commands and provide feedback on them.
More details
Candidate commands for the Standard Library should, in general:
- Have broad appeal - Be useful to a large number of users or use cases
- Be well-written and clearly commented for future maintainers
- Implement help comments with example usage
- Have a description that explains why you feel the command should be a part of the standard library. Think of this as an "advertisement" of sorts to convince people to try the command and provide feedback so that it can be promoted in the future.
In order for a command to be graduated from RFC to the Standard Library, it must have:
- Positive feedback
- Few (or no) outstanding issues and, of course, no significant issues
- A PR author for the
stdsubmission. This does not necessarily have to be the original author of the command. - Test cases as part of the
stdsubmission PR
Ultimately a member of the core team will decide when and if to merge the command into std based on these criteria.
Of course, if a candidate command in std-rfc no longer works or has too many issues, it may be removed from or disabled in std-rfc.
Disabling the Standard Library
To disable the standard library, you can start Nushell using:
nu --no-std-libThis can be especially useful to minimize overhead when running a command in a subshell using nu -c. With -c, $nu.startup-time shows how long Nushell took to start before running the command, so you can compare the two:
nu --no-std-lib -n -c "$nu.startup-time"
# => 9ms 650µs 250ns
nu -n -c "$nu.startup-time"
# => 11ms 558µs 83nsYou will not be able to import the library, any of its submodules, nor use any of its commands, when it is disabled in this way. std-rfc is disabled as well, so use std/log and use std-rfc/str both fail with Module not found.
The banner and pwd commands from std/prelude are gone too, so Nushell runs them as external commands. pwd runs a pwd program from your PATH, such as /bin/pwd. On macOS, banner runs /usr/bin/banner, which prints text in large letters. Where no such program exists, the command isn't found. The REPL also skips its startup banner, whatever show_banner says.
Using std/dirs in Modules
Two standard library modules set environment variables in an export-env block:
std/logsetsNU_LOG_FORMATandNU_LOG_DATE_FORMAT, unless they already have a value.std/dirscreatesDIRS_LISTandDIRS_POSITION, the directory stack, and adds anENV_CONVERSIONSentry for each of them.
If your own module imports one of them with a top-level use, that block doesn't run when your module is imported. See this caveat in the "Creating Modules" chapter for why.
std/log doesn't need the block. Its commands fall back to the default formats when the variables aren't set.
std/dirs does. Without the variables, dirs fails with Cannot find column 'DIRS_LIST'. Run the block from your module's own export-env, with use std/dirs [], which imports nothing but the environment:
# go.nu
use std/dirs
export-env {
use std/dirs []
}
export def --env stack [] {
dirs | length
}use go.nu
go stack
# => 1Optimal Startup
If Nushell's startup time is important to your workflow, review your startup configuration in config.nu, env.nu, and potentially others for inefficient use of the standard library. The following command should identify any problem areas:
view files
| enumerate | flatten
| where filename !~ '^std'
| where filename !~ '^repl_entry'
| where {|file|
(view span $file.start $file.end) =~ 'use\s+std(\s|;|$)'
}Edit those files to use the recommended syntax in the Importing Submodules section above.
Note
If a Nushell library (e.g., from the nu_scripts repository), example, or doc is still using this syntax, please report it via an issue or PR.
If a third-party module is using this syntax, please report it to the author/maintainers to update.