The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
2

auto sort workspace number

asked 2014-03-27 15:47:50 +0000

Folosse gravatar image

Hello,

I'm a very happy user of i3 but I have a question that I did not find any answer.

I want i3 to automatically sort the workspace number when I close one in a interval of 2 or more.

For example, I'm using workspace 1, 2, 3 and 4. I close all the windows on workspace 3. I now have workspace 1, 2 and 4 active.

What I want is workspace 1, 2 and 3. Workspace 4 can be renamed Workspace 3 or everything in workspace 4 is now in workspace 3.

How can I achieve that ?

Thank you very much !

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-03-30 18:44:29 +0000

TonyC gravatar image

You can do this with the new ipc library. This script uses the JavaScript version but the same API is available in many languages. You can find the latest version of this script here.

#!/usr/bin/env gjs

/*
   This example shows how to renumber your workspaces so they are consolidated
   into a harmonious linear order starting from 1 automatically when a
   workspace is closed. For instance, if you have open workspaces 1, 2, 4 they
   will be renumbered to 1, 2, 3 respectively when you move out of the
   workspace with no containers.

   https://faq.i3wm.org/question/3579/auto-sort-workspace-number/
*/

const i3ipc = imports.gi.i3ipc;

let conn = new i3ipc.Connection;

conn.on('workspace::empty', function(conn, e) {
    let workspaces = conn.get_workspaces().filter(function(ws) {
        return !isNaN(parseInt(ws.name));
    }).sort(function(a, b) {
        return parseInt(a.name) - parseInt(b.name);
    });

    workspaces.forEach(function(ws, i) {
        let new_name = ws.name.replace(parseInt(ws.name).toString(), (i + 1).toString());
        conn.command('rename workspace "' + ws.name + '" to "' + new_name + '"');
    });
});

conn.main();
edit flag offensive delete link more
0

answered 2014-04-01 04:37:35 +0000

Okay, I just solved this problem for myself since the previous answerer's script doesn't seem to want to work for me. I used the golang i3ipc library. You can find that library here: http://www(dot)github.com/proxypoke/i...

I prefer to use the spelled out names for the workspaces, i.e. "one", "two", "three", etc., but you should change this in the script to whatever your preference is. It is as easy as modifying those strings in the nums[10] array. I am NOT much of a Go programmer, as I merely cobbled together enough of an understanding of the language to accomplish this feat. If someone would like to reply with a more idiomatic 'go-ified' version of my script, feel free. Anyway, here's the script I made, called arrange_ws.go:


package main

import (
    "fmt"
    "sort"
    "github.com/proxypoke/i3ipc"
)
func main() {
    ipc, _  := i3ipc.GetIPCSocket()
    defer  ipc.Close()
    var nums [10]string
    nums[0] = "zero"
    nums[1] = "one"
    nums[2] = "two"
    nums[3] = "three"
    nums[4] = "four"
    nums[5] = "five"
    nums[6] = "six"
    nums[7] = "seven"
    nums[8] = "eight"
    nums[9] = "nine"
    ws_events, _ := i3ipc.Subscribe(i3ipc.I3WorkspaceEvent)
    for {
        event := <-ws_events
        if event.Change != "" {
            var wknums [10]int
            for i := range wknums {
                wknums[i] = 0xDEADBEEF
            }
            wkspcs, _ := ipc.GetWorkspaces()
            for i := range wkspcs {
                for j := range nums {
                    if wkspcs[i].Name == nums[j] {
                        wknums[i] = i
                    }
                }
            }
            sort.Ints(wknums[0:9])
            for i := range wkspcs {
                if wkspcs[i].Name != nums[i]  {
                        command := fmt.Sprintf("rename workspace %s to %s", wkspcs[i].Name, nums[i])
                        fmt.Println(command)
                        ipc.Command(command)
                }
            }
        }
    }
}
 
edit flag offensive delete link more

Comments

What was it about the other script that did not work for you?

TonyC gravatar imageTonyC ( 2014-04-01 08:10:56 +0000 )edit

I don't think it's actually a problem with the script per se, but after I installed all the dependencies, when I run the script, it complains about a missing Typelib file, and that causes me to be unable to import i3ipc. I'm not sure what the problem is because I am sure I have the dependencies...

char_star gravatar imagechar_star ( 2014-04-01 14:51:16 +0000 )edit

It's a little tricky to install right now because it's a C library. Ideally someone would host a package for your distro.

TonyC gravatar imageTonyC ( 2014-04-01 15:24:30 +0000 )edit

Excuse the dumb question, but where do you put/call this script for it to work?

drosehill gravatar imagedrosehill ( 2014-10-01 01:04:45 +0000 )edit

Question Tools

Stats

Asked: 2014-03-27 15:47:50 +0000

Seen: 352 times

Last updated: Apr 01 '14