go back

better terminal launching in hyprland

10 July 2025

In my hyprland.conf, I previously had the line:

bind = $mainMod, T, exec, $terminal

This is almost identical to the default configuration.

The only issue with this is that whenever you launch a terminal, which in my case is Alacritty, your working directory gets set to your home directory. This can become quite annoying when you’re working in a specific directory and want to quickly launch another terminal in that same directory.

I am somewhat ashamed to admit that I’d put up with this for so long, but at some point one comes to the conclusion that enough is enough.

Using a Bash script that was definitely not vibe-coded (wink, wink), we can fix this problem:

#!/bin/bash

WIN_ID=$(hyprctl activewindow -j | jq -r '.pid')
PID=$(pgrep -P "$WIN_ID" -a | grep -E 'bash' | awk '{print $1}' | head -n1)

if [ -z "$PID" ]; then
    PID="$WIN_ID"
fi

CWD=$(readlink "/proc/$PID/cwd" 2>/dev/null)

if [ -n "$CWD" ] && [ -d "$CWD" ]; then
    alacritty --working-directory "$CWD"
else
    alacritty
fi

This script takes the current active window into consideration when launching a terminal. If the current active window is an Alacritty instance, then it launches a new Alacritty instance with working directory set to the pwd of the previous. Otherwise, it simply defaults to launching an Alacritty instance with the default working directory.

As with any other random snippets of code you stumble across on the internet, use this at your own risk.

It’s not super protable (i.e. for zsh or some non-Alacritty terminal), but it’s somewhat trivial to modify for your own use case.

Now, we if simply substitute our previous hyprland.conf line with

bind = $mainMod, T, exec, path/to/script.sh

and blissfully throw our previous frustrations into the wind.