A while ago I made a tiny function in my ~/.zshrc to download a video from the link in my clipboard. I use this nearly every day to share videos with people without forcing them to watch it on whatever site I found it. What’s a script/alias that you use a lot?
# Download clipboard to tmp with yt-dlp
tmpv() {
cd /tmp/ && yt-dlp "$(wl-paste)"
}
I often want to know the status code of a
curl
request, but I don’t want that extra information to mess with the response body that it prints to stdout.What to do?
Render an image instead, of course!
curlcat
takes the same params ascurl
, but it uses iTerm2’simgcat
tool to draw an “HTTP Cat” of the status code.It even sends the image to stderr instead of stdout, so you can still pipe
curlcat
tojq
or something.#!/usr/bin/env zsh stdoutfile=$( mktemp ) curl -sw "\n%{http_code}" $@ > $stdoutfile exitcode=$? if [[ $exitcode == 0 ]]; then statuscode=$( cat $stdoutfile | tail -1 ) if [[ ! -f $HOME/.httpcat$statuscode ]]; then curl -so $HOME/.httpcat$statuscode https://http.cat/$statuscode fi imgcat $HOME/.httpcat$statuscode 1>&2 fi cat $stdoutfile | ghead -n -1 exit $exitcode
Note: This is macOS-specific, as written, but as long as your terminal supports images, you should be able to adapt it just fine.
LOVE this
this one is clean asl
I wrote a script called
please
. You inputplease
followed by any other command (e.g.please git clone
,please wget blahblah
) and a robotic voice will say “affirmative,” then the command will run, and when it completes, the robotic voice reads out the exit code (e.g. “completed successfully” or “failed with status 1” etc.)This is useful for when you have a command that takes a long time and you want to be alerted when it’s finished. And it’s a gentleman.
You can also use something like notifyd to generate a pop up for visual feedback :) I can’t remember the exact command right now though. Differs per distro or desktop environment, obviously.
notify-send 'command finished!'
works pretty wellAlso,
printf '\a'
will output an alert bell character which should make the terminal beep/blink and be highlighted for attention by your wm/compositor if it’s unfocused.I have that aliased to
a
to get notified whenever a long running command finishes just by adding;a
at the end.
I once experimented with something similar, except it was supported to trigger my smart speaker and drop into another part of the house to tell me.
Honestly, I really need to replace my proprietary smart speaker system with something self-hosted; it’s just I only recently have had the time to start cinsidering.
Here are probably the most useful ones. I prefer for
rm
to be interactive so I don’t accidentally delete something important and formkdir
to create a parent directory if necessary.alias rm='rm -i' alias mkdir='mkdir -p' alias podup='podman-compose down && podman-compose pull && podman-compose up -d'
This extract function (which I didn’t make myself, I got it from when I was using nakeDeb) has been pretty useful too.
function extract() { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *.xz) unxz $1 ;; *) echo "'$1' cannot be extracted via >extract<" ;; esac else echo "'$1' is not a valid file" fi }
I have a similar docker/podman alias, except I pull first. This greatly reduces downtime between
down
andup
, which is nice for critical services.Yeah, that makes sense. I don’t have anything critical; just nginx, a book server, a recipe collection, and some other small stuff.
alias fucking='sudo'
(my coworkers often usedprettyplease
instead)alias clip='xclip -selection clipboard'
When you pipe to this, for example
ls | clip
, it will stick the output of the command ran into the clipboard without needing to manually copy the output.I use a KDE variant of this that uses klipper instead (whatever you pipe to this will be available in klipper):
` #!/bin/sh
function copy { if ! tty -s && stdin=$(</dev/stdin) && [[ "$stdin" ]]; then stdin=$stdin$(cat) qdbus6 org.kde.klipper /klipper setClipboardContents "$stdin" exit fi qdbus6 org.kde.klipper /klipper getClipboardContents } copy $@`
Pretty sure this only works on x distros?
wl-copy
andwl-paste
are for Wayland FYI.Yep, pretty sure you are right.
alias sl=“ls“
real ones watch the train of shame
alias sl='ls | while IFS= read -r line; do while IFS= read -r -n1 char; do if [[ -z "$char" ]]; then printf "\n"; else printf "%s" "$char"; sleep 0.05; fi; done <<< "$line"; done'
I can’t easily check if it works until I get home to my laptop, but you get the idea
That’s very good.
Generalization and line feed customisation :
function sloooower() { BASE_INTERVAL=${1:-0.05} RET_ADD_INTERVAL=${2:-0.1} CHAR_ADD_INTERVAL=${3:-0} while IFS= read -r -n1 char; do if [[ -z "$char" ]]; then sleep "${RET_ADD_INTERVAL}" printf "\n" else sleep "${CHAR_ADD_INTERVAL}" printf "%s" "$char" fi sleep "${BASE_INTERVAL}" done } alias sl='ls | sloooower'
(
command | sloooower 0.000277
is pretty much a 3600 bauds output)
#Create a dir and cd into it mkcd() { mkdir -p "$@" && cd "$@"; }
That’s a helpful one! I also add a function that creates a tmp directory, and cds to it which I frequently use to open a scratch space. I use it a lot for unpacking tar files, but for other stuff too.
(These are nushell functions)
# Create a directory, and immediately cd into it. # The --env flag propagates the PWD environment variable to the caller, which is # necessary to make the directory change stick. def --env dir [dirname: string] { mkdir $dirname cd $dirname } # Create a temporary directory, and cd into it. def --env tmp [ dirname?: string # the name of the directory - if omitted the directory is named randomly ] { if ($dirname != null) { dir $"/tmp/($dirname)" } else { cd (mktemp -d) } }
Here’s a script I use a lot that creates a temporary directory, cds you into it, then cleans up after you exit. Ctrl-D to exit, and it takes you back to the directory you were in before.
Similar to what another user shared replying to this comment but mine is in bash + does these extra stuff.
#!/bin/bash function make_temp_dir { # create a temporary directory and cd into it. TMP_CURR="$PWD"; TMP_TMPDIR="$(mktemp -d)"; cd "$TMP_TMPDIR"; } function del_temp_dir { # delete the temporary directory once done using it. cd "$TMP_CURR"; rm -r "$TMP_TMPDIR"; } function temp { # create an empty temp directory and cd into it. Ctr-D to exit, which will # delete the temp directory make_temp_dir; bash -i; del_temp_dir; } temp
alias fuck='sudo $(history -p \!\!)'
sudo !!
Try it, and you will find it just does not provide the same emotional peace.
I like to imagine I’m yelling it. SUDO!!!
:D
Nice
Why not use thefuck which also corrects typos?
Because i’m not a psychopath, just autistic.
I have the same but it’s called “please”
i touch computers since almost 40 years. “Please” stopped being an option somewhere in the early 2000’s.
One of favorites cds to the root of a project directory from a subdirectory,
# Changes to top-level directory of git repository. alias gtop="cd \$(git rev-parse --show-toplevel)"
For doing stuff in a directory, I use a replacement for
cd
command.For aliases:
alias +='git add' alias +p='git add -p' alias +u='git add -u' alias -- -='cd -' alias @='for i in' alias c='cargo' alias date='LANG=C date' alias diff='cdiff' alias gg='git grep -n' alias grep='grep --color=auto' alias ll='ls -o' alias ls='ls -vFT0 --si --color=auto --time-style=long-iso' alias rmd='rmdir'
I also have various small scripts and functions:
a
for package management (thinkapt
but has simplified arguments which makes it faster to use in usual cases),e
for opening file in Emacs,g
forgit
,s
forsudo
.
And here’s
,
:$ cat ~/.local/bin/, #!/bin/sh if [ $# -eq 0 ]; then paste -sd, else printf '%s\n' "$@" | paste -sd, fi
This tmux wrapper is remarkably convenient:
Usage:
# Usage: t [session-name] # # With no arguments: # Lists existing tmux sessions, or prints "[No sessions]" if none exist. # # With a session name: # Attempts to attach to the named tmux session. # If the session does not exist, creates a new session with that name. # # Examples: # t # Lists all tmux sessions # t dev # Attaches to "dev" session or creates it if it doesn't exist function t { if [[ -z $1 ]]; then tmux ls 2> /dev/null || echo "[No sessions]" else tmux attach -t $@ 2> /dev/null if [[ $? -ne 0 ]]; then tmux new -s $@ fi fi }
$ which diffuc diffuc: aliased to diff -uw --color=always
$ which grepnir grepnir: aliased to grep -niIr
$ cat `which ts` #!/bin/bash if [ "$#" -lt 1 ]; then tmux list-sessions exit fi if ! tmux attach -t "$1" then tmux new-session -s "$1" fi
I have a few interesting ones.
Download a video:
alias yt="yt-dlp -o '%(title)s-%(id)s.%(ext)s' "
Execute the previous command as root:
alias please='sudo $(fc -n -l -1)'
Delete all the Docker things. I do this surprisingly often:
alias docker-nuke="docker system prune --all --volumes --force"
This is a handy one for detecting a hard link
function is-hardlink { count=$(stat -c %h -- "${1}") if [ "${count}" -gt 1 ]; then echo "Yes. There are ${count} links to this file." else echo "Nope. This file is unique." fi }
I run this one pretty much every day. Regardless of the distro I’m using, it Updates All The Things:
function up { if [[ $(command -v yay) ]]; then yay -Syu --noconfirm yay -Yc --noconfirm elif [[ $(command -v apt) ]]; then sudo apt update sudo apt upgrade -y sudo apt autoremove -y fi flatpak update --assumeyes flatpak remove --unused --assumeyes }
I maintain an aliases file in GitLab with all the stuff I have in my environment if anyone is curious.
Execute the previous command as root
Fun fact if you are using bash,
!!
will evaluate to the previous command, so if you miss sudo on some long command, you can also just dosudo !!
.With the added benefit of it looking like you’re yelling at your prompt in order to get it to use sudo.
alias gimme='git checkout'
Twins(-ish)!
alias gimme="chown <myname>:staff"
on most of my systems I get tired of constantly
ls
ing after acd
so I combine them:cd(){ cd $1 && ls }
(excuse if this doesn’t work, I am writing this from memory)
I also wrote a function to access docker commands quicker on my Truenas system. If passed nothing, it enters the docker jailmaker system, else it passes the command to docker running inside the system.
docker () { if [[ "$1" == "" ]]; then jlmkr shell docker return else sudo systemd-run --pipe --machine docker docker "$@" return fi }
I have a few similar shortcuts for programs inside jailmaker and long directories that I got sick of typing out.