<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.vifm.info/index.php?action=history&amp;feed=atom&amp;title=Vifm_Launcher_with_Byobu</id>
	<title>Vifm Launcher with Byobu - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.vifm.info/index.php?action=history&amp;feed=atom&amp;title=Vifm_Launcher_with_Byobu"/>
	<link rel="alternate" type="text/html" href="https://wiki.vifm.info/index.php?title=Vifm_Launcher_with_Byobu&amp;action=history"/>
	<updated>2026-07-15T14:19:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.vifm.info/index.php?title=Vifm_Launcher_with_Byobu&amp;diff=8032&amp;oldid=prev</id>
		<title>Xaizek: Initial import</title>
		<link rel="alternate" type="text/html" href="https://wiki.vifm.info/index.php?title=Vifm_Launcher_with_Byobu&amp;diff=8032&amp;oldid=prev"/>
		<updated>2025-03-21T13:47:27Z</updated>

		<summary type="html">&lt;p&gt;Initial import&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This script automates the process of launching a &amp;lt;code&amp;gt;byobu&amp;lt;/code&amp;gt; session with predefined windows (&amp;lt;code&amp;gt;clash&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;scrcpy&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;vifm&amp;lt;/code&amp;gt;) and ensures only one instance of the session is active. If the session already exists, it attaches to the existing session and switches to the &amp;lt;code&amp;gt;vifm&amp;lt;/code&amp;gt; window.&lt;br /&gt;
