Files
fish/configs/alias_useful.fish
T

611 lines
19 KiB
Fish
Executable File

#
# In ~/.config/fish/configs/alias_useful.fish
#
alias mount_all='sudo $HOME/.config/fish/scripts/mount.sh'
alias umount_all='sudo $HOME/.config/fish/scripts/umount.sh'
# function mkdir
# command mkdir $argv
# and cd -- $argv[-1]
# end
################ COMMON ALIASES ################################################
alias rm='rm -r'
alias remount='sudo mount -a'
alias mount_config='sudo nano /etc/fstab'
alias own='sudo chown -R "$USER":"$USER"'
alias right='sudo chmod -R 755'
function reload
if command -q omf
omf reload
else
clear
exec fish
end
end
alias fish_config='nano $HOME/.config/fish/config.fish'
alias ip='ip -color'
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
alias jctl='journalctl -p 3 -xb'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias tarnow='tar -acf'
alias untar='tar -zxvf'
################ HELPERS ######################################################
function help_pdf
echo "Best Tool to combine pdf: PDF Arranger"
echo "Best Tool for OCR: OCRMYPDF but you need to install the tesseracts for your languages"
echo "Best Tool to write in PDFs: XOURNAL++"
end
if test -e /usr/bin/rsync
function rcp
if test (count $argv) -lt 2
echo "Usage: rcp SOURCE... DESTINATION"
return 1
end
rsync -avh --progress $argv
end
function rmv
if test (count $argv) -lt 2
echo "Usage: rmv SOURCE... DESTINATION"
return 1
end
rsync -avh --progress --remove-source-files $argv
end
else
function rcp
echo "'rsync' is not installed. Install it via your package manager."
end
function rmv
echo "'rsync' is not installed. Install it via your package manager."
end
end
if test -e /usr/bin/nano
function nanorm
if test (count $argv) -lt 1
echo "Usage: nanorm {FILE}"
return 1
end
rm $argv[1]
nano $argv[1]
end
else
function nanorm
echo "'nano' is not installed. Install it via your package manager."
end
end
function source --description "Source files with auto-detection (replaces builtin source)"
# --- 0. Handle standard input piping (Fixes background script sourcing) ---
if not isatty stdin
builtin source $argv
return
end
# --- 1. Handle Flags First ---
if test (count $argv) -gt 0; and string match -q -- "--*" $argv[1]
builtin source $argv
return
end
# --- 2. Check: Is a file/argument given? ---
if test (count $argv) -gt 0
set file $argv[1]
set -e argv[1]
# Check if the explicit path actually exists as a file
if not test -f "$file"
echo "source: No such file: '$file'" >&2
return 1
end
# Logic: Is it .fish?
if string match -q "*.fish" -- $file
builtin source $file $argv
return
# Logic: Is it .bash?
else if string match -q "*.bash" -- $file or string match -q "*.sh" -- $file
if command -q bass
bass source $file $argv
return
else
echo "source: 'bass' is not installed. Install it to source bash files." >&2
return 1
end
# Logic: Else (unknown file type)
else
echo "source: Unknown file extension for '$file'. Please specify a .fish or .bash file." >&2
return 1
end
# --- 3. No file given: Check for local venv directories ---
else
# Target directories to check in order of preference
set venv_dirs .venv venv
for dir in $venv_dirs
if test -d "$dir"
# Check for .fish activator first
if test -f "$dir/bin/activate.fish"
builtin source "$dir/bin/activate.fish"
return
# Check for .bash activator second
else if test -f "$dir/bin/activate" or test -f "$dir/bin/activate.bash"
# Resolve which bash file exists
set bash_act "$dir/bin/activate"
if test -f "$dir/bin/activate.bash"
set bash_act "$dir/bin/activate.bash"
end
if command -q bass
bass source $bash_act
return
else
echo "source: Found bash virtual env in '$dir', but 'bass' is not installed." >&2
return 1
end
end
end
end
# Logic: Else error (No venv found and no args provided)
echo "source: No file specified and no local venv or .venv directory found." >&2
return 1
end
end
################ TERMINAL-SPECIFIC ############################################
if test "$TERM" = "xterm-kitty"
alias icat='kitty +kitten icat'
end
################ PYTHON #######################################################
if test -e /usr/bin/python3
alias python='python3'
alias py='python3'
end
if test -e /usr/bin/python3
function mkvenv
if not python3 -c "import venv" ^/dev/null
echo "Python3 'venv' module is not available. Install python3-venv via your package manager."
return 1
end
if test (count $argv) -lt 1
echo "Usage: mkvenv {folder name}"
return 1
end
python3 -m venv $argv[1]
if test -f $argv[1]/bin/activate.fish
source $argv[1]/bin/activate.fish
else
echo "Error: Could not find activate.fish in $argv[1]/bin/"
return 1
end
end
else
function mkvenv
echo "Python3 is not installed. Please install Python3 to use mkvenv."
end
end
if test -e /usr/bin/python3
function src
if not test -d .venv
echo "Cannot activate: .venv directory does not exist."
return 1
end
if not test -d .venv/bin
echo "Cannot activate: .venv/bin directory does not exist."
return 1
end
if test -f .venv/bin/activate.fish
source .venv/bin/activate.fish
return 0
end
if test -f .venv/bin/activate.bash
if test -e /usr/bin/bass
bass source .venv/bin/activate.bash
return 0
else
echo "'bass' is not installed. Install it via your package manager to source bash scripts."
return 1
end
end
echo "No usable activation script found in .venv/bin/"
return 1
end
else
function src
echo "Python3 is not installed. Please install Python3 to use src."
end
end
################ OLLAMA ######################################################
if test -e /usr/bin/ollama
alias llama_start='ollama serve'
alias llama3='ollama run llama3'
end
################ EZA ##########################################################
if test -x /usr/bin/eza
function ls
command eza -a --color=always --group-directories-first --icons=always $argv
end
function lr
command eza -al --color=always --group-directories-first --icons=always $argv
end
function ll
command eza -l --color=always --group-directories-first --icons=always $argv
end
function lt
command eza -aT --color=always --group-directories-first --icons=always $argv
end
function l.
command eza -ald --color=always --group-directories-first --icons=always .* $argv
end
function lf
command eza -aD --color=always --group-directories-first --icons=always $argv
end
else
for cmd in lr ll lt l. lf
function $cmd --inherit-variable cmd
echo "The command 'eza' is not installed. Please install 'eza' to use this: https://github.com/eza-community/eza"
end
end
end
################ BTOP #########################################################
if test -e /usr/bin/btop
alias tasks='btop'
end
################ MPV ##########################################################
if test -e /usr/bin/mpv
alias uni_play='mpv --vo=tct'
alias ascii_play='mpv --vo=caca'
else
for cmd in uni_play ascii_play
function $cmd --inherit-variable cmd
echo "The command 'mpv' is not installed. Please install mpv to use this: https://github.com/mpv-player/mpv"
end
end
end
################ GREP #########################################################
if command -q ugrep
alias grep='ugrep --color=auto'
alias egrep='ugrep -E --color=auto'
alias fgrep='ugrep -F --color=auto'
else
alias egrep='grep -E'
alias fgrep='grep -F'
end
if command -q ugrep
alias igrep='ugrep --color=auto --invert-match'
else if command -q grep
alias igrep='grep --color=auto --invert-match'
end
################ WGET #########################################################
if test -e /usr/bin/wget
alias wget='wget -c'
end
################ OCRMYPDF #####################################################
if test -e /usr/bin/ocrmypdf
alias ocrmypdf='ocrmypdf --deskew --clean --rotate-pages'
else
function ocrmypdf
echo "The command 'ocrmypdf' is not installed. Install it via your package manager: https://github.com/ocrmypdf/OCRmyPDF"
end
end
################ YAZI #########################################################
if test -e /usr/bin/yazi
function y
set tmp (mktemp -t "yazi-cwd.XXXXXX")
yazi $argv --cwd-file="$tmp"
if read -z cwd < "$tmp"; and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
builtin cd -- "$cwd"
end
rm -f -- "$tmp"
end
else
for cmd in yazi y
function $cmd --inherit-variable cmd
echo "The command 'yazi' is not installed. Please install 'yazi' to use this: https://github.com/sxyazi/yazi"
end
end
end
################ SUDO #########################################################
if test -e /usr/bin/sudo-rs
alias sudo=sudo-rs
end
################ FLASH ISO ####################################################
if test -e /usr/bin/dd
function flashiso --description "Flash an ISO to a target drive with dd"
if test (count $argv) -ne 2
echo "Usage: flashiso {ISO} {target drive}"
echo "Example: flashiso ubuntu-25.10-desktop-amd64.iso /dev/sdb"
return 1
end
sudo dd if=$argv[1] of=$argv[2] bs=4M status=progress oflag=sync
end
end
################ GIT ##########################################################
function git_cred
if test -f .git/config
# Check if the credential helper is already in the file
if grep -q "git-honney333-work-cred" .git/config
echo "Credential helper is already configured in .git/config"
else
echo "Adding Password tool to git"
# Append the blocks with a clean newline separator
echo -e "\n[credential]\n helper = !"$HOME"/.bin/git-honney333-work-cred" >> .git/config
end
else
echo "no .git/config file"
echo "could not add the credentials helper"
end
end
################ Optimize Pictures ##########################################################
function optimize-jpegs
if command -q jpegoptim
find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.jpe' -o -iname '*.jif' -o -iname '*.jfif' -o -iname '*.jfi' \) -exec jpegoptim --strip-none {} +
else
echo "jpegoptim is not installed. Install it with: sudo pacman -S jpegoptim"
end
end
function optimize-pngs
if command -q oxipng
find . -type f -iname '*.png' -exec oxipng {} +
else
echo "oxipng is not installed. Install it with: sudo pacman -S oxipng"
end
end
function optimize-gifs
if command -q gifsicle
find . -type f -iname '*.gif' -exec gifsicle -b -O3 {} +
else
echo "gifsicle is not installed. Install it with: sudo pacman -S gifsicle"
end
end
function optimize-webps
if command -q cwebp
find . -type f -iname '*.webp' -exec sh -c 'for f; do cwebp -lossless -quiet "$f" -o "$f.tmp" && mv "$f.tmp" "$f"; done' sh {} +
else
echo "cwebp is not installed. Install it with: sudo pacman -S libwebp-utils"
end
end
function optimize-svgs
if command -q svgo
find . -type f -iname '*.svg' -exec svgo {} +
else
echo "svgo is not installed. Install it with: sudo pacman -S svgo"
end
end
function __opt_size --description "Format a byte count as human-readable"
set -l b $argv[1]
if test $b -ge 1073741824
printf '%.1fG' (math "$b / 1073741824")
else if test $b -ge 1048576
printf '%.1fM' (math "$b / 1048576")
else if test $b -ge 1024
printf '%.1fK' (math "$b / 1024")
else
printf '%dB' $b
end
end
function optimize-pictures --description "Optimize images in one pass. Flags: --continue resume, -v/--verbose show tool output"
set -l cache_file "$PWD/.optimize-pictures.cache"
set -l state_file "$PWD/.optimize-pictures.state"
set -l rebuild 1
set -l verbose 0
for arg in $argv
switch $arg
case --continue
set rebuild 0
case --verbose -v
set verbose 1
end
end
if test $rebuild -eq 0
if not test -f "$cache_file"
echo "optimize-pictures: no cache file found at $cache_file" >&2
return 1
end
else
find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.jpe' -o -iname '*.jif' -o -iname '*.jfif' -o -iname '*.jfi' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.webp' -o -iname '*.svg' \) > "$cache_file"
end
if not test -s "$cache_file"
echo "optimize-pictures: no images to optimize."
return 0
end
set -l remaining_file "$cache_file.tmp"
: > "$remaining_file"
set -l total (count (cat $cache_file))
set -l done 0
set -l saved 0
set -l original 0
if test $rebuild -eq 0
set -l st (string split ' ' -- (cat "$state_file" 2>/dev/null))
if test (count $st) -ge 2
set saved $st[1]
set original $st[2]
end
end
set -l live 0
if isatty stdout; and test $verbose -eq 0
set live 1
end
while read -l file
set done (math $done + 1)
set -l ext (string lower -- (string split -r '.' -- $file)[-1])
set -l before (stat -c%s -- "$file" 2>/dev/null; or echo 0)
set -l ok 1
switch $ext
case jpg jpeg jpe jif jfif
if command -q jpegoptim
if test $verbose -eq 1
jpegoptim --strip-none "$file"
else
jpegoptim --strip-none "$file" >/dev/null 2>&1
end
set ok $status
else
echo "jpegoptim not installed, skipping: $file" >&2
end
case png
if command -q oxipng
if test $verbose -eq 1
oxipng "$file"
else
oxipng "$file" >/dev/null 2>&1
end
set ok $status
else
echo "oxipng not installed, skipping: $file" >&2
end
case gif
if command -q gifsicle
if test $verbose -eq 1
gifsicle -b -O3 "$file"
else
gifsicle -b -O3 "$file" >/dev/null 2>&1
end
set ok $status
else
echo "gifsicle not installed, skipping: $file" >&2
end
case webp
if command -q cwebp
if test $verbose -eq 1
cwebp -lossless "$file" -o "$file.tmp"
else
cwebp -lossless -quiet "$file" -o "$file.tmp" >/dev/null 2>&1
end
set ok $status
if test $ok -eq 0
mv "$file.tmp" "$file"
set ok $status
end
else
echo "cwebp not installed, skipping: $file" >&2
end
case svg
if command -q svgo
if test $verbose -eq 1
svgo "$file"
else
svgo "$file" >/dev/null 2>&1
end
set ok $status
else
echo "svgo not installed, skipping: $file" >&2
end
end
set original (math $original + $before)
if test $ok -eq 0
set -l after (stat -c%s -- "$file" 2>/dev/null; or echo 0)
if test $after -lt $before
set saved (math $saved + $before - $after)
end
else
echo "$file" >> "$remaining_file"
end
echo "$saved $original" > "$state_file"
set -l pct 0
if test $original -gt 0
set pct (math "round($saved * 100 / $original)")
end
if test $live -eq 1
printf '\r\033[2K[%d/%d] %s saved %s (%d%%)' $done $total $file (__opt_size $saved) $pct
else if test $verbose -eq 0
printf '[%d/%d] %s saved %s (%d%%)\n' $done $total $file (__opt_size $saved) $pct
end
end < "$cache_file"
if test $live -eq 1
echo
end
mv "$remaining_file" "$cache_file"
set -l pct 0
if test $original -gt 0
set pct (math "round($saved * 100 / $original)")
end
set -l sz (__opt_size $saved)
if not test -s "$cache_file"
rm -f "$cache_file" "$state_file"
echo "optimize-pictures: optimized $done images, saved $sz ($pct%)."
else
set -l remaining (count (cat $cache_file))
echo "optimize-pictures: $remaining images remaining (saved so far: $sz, $pct%). Run 'optimize-pictures --continue' to resume."
end
end
function install-optimizer --description "Install image optimizer packages (Arch/pacman)"
if not command -q pacman
echo "install-optimizer: pacman not found. This helper is for Arch Linux." >&2
return 1
end
set -l pkgs jpegoptim oxipng gifsicle libwebp-utils svgo
if test (count $argv) -gt 0
set pkgs $argv
end
sudo pacman -S --needed $pkgs
end