Configure Zathura with Xresources

Wed, Feb 03 14:22


The .Xresources file is great. It is a centralized place for you to define color themes and fonts among other things to be read by a variety of applications. This allows for a consistent theme, and less hassle when changing settings. Everyone knows that repetitive code is bad code.

However, for whatever reason, zathura, my pdf viewer of choice, does not want to read from this file. The effect I wanted to achieve was to make zathura have the same background color as my terminal, like so:

Obviously we aren't going to give up over such a small obstacle. It's a slight hassle, but we could simply have a shell script read from Xresource and write into the zathura config file. The only downside is that the script needs to be run everytime you want to modify the config.

First, locate your zathura config file. By default, it should be located at $HOME/.config/zathura/zathurarc. So this is the file we want to write our new config to.

Now to read from Xresource, we could just use sed or grep and find the line we want and format it so we get the value. However, if you used macros, this might not play out too well. Thankfully, X gives us a handy command to query our Xresource settings, namely xrdb -query. The output of this command looks something like this:

*.background:   #2E3440
*.color0:       #3B4252
*.color1:       #BF616A
*.color10:      #A3BE8C
*.color11:      #EBCB8B
*.color12:      #81A1C1
*.color13:      #B48EAD
*.color14:      #8FBCBB
*.color15:      #ECEFF4
*.color2:       #A3BE8C
*.color3:       #EBCB8B
*.color4:       #81A1C1
*.color5:       #B48EAD
*.color6:       #88C0D0
*.color7:       #E5E9F0
*.color8:       #4C566A
*.color9:       #BF616A

So now all we need to do is grep for the option we want, and use awk to grab the second column.

I made a function to query X like so:

get_option() {
    xrdb -query | grep $1 | awk '{print $2}'
}

And now we are ready to write to our config file. Since our config will most likely be multiline, we can use heredocs:

cat > "$HOME/.config/zathura/zathurarc" <<- CONF
    set recolor
    set recolor-darkcolor "$(get_option foreground)"
    set recolor-lightcolor "$(get_option *.color0:)"
    set default-bg "$(get_option *.color0:)"
    map i recolor
CONF

Note that using <<- ignores leading tabs while << does not, just for aesthetic. Notice how we used command substitution to call the function we wrote earlier.

Finally, chmod your shell script and run it every time you want to make a change to your config. Since we are overwriting the zathura config, you want to define your options inside the body of the heredoc instead. The example config I gave provides the background color behavior I described earlier. Specifically, we actually redefine the colors zathura's 'invert color' mode uses. This way, we can toggle between the original pdf color and our colors with the 'i' key.

And that's it! We are done, shell script to the rescue.