Learn how to show sway scratchpad windows in reverse order using a simple script and a key binding. This will help you access the most recent hidden window easily.
Table of Contents
Sway is a popular tiling window manager for Wayland, which allows you to manage your windows efficiently using keyboard shortcuts. One of the features of sway is the scratchpad, which lets you hide and show windows that you don’t need at the moment. However, by default, the scratchpad shows the windows in the order they were hidden, which means that the last hidden window is the last one to be shown. This can be inconvenient if you want to access the most recent hidden window quickly. In this article, we will show you how to change the behavior of the scratchpad to show the windows in reverse order, using a simple script and a key binding.
What is the problem with the default scratchpad behavior?
The default scratchpad behavior in sway is to show the windows in the order they were hidden, which is also known as FIFO (first in, first out). This means that if you hide window A, then window B, then window C, the scratchpad will show them in the same order: A, B, C. However, this may not be what you want, especially if you have many windows in the scratchpad. For example, if you hide a terminal window that you need to use frequently, you may have to cycle through all the other windows in the scratchpad before you can access it again. This can be annoying and time-consuming.
How to show sway scratchpad windows in reverse order?
To show sway scratchpad windows in reverse order, you need to use a script that will reverse the order of the windows in the scratchpad, and then bind a key to run the script. The script we will use is based on the one posted by u/PAI5REECHO, with some modifications. The script works by getting the list of windows in the scratchpad, reversing it, and then moving each window to the scratchpad again. This will effectively change the order of the windows in the scratchpad to LIFO (last in, first out). This means that if you hide window A, then window B, then window C, the scratchpad will show them in reverse order: C, B, A.
Step 1: Create the script
Create a file named scratchpad_reverse.sh in your home directory, and paste the following code into it:
#!/bin/bash # Get the list of windows in the scratchpad scratchpad_windows=$(swaymsg -t get_tree | jq -r '.. | select(.type?) | select(.type=="con") | select(.scratchpad_state!="none") | .id') # Reverse the order of the windows scratchpad_windows=$(echo "$scratchpad_windows" | tac) # Move each window to the scratchpad again for window in $scratchpad_windows; do swaymsg "[con_id=$window]" move scratchpad done
Make the script executable by running the following command in a terminal:
chmod +x ~/scratchpad_reverse.sh
Step 2: Bind a key to run the script
Open your sway configuration file, which is usually located at ~/.config/sway/config, and add the following line at the end:
bindsym $mod+Shift+grave exec ~/scratchpad_reverse.sh
This will bind the key combination of Mod+Shift+` (grave accent) to run the script. You can change the key binding to whatever you prefer, as long as it does not conflict with any existing ones.
Step 3: Reload sway and test the script
To apply the changes, reload sway by pressing Mod+Shift+C. Then, test the script by hiding and showing some windows using the scratchpad. You should see that the scratchpad now shows the windows in reverse order, which means that the last hidden window is the first one to be shown.
Frequently Asked Questions (FAQs)
Question: What is sway?
Answer: Sway is a tiling window manager for Wayland, which is a protocol for managing graphical interfaces on Linux. Sway is inspired by i3, another popular tiling window manager for X11, which is the older protocol for managing graphical interfaces on Linux. Sway aims to be compatible with i3, but also offers some advantages over it, such as better performance, security, and compatibility with modern technologies.
Question: What is a scratchpad?
Answer: A scratchpad is a feature of some window managers, such as sway and i3, that allows you to hide and show windows that you don’t need at the moment. You can think of it as a temporary storage for windows that you want to access later. The scratchpad is useful for decluttering your workspace and keeping your focus on the windows that you are currently working on.
Question: How do I hide and show windows using the scratchpad?
Answer: To hide a window using the scratchpad, you can press Mod+Shift+minus. To show a window from the scratchpad, you can press Mod+minus. You can also use the mouse to drag a window to the scratchpad icon on the status bar, or to click on the icon to show a window from the scratchpad.
In this article, we showed you how to show sway scratchpad windows in reverse order, using a simple script and a key binding. This will help you access the most recent hidden window easily, without having to cycle through all the other windows in the scratchpad. We hope you found this article helpful and informative. If you have any questions or feedback, please leave a comment below.
Disclaimer: This article is for informational purposes only and does not constitute professional advice. We are not affiliated with sway or any of the tools or websites mentioned in this article. Please use the information at your own risk and discretion.