Nushell Snippets
# Get helphelphelp <command, alias module>help --find <search text>help operatorshelp escapes
# Change directorycd mydirectory# Go back one directorycd -# Change to parentcd .. # or ..# Go up two levelscd ... # or ...
# Move filemv
# Remove filerm
# Copy filecp
# List filesls
# Run a pipeline of commandsnu -c <commands># Run scriptnu <script>
# List all markdown files with asterisk (*) glob, also called wildcardls *.md# List non-hidden files under current directoryls **/*# Other glob syntax is supported# List all files including hidden files (-al) and show sizes of files/folders (-d)ls -ald
# List files, sort, filter, query first itemls| where size > 10kb| sort-by size| first
# Print outputprint "Hello World"# Print is not the same as echo "Hello World" as# echo just returns the value
# Read a file as structured data and outputopen <file>
# Display contents of fileopen --raw <file>
# Read a csv file and output specific columnsopen data.csv | select "First Name" column2 column3 column4
# Read a csv file, find a specific row and output specific columnsopen contacts.csv | where "First Name" =~ "John" | select "First Name" "Last Name" "E-mail Address" "Mobile Phone"
# Transpose table display - useful for small amount of rows, but many columnslet name = 'John'; open contacts.csv | where "First Name" =~ $name | select "First Name" "Last Name" "E-mail Address" "Mobile Phone" "Birthday" | transpose
# Read csv data, filter and only return non-empty items in Phone fieldopen contacts.csv | where "First Name" =~ "John" | compact Phone
# Output default config.nu and save to fileconfig nu --default | save config.nu# Open config.nu in editorconfig nu
# Output default env.nu and save to fileconfig env --default | save env.nu# Open env.nu in editorconfig env
# Edit nushell env.nuvim $nu.env-path# Edit nushell config.nuvim $nu.config-path
# Open File, replace string and save it againopen ~/.zoxide.nu | str replace --all 'def-env' 'def --env' | save -f ~/.zoxide-fixed.nu# Replace strings, can take in file or textopen file.txt | str replace --all 'hello' 'hi''hello world' | str replace 'hello' 'hi'
# See processes with table pager explore
# Describe output of a command or expressionps | describe
## Explore use Vim key bindingsps | explore# See first 10 processesps | first 10# Find a process that contains a name "gnome" and sort by cpups | where name =~ 'gnome' | sort-by cpu# Kill a processps | where name =~ 'emacs' | first | kill $in.pid
# See environment$env | table## Environment using table pager$env | explore# Display current directorypwd$env.PWD
# Detect columns in output and output in tablepodman top mycontainer | detect columns
# Convert to other format like csv, md, json, yaml, html, textcat mytable.md | detect columns | to csv
# Find a program file, alias, or commandwhich <command>
# Set aliasalias s = git status -sb
# Sort in increasing ordersort# Sort in decreasing ordersort -r# -r or --reverse
## See yaml file and explore with page / table datakubectl get deployment <deployment-name> -o yaml | from yaml | explore
# Set environment variable## Spaces around = sign are required$env.YAZI_FILE_ONE = '~\scoop\apps\git\current\usr\bin\file.exe'$env.NVIM_APPNAME = 'lazyvim'## Extend path$env.Path = ($env.Path | prepend 'C:\path\you\want\to\add')
# Kill first process with a given nameps | where name =~ 'emacs' | first | kill -f $in.pid
# Kill process with process IDkill <pid>
# Filter output on stringgfold | lines | where {str contains "clean"}
# Filter output as json on a columngfold -d json | from json | where status =~ 'Unclean'gfold -d json | from json | where status =~ 'Unclean|Unpushed'
# Create a new column using upsert with computed values of other columnsgfold -d json | from json | where status =~ 'Unclean|Unpushed' | upsert path {|row| $row.parent + '/' + $row.name}
# Open specific Excel spreadsheetopen --raw sheet1.xlsx | from xlsx --sheets [Sheet1]# Open sheet "Sheet1" in binary xlsx fileopen sheet1.xlsx | get "Sheet1"# Open sheet "Sheet1" in binary xlsx file and search first columnopen sheet1.xlsx | get "Sheet1" | where column0 =~ myfilter"
# http command to get, fetch and other commandshttp get https://google.com# Can returned structured data for further use# Similar to curl, jq tools combined
# Use output of command fzf with another commandvim (fzf)
# Redirect both stdout and stderr to the same file with out+err>cat unknown.txt out+err> log.log
# Complete: Gather stdout, stderr, and exit code together in one recordcat unknown.txt | complete
# View system informationsys host# Other possible values are:# cpu - View information about the system CPUs.# disks - View information about the system disks.# host - View information about the system host, operating system# mem - View information about the system memory.# net - View information about the system network interfaces.# temp - View the temperatures of system components.# users - View information about the users on the system.
# Get user inputlet var = input# Get secret inputlet secret = input -s
# String to Integer conversion"12" | into int
# Date to a Time Zonedate now | date to-timezone "America/Toronto"
# Convert data, strings to yaml, other ouputs supported, see help for to[one two three] | to yaml
Variables
Section titled “Variables”# Set immutable variablelet name = 'John'
# Get variable$msg$"greeting, ($name)"
# Previous commmand's output, can be used similar to xargs in bash$in
# Copy largest file to user's home directory using $inls | sort-by size | first | get name | cp $in ~
Operators
Section titled “Operators”# Get helphelp operators
# Greater than operator4 > 10# => false
Scoping
Section titled “Scoping”Nushell blocks control their own environment. Changes to environment like working directories or environment variables will not persist outside a block.
ls | each { |row| # Change to PWD will not persist outside this block # ending in the bracket cd $row.name make}