mirror of
				https://github.com/alacritty/alacritty.git
				synced 2025-10-30 23:36:53 -04:00 
			
		
		
		
	Add script for spawning alacritty in CWD
The script uses the recently added _NET_WM_PID window property for finding the program running in Alacritty, fetching its working directory, and spawning a new Alacritty using that directory.
This commit is contained in:
		
							parent
							
								
									574586045f
								
							
						
					
					
						commit
						ac2a1ece9e
					
				
					 1 changed files with 44 additions and 0 deletions
				
			
		
							
								
								
									
										44
									
								
								scripts/spawn-alacritty-cwd
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										44
									
								
								scripts/spawn-alacritty-cwd
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
# Spawn a new instance of Alacritty using the CWD of the currently focused
 | 
			
		||||
# Alacritty process.
 | 
			
		||||
#
 | 
			
		||||
# This is useful in environment like i3 where terminals are opened using a
 | 
			
		||||
# key-combination while another terminal is already focused.
 | 
			
		||||
#
 | 
			
		||||
# If the script is run with a non-Alacritty window in focus or a non-compliant
 | 
			
		||||
# version of Alacritty, the script will exit with code 1. This makes it possible
 | 
			
		||||
# to use the script like
 | 
			
		||||
#
 | 
			
		||||
#     spawn-alacritty-cwd || alacritty
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
ACTIVE_WINDOW=$(xdotool getactivewindow)
 | 
			
		||||
ACTIVE_WM_CLASS=$(xprop -id $ACTIVE_WINDOW | grep WM_CLASS)
 | 
			
		||||
 | 
			
		||||
if [[ $ACTIVE_WM_CLASS == *"Alacritty"* ]]
 | 
			
		||||
then
 | 
			
		||||
    # Get PID. If _NET_WM_PID isn't set, bail.
 | 
			
		||||
    PID=$(xprop -id $ACTIVE_WINDOW | grep _NET_WM_PID | grep -oP "\d+")
 | 
			
		||||
    if [[ "$PID" == "" ]]
 | 
			
		||||
    then
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Get first child of terminal
 | 
			
		||||
    CHILD_PID=$(pgrep -P $PID)
 | 
			
		||||
    if [[ "$PID" == "" ]]
 | 
			
		||||
    then
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Get current directory of child. The first child should be the shell.
 | 
			
		||||
    pushd "/proc/${CHILD_PID}/cwd"
 | 
			
		||||
    SHELL_CWD=$(pwd -P)
 | 
			
		||||
    popd
 | 
			
		||||
 | 
			
		||||
    # Start alacritty with the working directory
 | 
			
		||||
    alacritty --working-directory $SHELL_CWD
 | 
			
		||||
else
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue