Skip to content

Instantly share code, notes, and snippets.

@edombowsky
Last active December 31, 2025 18:29
Show Gist options
  • Select an option

  • Save edombowsky/b54104771fad0571719594c274624e2d to your computer and use it in GitHub Desktop.

Select an option

Save edombowsky/b54104771fad0571719594c274624e2d to your computer and use it in GitHub Desktop.
# config.nu
#
# Installed by:
# version = "0.106.1"
#
# This file is used to override default Nushell settings, define
# (or import) custom commands, or run any other startup tasks.
# See https://www.nushell.sh/book/configuration.html
#
# Nushell sets "sensible defaults" for most configuration settings,
# so your `config.nu` only needs to override these defaults if desired.
#
# You can open this file in your default editor using:
# config nu
#
# You can also pretty-print and page through the documentation for configuration
# options using:
# config nu --doc | nu-highlight | less -R
$env.path ++= ["~/.local/bin"]
$env.path ++= ["~/.cargo/bin"]
$env.path ++= ["~/.config/emacs/bin"]
$env.config.buffer_editor = '/usr/bin/micro'
# reload Nushell's configuration files
def reconf [] {
source $nu.env-path
# source $nu.config-path
}
# This will run the given command and keep it running, even after the terminal or
# SSH connection is terminated. All output is ignored.
def bkr [cmd] {
job spawn { sleep 10sec; $cmd | save nohup.txt }
# (nohup $cmd &>/dev/null &)
}
# List files sorted by modification time in multiple directories
def lrt [
...directories: string # Variable number of directory paths
--reverse (-r) # Reverse sort order (newest first)
] {
let $directories = if ($directories | is-empty) { ['.'] } else { $directories }
$directories
| each {|dir|
let $resolved_dir = if ($dir | path exists) {
$dir | path expand
} else {
$dir
}
ls --all $resolved_dir
| select name modified size
| each {|file| $file | upsert dir $dir }
}
| flatten
| sort-by modified
| if $reverse { reverse } else { $in }
}
def la [
...directories: string # Variable number of directory paths
] {
let $directories = if ($directories | is-empty) { ['.'] } else { $directories }
$directories
| each {|dir|
let $resolved_dir = if ($dir | path exists) {
$dir | path expand
} else {
$dir
}
ls --all $resolved_dir
| each {|file| $file | upsert dir $dir }
}
| flatten
}
def ll [
...directories: string # Variable number of directory paths
] {
let $directories = if ($directories | is-empty) { ['.'] } else { $directories }
$directories
| each {|dir|
let $resolved_dir = if ($dir | path exists) {
$dir | path expand
} else {
$dir
}
ls -l $resolved_dir
| each {|file| $file | upsert dir $dir }
}
| flatten
}
# File sorting command extension with hidden files support and modified time
export def files-by-size [
...directories: string # Directories to search (default: current directory)
--reverse (-r) # Largest first
--human-readable (-h) # Human-readable sizes
--all (-a) # Include hidden files (starts with .)
--recursive (-R) # Search subdirectories
--depth: int # Max depth for recursive search
] {
let search_dirs = if ($directories | is-empty) { ['.'] } else { $directories }
let files = $search_dirs
| each { |dir|
let abs_path = if $dir == '.' { $env.PWD } else { $dir | path expand }
if ($abs_path | path exists) and ($abs_path | path type) == 'dir' {
let files_in_dir = if $recursive {
find-files $abs_path $all $depth
} else {
get-files $abs_path $all
}
$files_in_dir
} else {
[]
}
}
| flatten
| where { |file| $file.size > 0b }
let sorted_files = if $reverse {
$files | sort-by size --reverse
} else {
$files | sort-by size
}
$sorted_files
| enumerate
| each { |it|
let file = $it.item
let size_display = if $human_readable {
format-file-size $file.size
} else {
$file.size
}
{
rank: ($it.index + 1)
name: $file.name
size: $size_display
size_bytes: ($file.size | into int)
modified: $file.modified
}
}
}
# Helper: Get files from single directory (non-recursive)
def get-files [dir: string, all: bool] {
try {
let files = if $all {
ls -a $dir
} else {
ls $dir
}
$files
| where type == 'file'
| each { |file|
let full_path = ([$dir $file.name] | path join)
let modified_time = get-modified-time $full_path
{
name: $full_path
size: $file.size
modified: $modified_time
}
}
} catch {
[]
}
}
# Helper: Find files recursively with hidden file support
def find-files [dir: string, all: bool, max_depth: int, current_depth: int = 0] {
if $max_depth != null and $current_depth >= $max_depth {
return []
}
let items = try {
if $all {
ls -a $dir
} else {
ls $dir
}
} catch {
[]
}
let files = $items
| where type == 'file'
| each { |file|
let full_path = ([$dir $file.name] | path join)
let modified_time = get-modified-time $full_path
{
name: $full_path
size: $file.size
modified: $modified_time
}
}
let subdir_files = $items
| where type == 'dir'
| where { |dir| $all or not ($dir.name | str starts-with '.') }
| each { |subdir|
let subdir_path = ([$dir $subdir.name] | path join)
find-files $subdir_path $all $max_depth ($current_depth + 1)
}
| flatten
$files ++ $subdir_files
}
# Helper: Get modified time using multiple methods
def get-modified-time [file_path: string] {
# Try method 1: Using metadata command
try {
let meta = metadata $file_path
if ($meta.modified? | is-not-empty) {
return $meta.modified | format date '%b %d %H:%M'
}
} catch {}
# Try method 2: Using system stat command (Unix)
try {
let result = (do -i { stat -c '%y' $file_path } | complete)
if $result.exit_code == 0 and ($result.stdout | str length) > 0 {
let date_str = $result.stdout | str trim
return (parse-datetime $date_str '%Y-%m-%d %H:%M:%S')
}
} catch {}
# Try method 3: Using system stat command (macOS)
try {
let result = (do -i { stat -f '%Sm' $file_path } | complete)
if $result.exit_code == 0 and ($result.stdout | str length) > 0 {
let date_str = $result.stdout | str trim
return $date_str
}
} catch {}
# Fallback
"N/A"
}
# Helper: Parse datetime string
def parse-datetime [date_str: string, format: string] {
try {
# Simple parsing for common formats
if $format == '%Y-%m-%d %H:%M:%S' {
let parts = $date_str | split row ' '
let date_part = $parts.0
let time_part = $parts.1 | split row ':' | take 2 | str join ':'
let month_map = {
'01': 'Jan', '02': 'Feb', '03': 'Mar', '04': 'Apr',
'05': 'May', '06': 'Jun', '07': 'Jul', '08': 'Aug',
'09': 'Sep', '10': 'Oct', '11': 'Nov', '12': 'Dec'
}
let date_parts = $date_part | split row '-'
let month = $month_map | get $date_parts.1
let day = $date_parts.2 | into int | into string
return $"($month) ($day) ($time_part)"
}
} catch {}
$date_str
}
# Helper: Format file size for human-readable output.
# Functional version using logarithms (more mathematical)
def format-file-size [size: filesize] {
let size_int = $size | into int
if $size_int == 0 {
"0 B"
} else {
let units = ['B', 'KB', 'MB', 'GB', 'TB']
# Calculate unit index using powers of 1024
let unit_index = ([0, 1, 2, 3, 4] | where { |exp| $size_int >= (1024 ** $exp) } | last)
let value = $size_int / (1024 ** $unit_index)
let unit = $units | get $unit_index
$'($value | math round -p 2) ($unit)'
}
}
# Create convenient aliases
export alias lz = files-by-size
export alias fbs = files-by-size
export alias large-files = files-by-size --reverse --human-readable
export alias find-large = files-by-size --recursive --reverse --human-readable
export alias all-files = files-by-size --all --human-readable
export alias all-large = files-by-size --all --reverse --human-readable
export alias sysdmng = systemd-manager-tui
export alias h = history
export alias rt = sudo /usr/bin/kitten run-shell --shell=/usr/bin/fish --env PATH=$PATH:$(dirname /usr/bin/kitten)
### Usage Examples
## Basic usage
# files-by-size
#
## Include hidden files
# files-by-size --all
# files-by-size -a
#
## Various combinations
# files-by-size -ah ~/Documents
# files-by-size -ar ~/Projects
# files-by-size -aRh ~/Downloads
#
## Recursive search
# files-by-size --recursive --all --depth 3 ~/
#
## Using aliases
# all-files ~/config
# all-large ~/cache
# find-large -a ~/projects
#
## Multiple directories
# files-by-size -a ~/.config ~/.cache ~/.local
# Create multiple directories with 755 permissions (including parent directories)
def md [
...directories: string # Variable number of directory paths to create
--parents (-p) # Create parent directories as needed
] {
if ($directories | is-empty) {
print "⚠️ md requires at least one argument (e.g. md directory_name)"
return
}
$directories
| each {|dir|
if $parents {
# Create parent directories recursively
mkdir $dir
# Set permissions on the target directory
chmod 755 $dir
echo $"Created directory ($dir) with permissions 755 (including parents)"
} else {
if ($dir | path exists) {
echo $"Directory ($dir) already exists"
} else {
mkdir $dir
chmod 755 $dir
echo $"Created directory ($dir) with permissions 755"
}
}
}
}
# Check size of a specific file or directory
#
# Usage
# size-of ~/Documents
# size-of some_file.txt
def size-of [path: string] {
if ($path | path exists) {
let item = (ls $path | first)
if $item.type == "dir" {
let size = (du $path | get size | math sum)
{path: $path, type: "directory", size: $size}
} else {
{path: $path, type: "file", size: $item.size}
}
} else {
echo $"Path ($path) does not exist"
}
}
$env.LESS = "--line-numbers --ignore-case --chop-long-lines --raw-control-chars --long-prompt --hilite-search --no-init --incsearch"
$env.RIPGREP_CONFIG_PATH = ($env.HOME | path join ".config/ripgrep/ripgrep.conf")
# Disable the ls underlining
# $env.config.ls.clickable_links = false depricated
$env.config.shell_integration.osc8 = false
source ~/.zoxide.nu
source ~/.cache/carapace/init.nu
# Games
$env.PATH = ($env.PATH | prepend "/usr/games")
#
# source ~/.config/nushell/prompt.nu
# $env.PROMPT_COMMAND = {|| full-left-prompt }
# $env.PROMPT_INDICATOR = {|| "" }
# $env.PROMPT_COMMAND_RIGHT = {|| "" }
mkdir ($nu.data-dir | path join "vendor/autoload")
starship init nu | save -f ($nu.data-dir | path join "vendor/autoload/starship.nu")
# IntelliShell
$env.INTELLI_HOME = "/home/earl/.local/share/intelli-shell"
$env.PATH = ($env.PATH | prepend "/home/earl/.local/share/intelli-shell/bin")
# $env.INTELLI_SEARCH_HOTKEY = "control space"
# $env.INTELLI_VARIABLE_HOTKEY = "control char_l"
# $env.INTELLI_BOOKMARK_HOTKEY = "control char_b"
# $env.INTELLI_FIX_HOTKEY = "control char_x"
# alias is = intelli-shell
# mkdir ($nu.data-dir | path join "vendor/autoload")
intelli-shell init nushell | save -f ($nu.data-dir | path join "vendor/autoload/intelli-shell.nu")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment