Skip to content

Opening a new terminal with Super+Enter sometimes sets working directory to /usr/bin instead of $HOME #3427

@daviewales

Description

@daviewales

System details

Thinkpad L380, Omarchy 3.1.7

What's wrong?

If the current active window has a child process, then Super+Enter sets the working directory of the newly opened terminal to the cwd of the child process. This is great when the current active window is a terminal, and the child process is a shell. This is less great when the current active window is Chromium or Firefox, and the child process is some random web process, with cwd /usr/bin.

Why

Looking at ~/.config/hypr/bindings.conf, we see the following:

bindd = SUPER, RETURN, Terminal, exec, $terminal --dir="$(omarchy-cmd-terminal-
cwd)"

Of particular interest, is the command omarchy-cmd-terminal-cwd.

We can reproduce the underlying issue by running the following, and ensuring that the active window is Chromium before the sleep finishes:

sleep 3 && omarchy-cmd-terminal-cwd

If you managed to focus Chromium before the sleep finished, you will see the output /usr/bin.

Looking at the source for omarchy-cmd-terminal-cwd, it is immediately clear why this happens:

#!/bin/bash
# Go from current active terminal to its child shell process and run cwd there
terminal_pid=$(hyprctl activewindow | awk '/pid:/ {print $2}')
shell_pid=$(pgrep -P "$terminal_pid" | tail -n1)
if [[ -n $shell_pid ]]; then
cwd=$(readlink -f "/proc/$shell_pid/cwd" 2>/dev/null)
if [[ -d $cwd ]]; then
echo "$cwd"
else
echo "$HOME"
fi
else
echo "$HOME"
fi

You can see that it assumes that the current active window is a terminal, and that the last child PID belongs to a shell spawned by that terminal.

But this is not a valid assumption, if the current active window is not a terminal, and the last child PID of that window is not a shell.

Proposed fix

diff --git a/bin/omarchy-cmd-terminal-cwd b/bin/omarchy-cmd-terminal-cwd
index 062c35f..b651148 100755
--- a/bin/omarchy-cmd-terminal-cwd
+++ b/bin/omarchy-cmd-terminal-cwd
@@ -6,8 +6,10 @@ shell_pid=$(pgrep -P "$terminal_pid" | tail -n1)

 if [[ -n $shell_pid ]]; then
   cwd=$(readlink -f "/proc/$shell_pid/cwd" 2>/dev/null)
+  shell=$(readlink -f "/proc/$shell_pid/exe" 2>/dev/null)

-  if [[ -d $cwd ]]; then
+  # Check if $shell is a valid shell and $cwd is a directory.
+  if grep -qs "$shell" /etc/shells && [[ -d $cwd ]]; then
     echo "$cwd"
   else
     echo "$HOME"

I'll link a PR with this fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions