Awesome Window Manager: Part 3

Awesome Window Manager: Part 3
Share on:

In part two of the “Awesome Window Manager Guide”, we looked at:

  • Editing the default Awesome configuration file in the “Home” folder,
  • setting the default terminal emulator and text editor,
  • installing a third-party theme named awesome-copycats/powerarrow-dark
  • performing tweaks to the theme code.

Continuing in part three, we’ll look at adding a “run launcher” called “Rofi” and “hotkey daemon” called “sxhkd”. We’ll also create an “AutoStart” script, which will allow us to launch applications, scripts and systray applets when Awesome starts up.


Simple X Hotkey Daemon

I normally leave the hotkeys for controlling my window layouts to those set in rc.lua. However, for launching apps and scripts, the two apps mentioned above are easier to configure when used in combination with each other and Awesome.

“Simple X Hotkey Daemon” or “sxhkd”, is a tool which reads input events from your keyboard and executes commands associated to those key presses. Using “sxhkd” will make configuring your hotkeys for launching apps far easier than having to edit your rc.lua config file to add key binds in “Lua” syntax. Plus, it can be used with any window manager you may decide to switch to in future.

Installing sxhkd and Rofi

We’ll first install “sxhkd”, and “Rofi”. Then create a hotkey to launch Rofi.

Open a terminal and type:

sudo pacman -S sxhkd rofi

sxhkd Configuration

In order for sxhkd to work, it must be run when Awesome starts up. It will then read from a config file in ~/.config/sxhkd/sxhkdrc for the key bindings you’ve set to launch apps or perform actions when button combinations are pressed on your keyboard.

First let’s create the config file and open it in a text editor.

In the terminal type:

mkdir ~/.config/sxhkd
touch ~/.config/sxhkd/sxhkdrc
mousepad ~/.config/sxhkd/sxhkdrc

Since we’ve only just created the sxhkdrc file, it will be empty, so you’ll need to enter a few key commands.

Keybinding

Each time you make edits to the sxhkdrc config file, you’ll need to restart the daemon for it to read any new changes you’ve made. Thankfully, we can set a hotkey for doing that right inside the sxhkdrc config file itself.

At the top of the sxhkdrc config file enter:

####################
# SXHKD CONFIG
####################

# Restart sxhkd hotkey daemon
super + shift + x
  pkill -USR1 -x sxhkd && notify-send "sxhkd config" "Reloaded."

As you can see, the syntax for defining hotkeys is very simple.

  • The first line beginning with a # is a comment or title for the app or script you wish to run.
  • The second line is the key combination you wish to use.
  • The third line (indented with 4 spaces) is the command you wish to run.
# This is a comment
key combinations go here
    command goes here

You can now start adding some useful keys to the sxhkdrc file.

On a new line in your sxhkdrc enter:

####################
# APPS
####################

# Thunar Filemanager
super + t
   thunar

# Thunderbird
super + alt + t
   thunderbird

Key Concatenation

In the above example we added two key binds for the letter t, to launch two apps, “Thunar” and “Thunderbird”. However, in sxhkdrc, you can concatenate key bindings on the same line to perform two different actions. Linux users hate “bloat”, so by using this feature you can reduce the size of the sxhkdrc file. This can also help you keep track of keys you’re already using without accidentally creating duplicates further down in your config file.

For example, if you wanted Super + t to launch Thunar, and Super + Alt + t to launch Thunderbird, you can do it like this:

# Thunar & Thunderbird			
super + {_,alt + }t
	{thunar, thunderbird}

Pretty cool huh?! This is another reason why I use sxhkd, instead of adding all my app hotkeys into the rc.lua file. Using sxhkd, we avoid adding unnecessary levels of complexity and bloat into the Awesome configuration, which would make things harder to manage in future.

Launching Rofi with sxhkd

In order to use Rofi, we’ll obviously need to define a hotkey to launch it. To do this, on a new line, add the following to your sxhkdrc file:

####################
# Rofi
####################

# Rofi Launcher
super + space
    rofi -show drun -show-icons

🔥 IMPORTANT: The rc.lua config file has a hotkey set to switch window layouts already mapped to Super + Space, so you’ll need to comment this out in order for sxhkd to be able to use those keys. If you don’t do this, then the Awesome hotkeys will take priority over sxhkd, blocking it from working.

Edit rc.lua, go to line 411 and comment out the “select next” function with double-dashes:

--awful.key({modkey,         }, "space", function () awful.layout.inc ( 1)      end,
--         {description = "select next", group = "layout"}),

It would be a good idea to check both your rc.lua and sxhkdrc file for conflicting hotkeys. In the event that a hot key does conflict with what you wish to use in either configuration, it’s up to you to comment out or remap them to something else.

Media Keys

Most keyboards come with media buttons on them which allow you to control your audio. While these keys are recognized in a desktop environment such as Xfce and KDE, they may not be bound to anything by default in Awesome, so you may have to define them in sxhkdrc as shown below.

If you don’t have your media keys set in your rc.lua file, you can add them to your sxhkdrc like this:

####################
# Media Keys
####################

# Pulse Audio Volume Control
super + p
    pavucontrol

# Pulse Audio Master Volume
super + {Down,Up}
    amixer set Master {5%-,5%+}

# Speaker Volume
XF86Audio{Mute,Play,LowerVolume,RaiseVolume}
    amixer set Master {toggle,toggle,5%-,5%+}

# Mute Mic Audio
XF86AudioMicMute
    amixer set Capture toggle

Screenshot Functionality

People who use a standard desktop like Xfce or KDE will be familiar with the ability to easily take screenshots by pressing the PrtScn key. However, since we are only using a minimal window manager like Awesome, those extra features that a normal desktop environment provide will be missing by default.

