2018-08-28 16:48:24 +02:00
# i3-layout-manager
Saving, loading and managing layouts for i3wm.
2018-08-28 17:44:52 +02:00
2018-09-02 16:13:26 +02:00
Video:
2018-08-30 03:32:39 +02:00
[![Play video ](https://img.youtube.com/vi/Q0qlUfG_lZ0/0.jpg )](https://www.youtube.com/watch?v=Q0qlUfG_lZ0)
2018-08-30 03:28:24 +02:00
2018-09-03 00:36:33 +02:00
Features:
* saving a layout (including floating windows)
* restoring the layout on any workspace
* rearranging existing windows as well as preparing the layout for new windows
* layout management using _rofi_
2018-09-02 15:51:33 +02:00
## Preamble - don't worry, I solved all of this
2018-08-28 18:33:13 +02:00
i3 window manager supports saving and loading of window layouts, however, the features are bare-bone and partially missing.
2018-09-02 15:51:33 +02:00
According to the [manual ](https://i3wm.org/docs/layout-saving.html ), the layout tree can be exported into a JSON file.
2018-08-28 18:33:13 +02:00
The file contains a description of the containers of a workspace with prefilled (and commented) potential matching rules for the windows.
2018-09-02 15:51:33 +02:00
The user is supposed to uncomment the desired one (and modify them) and delete the unused ones.
Moreover, the user should add a surrounding root container which is missing in the file (this baffles me, why can't they save it too?).
2018-08-28 18:33:13 +02:00
2018-09-02 15:51:33 +02:00
So doing it manually (which I don't want) consists of following steps, as described at [i3wm.org ](https://i3wm.org/docs/layout-saving.html ):
1. export the workspace into JSON using ```i3-save-tree --workspace ...```
2. edit the JSON to match your desired matching rules for the windows
2018-08-28 18:33:13 +02:00
3. wrap the file in a root node, which defines the root split.
4. when needed, load the layout using ```i3-append ...```
2018-08-29 20:59:37 +02:00
However, this plan has flaws.
2018-09-02 15:51:33 +02:00
It's not scalable, it's not automated and loading a layout does not work when windows are already present in the current workspace.
2018-08-30 03:48:04 +02:00
To fix it, I built this **i3-layout-manager** .
2018-08-28 18:35:37 +02:00
Currently, its a hacky-type of a shell script, but feel free to contribute :-).
2018-08-28 18:33:13 +02:00
## How does it work?
2018-09-02 15:51:33 +02:00
1. The workspace tree is exported using ```i3-save-tree --workspace ...```
2018-08-29 22:23:20 +02:00
2. The tree for all workspaces on the currently focused monitor exported using ```i3-save-tree --output ...```
2018-08-29 20:59:37 +02:00
3. The location of the current workspace in the all-tree is found by matching the workspace-tree file on the monitor-tree file.
2018-09-02 15:51:33 +02:00
4. The parameters of the root split are extracted, and the workspace tree is wrapped in a new split.
2018-09-04 08:04:52 +02:00
5. The floating windows are extracted from within and appended behind the root split.
2018-09-03 00:36:33 +02:00
6. The user is then asked about how should the windows be matched. The options are:
2018-08-29 22:26:32 +02:00
* All by _instance_ (instance will be uncommented for all windows)
* Match any window to any placeholder
2018-09-02 16:09:50 +02:00
* Choose an option for each window. The user will be asked to choose between the _class_ , _instance_ and _title_ for each window. The tree file will be modified automatically according to the selected options.
2018-09-02 16:08:00 +02:00
![matching ](misc/choice_matching.jpg )
2018-09-03 00:36:33 +02:00
7. After that, the tree is saved and ready to be loaded.
8. The user can load the layout either before opening windows, which creates placeholders, or after, which adds the existing windows to the layout. The second part normally does not work.
9. To apply a layout, we first move all windows containing a process from the workspace using `xdotool` , which leaves only placeholders. Then we kill all the old placeholders before we apply the layout, which spawns new placeholders in the correct places. Lastly, we move the windows back, which triggers the _swallow_ mechanism in the same way, as newly create windows do.
2018-08-30 03:45:05 +02:00
2018-08-30 03:51:11 +02:00
## How to use it?
2018-08-30 03:45:05 +02:00
2018-08-30 03:53:25 +02:00
* By directly running the script
```bash
./layout_manager.sh
```
It uses *rofi* to interact with the user, no file editing or coding is required.
2018-09-02 15:51:33 +02:00
You can bind the script to an i3 key combination.
2018-08-30 03:53:25 +02:00
* The layout manager can load a layout by running
```bssh
./layout_manager.sh < layout_name >
```
2020-04-23 14:41:52 +02:00
which is useful for automation. If the `layout_name` ends with .json, the manager treats the argument as a path to the particular layout file.
2018-08-28 18:33:13 +02:00
2018-09-02 03:44:45 +02:00
## Layout files
2020-04-23 14:41:52 +02:00
The layout files are stored by default in `~/.layouts` or in `~/.config/i3-layout-manager/layouts` , depending on your `$XDG_CONFIG_HOME` .
2018-09-02 03:44:45 +02:00
2018-08-28 17:44:52 +02:00
## Dependencies
* vim/nvim
* jq
* i3
* rofi
* xdotool
* x11-xserver-utils
2020-04-23 14:41:52 +02:00
* indent, libanyevent-i3-perl
2018-08-28 17:44:52 +02:00
```bash
2020-04-22 10:38:49 +02:00
sudo apt install jq vim rofi xdotool x11-xserver-utils indent libanyevent-i3-perl
2018-08-28 17:44:52 +02:00
```
2018-08-30 03:51:11 +02:00
## FAQ
2018-09-03 00:36:33 +02:00
* **Does it work on floating windows?**
2020-04-23 16:33:37 +02:00
Yes, sometimes. Some programs behave strangely, e.g., the *Thunar file* manager fails to load into a floating place holder.
2018-09-03 00:36:33 +02:00
* **Will it run the programs for me?**
Nope. It is not intended to do that. The layout manager only automates the already built-in features of i3. Running programs is a different matter than applying layout.
* **Does it move windows across workspaces?**
No, it only affects the current workspace. However, layouts can be used on another workspace than they had been created on.
* **Why do you use vim for the automated file editing?**
2018-08-30 03:51:11 +02:00
2018-09-03 00:36:33 +02:00
Vim is great for this kind of work. A simple one-liner can do complex edits which would be difficult to program even using, e.g., python. Thanks to this, the layout manager was hacked up in a single day.
2018-08-30 03:51:11 +02:00