&lt;br /&gt;
== Features ==&lt;br /&gt;
&lt;br /&gt;
* Automatically creates or attaches to a &amp;lt;code&amp;gt;byobu&amp;lt;/code&amp;gt; session.&lt;br /&gt;
* Configures multiple windows within the session as an example: &amp;lt;code&amp;gt;clash&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;scrcpy&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;vifm&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Ensures only one instance of the session is active.&lt;br /&gt;
* Activates the session's terminal window and switches to the &amp;lt;code&amp;gt;vifm&amp;lt;/code&amp;gt; window.&lt;br /&gt;
* Logs execution details for debugging purposes.&lt;br /&gt;
* Shortcut Integration: You can set up a keyboard shortcut (e.g., &amp;lt;kbd&amp;gt;Win + E&amp;lt;/kbd&amp;gt;) in GNOME by configuring it to run: &amp;lt;code&amp;gt;gnome-terminal -- /home/xxx/apps/vifm_launcher.sh&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This allows you to activate the vifm window directly, similar to how applications are launched in Windows. When the shortcut is pressed:&lt;br /&gt;
* If the session exists, it attaches to the session and switches to the vifm window.&lt;br /&gt;
* If the session does not exist, it creates a new session and opens the predefined windows.&lt;br /&gt;
&lt;br /&gt;
== Configuration ==&lt;br /&gt;
&lt;br /&gt;
Edit variables at the top.&lt;br /&gt;
&lt;br /&gt;
== Known issues ==&lt;br /&gt;
&lt;br /&gt;
* In &amp;lt;code&amp;gt;find_existing_window&amp;lt;/code&amp;gt;, using &amp;lt;code&amp;gt;grep -qi &amp;quot;byobu&amp;quot;&amp;lt;/code&amp;gt; might cause confusion if multiple byobu windows exist&lt;br /&gt;
* When working with terminal tabs (where a new tab is opened in the same window) and the byobu tab isn't currently active, the script fails to reactivate the original byobu terminal tab and instead creates a new session&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
# Get the script directory&lt;br /&gt;
SCRIPT_DIR=&amp;quot;$(cd &amp;quot;$(dirname &amp;quot;${BASH_SOURCE[0]}&amp;quot;)&amp;quot; &amp;amp;&amp;amp; pwd)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
LOG_FILE=&amp;quot;$SCRIPT_DIR/vifm-launcher.log&amp;quot;&lt;br /&gt;
&lt;br /&gt;
SESSION_NAME=&amp;quot;my_session&amp;quot;&lt;br /&gt;
VIFM_WINDOW_NAME=&amp;quot;vifm&amp;quot;&lt;br /&gt;
TERMINAL_CMD=&amp;quot;gnome-terminal&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# windowname:cmd pairs that define configuration of a session&lt;br /&gt;
WINDOWS=(&lt;br /&gt;
    clash:$HOME/apps/clash-linux-amd64-v3&lt;br /&gt;
    scrcpy:$HOME/apps/scrcpy.sh&lt;br /&gt;
    $VIFM_WINDOW_NAME:vifm&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
fail() {&lt;br /&gt;
    echo &amp;quot;Error:&amp;quot; &amp;quot;$@&amp;quot;&lt;br /&gt;
    exit 1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# dependency checks&lt;br /&gt;
command -v xdotool &amp;gt;/dev/null 2&amp;gt;&amp;amp;1 ||&lt;br /&gt;
    fail &amp;quot;xdotool is not installed. Install it using 'sudo apt install xdotool' or alike.&amp;quot;&lt;br /&gt;
command -v byobu &amp;gt;/dev/null 2&amp;gt;&amp;amp;1 ||&lt;br /&gt;
    fail &amp;quot;byobu is not installed. Install it using 'sudo apt install byobu' or alike.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
detect_byobu_backend() {&lt;br /&gt;
    if grep -q &amp;quot;tmux&amp;quot; ~/.byobu/backend ~/.config/byobu/backend 2&amp;gt;/dev/null; then&lt;br /&gt;
        echo &amp;quot;tmux&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        echo &amp;quot;screen&amp;quot;&lt;br /&gt;
    fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# just check for byobu-tmux and use it instead?&lt;br /&gt;
byobu_backend=$(detect_byobu_backend)&lt;br /&gt;
echo &amp;quot;Detected byobu backend: $byobu_backend&amp;quot; &amp;gt;&amp;gt; &amp;quot;$LOG_FILE&amp;quot;&lt;br /&gt;
[ &amp;quot;$byobu_backend&amp;quot; = tmux ] ||&lt;br /&gt;
    fail &amp;quot;Only tmux backend is supported. Run 'byobu-select-profile' to switch.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
find_existing_window() {&lt;br /&gt;
    local retries=5&lt;br /&gt;
    local delay=0.5&lt;br /&gt;
&lt;br /&gt;
    for ((i = 0; i &amp;lt; retries; i++)); do&lt;br /&gt;
        window_id=$(xdotool search --onlyvisible --class &amp;quot;$TERMINAL_CMD&amp;quot; 2&amp;gt;/dev/null | while read -r id; do&lt;br /&gt;
            xdotool getwindowname &amp;quot;$id&amp;quot; 2&amp;gt;/dev/null | grep -qi &amp;quot;byobu&amp;quot; &amp;amp;&amp;amp; echo &amp;quot;$id&amp;quot;&lt;br /&gt;
        done | head -n1)&lt;br /&gt;
&lt;br /&gt;
        if [ -n &amp;quot;$window_id&amp;quot; ]; then&lt;br /&gt;
            echo &amp;quot;$window_id&amp;quot;&lt;br /&gt;
            return 0&lt;br /&gt;
        fi&lt;br /&gt;
        sleep &amp;quot;$delay&amp;quot;&lt;br /&gt;
    done&lt;br /&gt;
&lt;br /&gt;
    return 1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
activate_window() {&lt;br /&gt;
    local window_id=&amp;quot;$1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    if [ -z &amp;quot;$window_id&amp;quot; ]; then&lt;br /&gt;
        return 1&lt;br /&gt;
    fi&lt;br /&gt;
&lt;br /&gt;
    xdotool windowmap &amp;quot;$window_id&amp;quot; 2&amp;gt;/dev/null&lt;br /&gt;
    xdotool windowactivate &amp;quot;$window_id&amp;quot; 2&amp;gt;/dev/null&lt;br /&gt;
&lt;br /&gt;
    return 0&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
create_byobu_session() {&lt;br /&gt;
    if byobu has-session -t &amp;quot;$SESSION_NAME&amp;quot; 2&amp;gt;/dev/null; then&lt;br /&gt;
        echo &amp;quot;Session $SESSION_NAME already exists. Attaching...&amp;quot; &amp;gt;&amp;gt; &amp;quot;$LOG_FILE&amp;quot;&lt;br /&gt;
        byobu attach-session -t &amp;quot;$SESSION_NAME&amp;quot;&lt;br /&gt;
        return 0&lt;br /&gt;
    fi&lt;br /&gt;
&lt;br /&gt;
    byobu new-session -d -s &amp;quot;$SESSION_NAME&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    local first=yes&lt;br /&gt;
    for id_cmd in &amp;quot;${WINDOWS[@]}&amp;quot;; do&lt;br /&gt;
        local id=${id_cmd%%:*}&lt;br /&gt;
        local cmd=${id_cmd#*:}&lt;br /&gt;
&lt;br /&gt;
        if [ &amp;quot;$first&amp;quot; = yes ]; then&lt;br /&gt;
            byobu rename-window -t &amp;quot;$SESSION_NAME:0&amp;quot; &amp;quot;$id&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
            byobu new-window -t &amp;quot;$SESSION_NAME&amp;quot; -n &amp;quot;$id&amp;quot;&lt;br /&gt;
        fi&lt;br /&gt;
        byobu send-keys -t &amp;quot;${SESSION_NAME}:$id&amp;quot; &amp;quot;$cmd&amp;quot; Enter&lt;br /&gt;
&lt;br /&gt;
        first=no&lt;br /&gt;
    done&lt;br /&gt;
&lt;br /&gt;
    byobu attach-session -t &amp;quot;$SESSION_NAME&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
main() {&lt;br /&gt;
    local window_id=$(find_existing_window)&lt;br /&gt;
&lt;br /&gt;
    if [ -n &amp;quot;$window_id&amp;quot; ]; then&lt;br /&gt;
        if activate_window &amp;quot;$window_id&amp;quot;; then&lt;br /&gt;
            if byobu select-window -t &amp;quot;$SESSION_NAME:$VIFM_WINDOW_NAME&amp;quot; 2&amp;gt;/dev/null; then&lt;br /&gt;
                echo &amp;quot;Activated existing byobu terminal window and switched to vifm.&amp;quot;&lt;br /&gt;
                return 0&lt;br /&gt;
            else&lt;br /&gt;
                fail &amp;quot;Failed switching to vifm window.&amp;quot;&lt;br /&gt;
            fi&lt;br /&gt;
        else&lt;br /&gt;
            fail &amp;quot;Failed to activate existing window.&amp;quot;&lt;br /&gt;
        fi&lt;br /&gt;
    fi&lt;br /&gt;
&lt;br /&gt;
    if ! create_byobu_session; then&lt;br /&gt;
        echo &amp;quot;Failed to create a new byobu session.&amp;quot; &amp;gt;&amp;gt; &amp;quot;$LOG_FILE&amp;quot;&lt;br /&gt;
        exit 1&lt;br /&gt;
    fi&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
main&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Script execution completed at $(date)&amp;quot; &amp;gt;&amp;gt; &amp;quot;$LOG_FILE&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Source ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/vifm/vifm/issues/1061 GitHub#1061] by [https://github.com/wideweide wideweide]&lt;/div&gt;</summary>
		<author><name>Xaizek</name></author>
	</entry>
</feed>