We can add a key bind to sxhkdrc, so that we can take screenshots of the desktop or a specific window and save it as an image. Once defined, we can press the Print Screen button and left-click or drag a box-selection over the area/window we wish to snapshot.

If you haven’t already, create a folder named $HOME/Pictures/screenshots/ and then install “maim” and “xclip”.

mkdir ~/Pictures/screenshots
sudo pacman -S maim xclip

Then, in the sxhkdrc file, on a new line enter this:

####################
# SCREEN SNAPSHOTS
####################

# Print Screen
Print
    maim -s --hidecursor | tee ~/Pictures/screenshots/$(whoami)_screenshot_$(date +%Y_%m_%d_%H-%M-%S).jpg | xclip -selection clipboard -t image/jpg 

Sharing Screenshots via Dropbox

You can define a location for where a screenshot file will go. I often use a shared Dropbox folder to share gaming or desktop screenshots with a friend.

You can do it by adding this line to your sxhkdrc:

# Print Screen To Dropbox
ctrl + Print
    maim -s --hidecursor | tee ~/Dropbox/Shared_Folder/screenshots/$(whoami)_screenshot_$(date +%Y_%m_%d_%H-%M-%S).jpg | xclip -selection clipboard -t image/jpg 

After you’ve made your edits to your config files, save your changes.


Autostart Script

When you log in to Awesome, you’ll notice that apps which usually start up when you log in to Xfce are missing. There’s no network connection icon, clipboard manager, and you may be missing other applets from your systray.

Since most of those applets are programmed to work with the Xfce desktop, they may not work with AwesomeWM. Therefore, we’ll need to use “desktop-agnostic” versions of these apps, and create a script to run them from within Awesome.

Create AutoStart Script

First, create a new file:

mkdir -p ~/.scripts/awesome
touch ~/.scripts/awesome/autostart.sh

We can use this script to load the sxhkd hotkey daemon and any other apps/applets when Awesome starts.

With your text editor, navigate to the newly created ~/.scripts/awesome/autostart.sh file and open it. Then add the following code, along with any applications you wish to run when Awesome loads or is reset.

Here is my autostart.sh script as an example:

#!/usr/bin/env bash
# .scripts/awesome/autostart.sh
# Launch Apps when AwesomeWM starts.

function run {
  if ! pgrep -f $1 ;
  then
    $@&
  fi
}

# List the apps you wish to run on startup below preceded with "run"

# Policy kit (needed for GUI apps to ask for password)
run /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 &
# xrandr layout for AwesomeWM
run ~/.scripts/awesome/awesome_display_layout.sh &
# Start compositor
run picom --experimental-backend &
# sxhkd Hotkeys
run sxhkd &
# Start Volume Control applet
run volctl &
# Start Network Manager Applet 
run nm-applet &
# Set Numlock key to active.
run numlockx &
# Start Guake terminal  
run guake &
# Screensaver
run xscreensaver -no-splash &
# Greenclip for Rofi
run greenclip daemon &
# Pamac system update notifications
run pamac-tray &
# Start Dropbox
run dropbox &
# Bluetooth
run blueman-tray &
# MPD
run mpd ~/.config/mpd/mpd.conf &
# Unclutter - (hides mouse pointer after 5 seconds of inactivity)
run unclutter &

Running The Autostart Script With Awesome

Now that you’ve got a script containing all the apps you wish to run, in order for Awesome to run those apps, you’ll need to add a line of code at the bottom of your rc.lua configuration file.

Firstly, ensure that you’ve set execute permissions for your autostart.sh file, else it’ll not be executed.

chmod +x ~/.scripts/awesome/autostart.sh

Edit your rc.lua file and add the following at the very bottom:

-- Autostart Apps
awful.spawn.with_shell("~/.scripts/awesome/autostart.sh")

Once all this is complete, save your changes, log out of Awesome by pressing Super + Shift + q. After you log back in to Awesome, it will load what you’ve added into the autostart.sh file.


Rofi

Awesome has a built-in “run launcher”, which you can access by pressing Super + r. From this, you can launch apps by simply typing their name and pressing Return. Sadly, it’s pretty basic, therefore, when it comes to launching apps or scripts in Awesome - I use “Rofi”.

Rofi presents you with a searchable list to launch apps by typing their names.

Rofi presents you with a searchable list to launch apps by typing their names.

Launching Rofi

Earlier we defined a hotkey in the sxhkdrc config file to launch Rofi. You should now be able to press Super + Space to run it. To launch an app using Rofi, simply type the app name in the Rofi window. You’ll notice that this is a much quicker way to load apps rather than having to navigate menus or left-click icons.

Rofi Fonts

If you wish to change the font or additional settings which Rofi uses when it displays its list, first (if it exists) remove the ~/.config/rofi/config file, and create a new configuration file:

rm ~/.config/rofi/config
rofi -dump-config > ~/.config/rofi/config.rasi

You can then edit this file and enable features by removing the /* and */ from the beginning and end of each line.

Example: To change the font which Rofi uses to “Ubuntu 14”, we can edit line 6 like this:

font: "Ubuntu 14";

Rofi Themes

You can change the way Rofi looks by using its theme selector.

  • Launch Rofi with your Rofi hotkey, Super + Space
  • While in Rofi, press Ctrl + Tab until the prompt says run:
  • Type rofi and select rofi-theme-selector from the list by pressing Return.
  • Use your arrow keys to navigate through the Rofi themes and press Return to enable them.
  • Once you are happy with your chosen theme, press Alt + a to accept it.

The next time you launch Rofi, it will use the theme you set.


This concludes this part of the AwesomeWM guide. In the next parts of the Awesome guide, we’ll look at setting screen resolutions using “arandr”, adding wallpapers and using the “picom” compositor.