I3 Workspace Chooser
Creating an workspace chooser for I3
After working with I3 I’ve got a few window layouts I like for various tasks.
This script is available on my Github.
I’m a creature of habit, and I like to have my window layouts in a consistent layout. I have one layout for working on this blog, two columns. The first column has VIM, a free terminal, and hugo server, and the second column is a browser window showing a preview of my blog as I update it. Another layout I’ve been using while writing the Project Euler posts, which is similar to the blog layout, but one less terminal window on the left, and all of the terminals are pointed to a different directory.
Here you can see my blogging layout, as well as my Workspace chooser.
I’m already using rofi for a program launcher, so I wanted to use it’s dmenu replacement mode to display a list of predefined workspaces, and run a script based on the selection.
Now, when I want to setup a new workspace, I can open each of the windows I want and arrange them to my liking, then use i3-save-tree to save the layout to a JSON file and modifying it , then I create a shell script with a series of i3-msg commands to launch various programs. The issue I ran into however was I wanted the terminals to open in specific directories, and run a program, but then to remain open after the program (i.e. VIM) had closed. In order to accomplish this I had to add a small tweak to my shell’s rc file.
This snippet tells your shell that if the first argument is
eval, to execute the following arguments. This allowed me to build up a command in my workspace shell script below to start a program in a specific target directory.
i3-msg telling it the workspace context (WorkSpaceName), and telling i3 to execute a command. The command being executed is
i3-sensible-terminal which launches my terminal
urxvt with the
-e option, which runs another command. That command is my shell
zsh with the
-s options. The
-i option forces the shell to be interactive, and the
-s reads the input from the command line. The command being run is
eval which the snippet from above tells the shell to execute the following arguments, in this case a
cd to a directory and then a program name.
This combined with the
i3-msg append_layout command to restore a
i3-save-tree JSON layout to a workspace can give us the ability to restore a complete workspace. So I wrote a script that will look for the JSON layout files, format them for rofi, then execute the associated shell script with the layout chosen.
This combined with your JSON and shell script files will show you a rofi menu, allowing you to select the workspace you want to launch, then launch the layout on the workspace defined in the shell script. Below is a sample JSON and shell script that will launch two terminals, one running vim and one open and a web browser on workspace 9. You will notice in the shell scripts, a short sleep between opening similar windows (i.e. two terminals). This is to prevent the terminals from opening in the wrong order.