My first experiences with NuShell
Encouraged by some tweets about NuShell I decided to give it a try. The project is still in MVP phase, but what I encountered makes my really eager for the full-blown ready-to-fight version.
I downloaded and installed Windows version using .msi file; I use Windows Terminal and it was automatically added to my profile file after installation. It can be done also manually as described here.
Next I wanted to have it also in WSL, Ubuntu 20.04 release. In my case I used Homebrew package + set it as a default shell.
brew install nushell
which nu | sudo tee -a /etc/shells
chsh -s "$(which nu)"
The next launch of my WSL ditro started the initialization of the new shell.
Regarding the initialization - NuShell uses two files: first it loads env.nu
, then
config.nu
. The former is used to add environment variables which are used in
the latter file, which keeps global definitions, aliases etc.
The initialization text also says which entry in the config is responsible for
showing the welcome text when NuShell is opened. I disabled it with:
config nu
then added to the file:
let-env config = {
show_banner: false
}
Configuration is stored in the config
environment variable. Environment variables can be accessed
with $env
, so for the config the syntax is just $env.config
.
OK, having this done let's navigate the filesystem. NuShell has well known set of commands
like cd
, ls
, mkdir
, mv
etc. BTW I like command hints when you press the tab key - the list can
be navigated using arrow keys.
NuShell also displays hints based on your previous input, so by pressing the right arrow key you can use its proposition.
Let's add an alias for my very frequently used action - changing the current directory to the root of my workspace.
alias cdw = cd c:\workspace
To have it persisted, it should be added to the config file config.nu
.
The whole magic starts with output of internal commands - they are not always string, but might have a structured, NuShell's data type like list, record or table. Which means the presentation is better BUT not only - it allows to work with the data in more interactive way.
OK, this is an internal command, let's see what it can do docker ps -a
.
This command allows to pass Go template style formatting,
so we can have a custom output just by using its built-in feature:
Now let's use NuShell on top of this functionality to get a nice looking list, with running containers at the beginning, sorted by name; then stopped containers, also sorted by name:
The commands is:
docker ps -a --format "{{.ID}}|{{.Names}}|{{.State}}|{{.Ports}}" | lines |
split column "|" ID Name State Ports | into df | sort-by [State Name] -r [true, false]
I stored this under dspa
alias, which means I can use it with the another command, for example
to get just Name
column:
The error handling is really human-friendly:
NuShell also has built-in support for many file types - you can access the required information much easier
with open
and get
commands.
Accessing package.json devDependencies:
Opening k8s yaml file. I see there is a service and a deployment inside...
...so getting the number of replicas of the deployment + saving it as json file will be:
We can just print the whole file content with open --raw <file>
(and if you are used to "cat ..." then
set is an alias).
Next stop was to set Oh-My-Posh. In Windows I run oh-my-posh init nu
, then
config nu
and added the line
source ~/.oh-my-posh.nu
I already have a nerd font installed, if not then you should grab one.
To set your own file you can use again the config + env variable, but I just updated the ~/.oh-my-posh.nu
file and set
the path to my omp.json. The last step was to set the font section for NuShell in Windows Terminal settings.json :
Oh-My-Posh applied:
It is worth to mention that NuShell has its own ways for tuning the prompt and changing colors.
In the examples above there was a lot of pipeline operator usage, but this shell is not Bash and delivers its own programming language with immutable variables and scoped environment.
With just few hours spent on exploring NuShell features, I really love what I seen and I am digging into the docs to learn more and fully leverage its features to work with structured output and built-in file handling possibilities.
Welcome, my new Shell! 🤗🐚