Skip to content

Instantly share code, notes, and snippets.

@JorianWoltjer
Last active March 15, 2025 10:44
Show Gist options
  • Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
My dotfiles setup for WSL with way too many shortcuts
# Environment Variables
export SCREENDIR=$HOME/.screen
export DISCORD_WEBHOOK="https://discord.com/api/webhooks/[...]/[...]"
export DISCORD_TOKEN="[...]"
export MANPAGER="sh -c 'col -bx | bat -l man -p'" # highlight manpages with https://github.com/sharkdp/bat
export PROMPT_DIRTRIM=1 # truncate cwd in prompt to '...'
wt_duplicate() { # Keep working directory when splitting panes in Windows Terminal
echo -en '\e]9;9;"'
wslpath -w "$PWD" | tr -d '\n'
echo -en '"\x07'
}
export PROMPT_COMMAND="wt_duplicate 2>/dev/null"
# Tools
alias d="/tool/self/drag.py" # https://gist.github.com/JorianWoltjer/cc4ed7415b665d35e2d010cd2c04c8a6
alias bashrc="nano +157 ~/.bashrc && source ~/.bashrc" # start further down in file and automatically apply after saving
alias update="sudo bash -c 'apt update && apt list --upgradable && apt upgrade -y'"
alias dc="docker compose"
alias docker-reload="docker-compose rm -f && docker-compose up --build"
alias dr=docker-reload # yes, an alias for my alias :P
alias b="cd .." # short for 'back'
alias c="code ."
alias objdump="objdump -M intel"
alias strace="strace -ff"
alias python=python3
alias py=python3
alias psh=powershell.exe
alias me="sudo chown $USER:$(id -g)" # make you own a file
alias untar="tar -xvf"
alias gitc="git init && git add . && git commit -m 'Initial commit'"
alias sizes="du -had1 . | sort -h"
alias discord='sed '"'"'s/\x1b\[9\([0-7]\)m/\x1b\[3\1m/g'"'"' | sed '"'"'s/\x1b\[10\([0-7]\)m/\x1b\[4\1m/g'"'"' | (echo '"'"'```ansi'"'"'; cat; echo '"'"'```'"'"') | clip.exe'
# ^^ crazy bash syntax for copying command output (piped to it) as a discord ansi code block
alias dubuntu='docker run -it --rm -v $(pwd):/pwd -w /pwd ubuntu bash' # start sandbox docker container with current directory
alias tmp='cd $(mktemp -d)'
# Functions
mkdirs() { # Make directory and cd into it
mkdir -p "$1" && cd "$1"
}
clone() {
git clone "$@" && cd "$(basename "$1" .git)"
}
xp() { # Open current or specific directory in Windows Explorer
p=${1:-"."}
explorer.exe $(wslpath -w "$p")
}
man() { # Show manpage or help menu if not found
/usr/bin/man $@ || $1 -h
}
cdw() {
if [ $# -eq 0 ]; then # Go to windows home directory
cd /mnt/c/
path=$(cmd.exe /C "echo %USERPROFILE%" | tr -d '\r')
else # Go to specified directory
path="$1"
fi
path=$(wslpath "$path")
cd "$path"
}
waitport() { # Wait until one of many ports is open
echo -n "Waiting"
while true; do
for port in "${@:2}"; do
nc -w 1 -z $1 $port
if [[ $? == 0 ]]; then
echo -e "\a\nPort $2 is open on $1"
return
fi
done
sleep 1
echo -n "."
done
}
ham() { # short for 'hamburger', shows top and bottom of file in hex
lines=${2:-10}
size=$(stat --format="%s" "$1")
if [ $size -lt $((32*$lines)) ]; then
hexyl "$1"
return
fi
offset=$((16 - $size % 16))
head "$1" -c $(($lines*16)) | hexyl | sed '$d'
echo "..."
tail "$1" -c $(($lines*16-$offset)) | hexyl -o $(($size+$offset - $lines*16)) | sed '1d'
}
vps_forward() { # Expose a local TCP port remotely on a VPS
echo "Forwarding TCP localhost:$1 to vps.jorianwoltjer.com:$1"
ssh -R 0.0.0.0:$1:localhost:$1 [email protected] "read"
}
vps_forward_udp() { # Expose a local UDP port remotely on a VPS (over TCP)
echo "Forwarding UDP localhost:$1 to vps.jorianwoltjer.com:$1"
echo "Make sure VPS firewall also allows $1"
remote_port=$(shuf -i 2000-65000 -n 1)
ssh -R $remote_port:localhost:$1 [email protected] "socat udp-listen:$1,reuseaddr,fork udp:localhost:$remote_port" && ssh [email protected] "pkill socat"
}
forward() { # Forward one TCP port to another
if [[ $# -ne 2 ]]; then
echo "Usage: forward <listen_port> <forward_port>"
return 1
fi
if (( $1 < 1024 )); then
sudo socat tcp-listen:$1,reuseaddr,fork tcp:0.0.0.0:$2
else
socat tcp-listen:$1,reuseaddr,fork tcp:0.0.0.0:$2
fi
}
len() { # Get length of a string in the first argument or STDIN
if [ $# -gt 1 ]; then
echo "Error: Only one argument is allowed (len 'something with spaces')"
return 1
elif [ -z "$1" ]; then
read -r input
echo ${#input}
else
echo ${#1}
fi
}
cmd() {
cmd.exe /c "$*"
}
pyrust() { # Setup Rust library for use in Python
python -m venv .env
source .env/bin/activate
pip install maturin
maturin init --bindings pyo3
maturin develop
}
csv() { # Read CSV for piping
column -t -s, -n "$@" | less -F -S -X -K
}
urldecode() { # URL decode STDIN
while read; do echo -e ${REPLY//%/\\x}; done
}
hashcat() {
(cd '/mnt/c/Program Files/hashcat-6.2.6' && ./hashcat.exe $@)
}
scan() {
if [ "$#" -lt 1 ]; then
echo "Usage: scan <language> [build-mode]"
return 1
fi
local language="$1"
semgrep --config=p/$language -o semgrep.txt
local cmd=(codeql database create .codeql --overwrite --language=$language)
if [ ! -z "$2" ]; then
cmd+=(--build-mode=$2)
fi
"${cmd[@]}"
codeql database analyze .codeql --format=sarif-latest --output=codeql.sarif
echo "CodeQL found $(jq '.runs[0].results | length' codeql.sarif) vulnerabilities."
}
# Place this file in %USERPROFILE%\Documents\WindowsPowerShell
# Exit powershell with Ctrl+D
Set-PSReadlineKeyHandler -Key ctrl+d -Function ViExit
# Check what process is using a port
function Get-Port {
param (
$Port
)
Get-Process -Id (Get-NetTCPConnection -LocalPort $Port).OwningProcess
Get-Process -Id (Get-NetUDPEndpoint -LocalPort $Port).OwningProcess
echo "Tip: If nothing is running and the port is still blocked, try:"
echo "cmd net stop hns && net start hns"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment