urxvt-background(1)

πŸ’– urxvt-background(1) β€” RXVT-UNICODE πŸ’–

okay so bestie, this is the urxvt-background(1) page from the RXVT-UNICODE manual, and it's literally all about making your terminal background serve main character energy instead of boring flat colour vibes πŸ’…βœ¨ no cap.

🌸 NAME 🌸

  • background β€” manage terminal background

✨ SYNOPSIS ✨

the whole summoning ritual looks like this:

urxvt --background-expr 'background expression'
      --background-border
      --background-interval seconds

πŸŽ€ QUICK AND DIRTY CHEAT SHEET πŸŽ€

Load a random jpeg image and tile the background with it without scaling or anything else:

load "/path/to/img.jpg"

The same, but use mirroring/reflection instead of tiling:

mirror load "/path/to/img.jpg"

Load an image and scale it to exactly fill the terminal window:

scale keep { load "/path/to/img.jpg" }

Implement pseudo-transparency by using a suitably-aligned root pixmap as window background:

rootalign root

Likewise, but keep a blurred copy:

rootalign keep { blur 10, root }

πŸ’… DESCRIPTION πŸ’…

This extension manages the terminal background by creating a picture that is behind the text, replacing the normal background colour. It does so by evaluating a Perl expression that calculates the image on the fly, for example, by grabbing the root background or loading a file.

While the full power of Perl is available, the operators have been designed to be as simple as possible.

For example, to load an image and scale it to the window size, you would use:

urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }'

Or specified as a X resource:

URxvt.background.expr: scale keep { load "/path/to/mybg.png" }

πŸ¦‹ THEORY OF OPERATION πŸ¦‹

At startup, just before the window is mapped for the first time, the expression is evaluated and must yield an image. The image is then extended as necessary to cover the whole terminal window, and is set as a background pixmap.

If the image contains an alpha channel, then it will be used as-is in visuals that support alpha channels (for example, for a compositing manager). In other visuals, the terminal background colour will be used to replace any transparency.

When the expression relies, directly or indirectly, on the window size, position, the root pixmap, or a timer, then it will be remembered. If not, then it will be removed.

If any of the parameters that the expression relies on changes (when the window is moved or resized, its position or size changes; when the root pixmap is replaced by another one the root background changes; or when the timer elapses), then the expression will be evaluated again.

For example, an expression such as scale keep { load "$HOME/mybg.png" } scales the image to the window size, so it relies on the window size and will be reevaluated each time it is changed, but not when it moves for example. That ensures that the picture always fills the terminal, even after its size changes.

🧚 EXPRESSIONS 🧚

Expressions are normal Perl expressions, in fact, they are Perl blocks β€” which means you could use multiple lines and statements:

scale keep {
   again 3600;
   if (localtime now)[6]) {
      return load "$HOME/weekday.png";
   } else {
      return load "$HOME/sunday.png";
   }
}

This inner expression is evaluated once per hour (and whenever the terminal window is resized). It sets sunday.png as background on Sundays, and weekday.png on all other days.

Fortunately, we expect that most expressions will be much simpler, with little Perl knowledge needed.

Basically, you always start with a function that "generates" an image object, such as load, which loads an image from disk, or root, which returns the root window background image:

load "$HOME/mypic.png"

The path is usually specified as a quoted string (the exact rules can be found in the perlop manpage). The $HOME at the beginning of the string is expanded to the home directory.

Then you prepend one or more modifiers or filtering expressions, such as scale:

scale load "$HOME/mypic.png"

Just like a mathematical expression with functions, you should read these expressions from right to left, as the load is evaluated first, and its result becomes the argument to the scale function.

Many operators also allow some parameters preceding the input image that modify its behaviour. For example, scale without any additional arguments scales the image to size of the terminal window. If you specify an additional argument, it uses it as a scale factor (multiply by 100 to get a percentage):

scale 2, load "$HOME/mypic.png"

This enlarges the image by a factor of 2 (200%). As you can see, scale has now two arguments, the 2 and the load expression, while load only has one argument. Arguments are separated from each other by commas.

Scale also accepts two arguments, which are then separate factors for both horizontal and vertical dimensions. For example, this halves the image width and doubles the image height:

scale 0.5, 2, load "$HOME/mypic.png"
⚠️ IF you try out these expressions, you might suffer from some sluggishness, because each time the terminal is resized, it loads the PNG image again and scales it. Scaling is usually fast (and unavoidable), but loading the image can be quite time consuming.

This is where keep comes in handy:

scale 0.5, 2, keep { load "$HOME/mypic.png" }

The keep operator executes all the statements inside the braces only once, or when it thinks the outcome might change. In other cases it returns the last value computed by the brace block.

This means that the load is only executed once, which makes it much faster, but also means that more memory is being used, because the loaded image must be kept in memory at all times. In this expression, the trade-off is likely worth it.

But back to effects: Other effects than scaling are also readily available, for example, you can tile the image to fill the whole window, instead of resizing it:

tile keep { load "$HOME/mypic.png" }

In fact, images returned by load are in tile mode by default, so the tile operator is kind of superfluous.

Another common effect is to mirror the image, so that the same edges touch:

mirror keep { load "$HOME/mypic.png" }

Another common background expression is:

rootalign root

This one first takes a snapshot of the screen background image, and then moves it to the upper left corner of the screen (as opposed to the upper left corner of the terminal window)- the result is pseudo-transparency: the image seems to be static while the window is moved around.

🌈 COLOUR SPECIFICATIONS 🌈

Whenever an operator expects a "colour", then this can be specified in one of two ways: Either as string with an X11 colour specification, such as:

"red"               # named colour
"#f00"              # simple rgb
"[50]red"           # red with 50% alpha
"TekHVC:300/50/50"  # anything goes

OR as an array reference with one, three or four components:

[0.5]               # 50% gray, 100% alpha
[0.5, 0, 0]         # dark red, no green or blur, 100% alpha
[0.5, 0, 0, 0.7]    # same with explicit 70% alpha

πŸ“ CACHING AND SENSITIVITY πŸ“

Since some operations (such as load and blur) can take a long time, caching results can be very important for a smooth operation. Caching can also be useful to reduce memory usage, though, for example, when an image is cached by load, it could be shared by multiple terminal windows running inside urxvtd.

πŸ’œ "keep { ... }" caching πŸ’œ

The most important way to cache expensive operations is to use keep { ... }. The keep operator takes a block of multiple statements enclosed by {} and keeps the return value in memory.

An expression can be "sensitive" to various external events, such as scaling or moving the window, root background changes and timers. Simply using an expression (such as scale without parameters) that depends on certain changing values (called "variables"), or using those variables directly, will make an expression sensitive to these events - for example, using scale or TW will make the expression sensitive to the terminal size, and thus to resizing events.

When such an event happens, keep will automatically trigger a reevaluation of the whole expression with the new value of the expression.

keep is most useful for expensive operations, such as blur:

rootalign keep { blur 20, root }

This makes a blurred copy of the root background once, and on subsequent calls, just root-aligns it. Since blur is usually quite slow and rootalign is quite fast, this trades extra memory (for the cached blurred pixmap) with speed (blur only needs to be redone when root changes).

⭐ "load" caching ⭐

The load operator itself does not keep images in memory, but as long as the image is still in memory, load will use the in-memory image instead of loading it freshly from disk.

That means that this expression:

keep { load "$HOME/path..." }

Not only caches the image in memory, other terminal instances that try to load it can reuse that in-memory copy.

πŸ‘‘ REFERENCE πŸ‘‘

πŸ”₯ COMMAND LINE SWITCHES πŸ”₯

  • --background-expr perl-expression β€” Specifies the Perl expression to evaluate.
  • --background-border β€” By default, the expression creates an image that fills the full window, overwriting borders and any other areas, such as the scrollbar. Specifying this flag changes the behaviour, so that the image only replaces the background of the character area.
  • --background-interval seconds β€” Since some operations in the underlying XRender extension can effectively freeze your X-server for prolonged time, this extension enforces a minimum time between updates, which is normally about 0.1 seconds. If you want to do updates more often, you can decrease this safety interval with this switch.

🌸 PROVIDERS/GENERATORS 🌸

These functions provide an image, by loading it from disk, grabbing it from the root screen or by simply generating it. They are used as starting points to get an image you can play with.

πŸ’– load $path πŸ’–

Loads the image at the given $path. The image is set to plane tiling mode.

If the image is already in memory (e.g. because another terminal instance uses it), then the in-memory copy is returned instead.

πŸ¦‹ load_uc $path πŸ¦‹

Load uncached - same as load, but does not cache the image, which means it is always loaded from the filesystem again, even if another copy of it is in memory at the time.

πŸ‘‘ root πŸ‘‘

Returns the root window pixmap, that is, hopefully, the background image of your screen.

This function makes your expression root sensitive, that means it will be reevaluated when the bg image changes.

  • solid $colour
  • solid $width, $height, $colour β€” makes a brand new image and fills it all the way with the colour you gave it, bestie. The image comes out in tiling mode. ✨ And if you skip $width and $height? It gives you a 1x1 image β€” which is lowkey perfect for solid backgrounds or for feeding into filtering effects. tiny but mighty πŸ’…
  • clone $img β€” hands you back an exact copy of the image. Super useful when you want several copies of the same pic so you can slap different effects on each one. no cap 🫢
  • merge $img ... β€” takes any number of images and merges them into one single image containing all of them. The tiling mode of the first image becomes the tiling mode of the result. Heads up: this one gets called automatically whenever an expression returns multiple images, so it's doing work behind your back (iconic behaviour honestly). πŸ‘‘

πŸŒ€ TILING MODES πŸŒ€

These next operators change an image's tiling mode β€” which is just the whole vibe of how pixels outside the image area get painted when the image is actually used. 🎨

  • tile $img β€” tiles the entire plane with the image and returns that new image. In other words (iykyk): it gives you a copy of the image in plane tiling mode.

Example: load an image and tile it over the background, no resizing involved. The tile call is actually superfluous here because load already defaults to tiling mode οΏ½οΏ½

tile load "mybg.png"
  • mirror $img β€” basically tile's cousin, but it reflects the image every time it lays down a new copy. So top edges always meet top edges, right edges always meet right edges, etc. (With normal tiling, left edges always touch right edges and top always touches bottom, fyi.) πŸͺž

Example: load an image and mirror it over the background β€” you dodge those sharp edges at the image borders, at the cost of, y'know, mirroring the image itself.

mirror load "mybg.png"
  • pad $img β€” takes an image and modifies it so every pixel outside the image area goes transparent. This mode ate when you wanna place an image over another image or over the background colour while leaving all those outside background pixels untouched. πŸ™ˆ

Example: load an image and display it in the upper left corner. All the rest of the space stays "empty" β€” transparent, or whatever your compositor does in alpha mode, otherwise background colour.

pad load "mybg.png"
  • extend $img β€” stretches the image over the whole plane using the closest pixel in the area outside the image. Mostly useful when you're doing more complex filtering ops and you want the pixels outside the image to carry the same values as the pixels near the edge. 🌈

Example: just for curiosity, how does this pixel extension stuff even work?

extend move 50, 50, load "mybg.png"

πŸ”’ VARIABLE VALUES πŸ”’

These functions hand you variable data β€” terminal window dimensions and such. Plot twist: they are not (Perl-) variables, they just return stuff that varies. Most of them make your expression sensitive to certain events β€” so for example, using TW (terminal width) means your expression gets reevaluated every time the terminal is resized. πŸ‘€

  • TX
  • TY β€” return the X and Y coordinates of the terminal window. (The terminal window is the full window by default, and just the character area when in border-respect mode.) πŸ‘‘

Using these makes your expression sensitive to window moves.

Mainly useful for aligning images to the root window, ngl.

Example: load an image and align it so it looks anchored to the background (which is literally what rootalign does btw 😭):

move -TX, -TY, keep { load "mybg.png" }
  • TW
  • TH β€” return the width (TW) and height (TH) of the terminal window. (Again: full window by default, character area only in border-respect mode.) πŸ“

Using these makes your expression sensitive to window resizes.

Mainly useful for scaling images, or clipping images down to the window size to conserve memory.

Example: grab the screen background, clip it to the window size, blur it a lil, align it to the window position, and use it as your background. πŸ’–

clip move -TX, -TY, keep { blur 5, root }
  • FOCUS β€” returns a boolean telling you whether the terminal window has keyboard focus β€” true if it does. 🎯

Using this makes your expression sensitive to focus changes.

The classic use case is fading your background image out when the terminal loses focus, often paired with the -fade command line option. And actually there's a dedicated function for exactly that: focus_fade. 🫢

Example: two completely different background images depending on whether the window has focus.

FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" }
  • now β€” returns the current time as (fractional) seconds since the epoch. ⏰

Using this one does not make your expression sensitive to time β€” but the next two functions absolutely do.

  • again $seconds β€” when you use this, the expression gets reevaluated again in $seconds seconds. πŸ”

Example: load some image and rotate it according to the time of day (like it's the hour hand of a clock). Update it every minute.

again 60;
rotate 50, 50, (now % 86400) * -72 / 8640, scale keep { load "myclock.png" }
  • counter $seconds β€” like again, but it also returns an increasing counter value starting at 0, which is lowkey handy for simple animation effects. 🎞️

πŸ¦‹ SHAPE CHANGING OPERATORS πŸ¦‹

These ones mess with the shape, size or position of your image. ✨

  • clip $img
  • clip $width, $height, $img
  • clip $x, $y, $width, $height, $img β€” clips the image to the given rectangle. If that rectangle sits outside the image area (like when $x or $y are negative) or it's bigger than the image, then the tiling mode decides how those extra pixels get filled. πŸŽ€

If $x and $y are missing, 0 is assumed for both.

If $width and $height are missing, the window size is assumed.

Example: load an image, blur it, then clip it to the window size to save memory.

clip keep { blur 10, load "mybg.png" }
  • scale $img
  • scale $size_factor, $img
  • scale $width_factor, $height_factor, $img β€” scales the image by the given factors, horizontally ($width) and vertically ($height). πŸ“

Give it only one factor and it's used for both directions.

Give it no factors and it scales the image to the window size without keeping aspect.

  • resize $width, $height, $img β€” resizes the image to exactly $width times $height pixels. Periodt. πŸ“
  • fit $img
  • fit $width, $height, $img β€” fits the image into the given $width and $height (or the terminal size) without changing aspect. So it shrinks or grows until the whole image fits inside the given area, and it might leave borders. πŸ–ΌοΈ
  • cover $img
  • cover $width, $height, $img β€” like fit, except it shrinks or grows until the whole area is covered by the image. So instead of leaving borders, it cuts off image data that doesn't fit. goodbye, edges πŸ’€
  • move $dx, $dy, $img β€” moves the image $dx pixels horizontally and $dy pixels vertically. ➑️

Example: move the image right by 20 pixels and down by 30.

move 20, 30, ...
  • align $xalign, $yalign, $img β€” aligns the image according to a factor. 0 means the image gets moved to the left or top edge (for $xalign or $yalign respectively), 0.5 means perfectly centered, and 1 means it touches the right or bottom edge. 🎯

Example: remove any visible border around an image, center it vertically, but shove it over to the right hand side.

align 1, 0.5, pad $img
  • center $img
  • center $width, $height, $img β€” centers the image, meaning the center of the image moves to the center of the terminal window (or to the center of the box you specified with $width and $height). πŸ’–

Example: load an image and center it.

center keep { pad load "mybg.png" }
  • rootalign $img β€” moves the image so it looks glued to the screen instead of the window. Gives you the illusion of a bigger area sitting behind the window. It's exactly equivalent to move -TX, -TY, i.e. it moves the image to the top left of the screen. 🌍

Example: load a background image, put it in mirror mode, and root align it.

rootalign keep { mirror load "mybg.png" }

Example: take the screen background and align it β€” giving the illusion of transparency as long as the window isn't sitting in front of other windows. πŸͺŸ

rootalign root
  • rotate $center_x, $center_y, $degrees, $img β€” rotates the image clockwise by $degrees degrees around the point at $center_x and $center_y (specified as a factor of image width/height). πŸ”„

Example: rotate the image 90 degrees around its center.

rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }

🎨 COLOUR MODIFICATIONS 🎨

These operators change the actual pixels of the image. πŸ’…

  • tint $color, $img β€” tints the image in the colour you give it. 🌸

Example: tint the image red.

tint "red", load "rgb.png"

Example: same thing, but specify the colour by component.

tint [1, 0, 0], load "rgb.png"
  • shade $factor, $img β€” shades the image by the given factor. πŸŒ—
  • contrast $factor, $img
  • contrast $r, $g, $b, $img
  • contrast $r, $g, $b, $a, $img β€” adjusts an image's contrast. πŸ”†

The first form applies one single $factor to red, green and blue; the second form applies separate factors to each colour channel; the last form also includes the alpha channel.

Values from 0 to 1 lower the contrast, values higher than 1 increase it.

⚠️ Due to limitations in the underlying XRender extension, lowering contrast also reduces brightness, while increasing contrast currently also increases brightness. So it's a package deal, bestie 😭
  • brightness $bias, $img
  • brightness $r, $g, $b, $img
  • brightness $r, $g, $b, $a, $img β€” adjusts an image's brightness. β˜€οΈ

First form: one single $bias applied to red, green and blue. Second form: separate biases per colour channel. Last form: alpha channel included too.

Values less than 0 reduce brightness, values larger than 0 increase it. Useful range is from -1 to 1 β€” the former gives you a black picture, the latter a white one. πŸ–€πŸ€

πŸ’€ Due to idiosyncrasies in the underlying XRender extension, biases less than zero can be very slow.

You can also give the experimental(!) muladd operator a try. πŸ§ͺ

  • muladd $mul, $add, $img β€” EXPERIMENTAL πŸ‘‘ β€” this one first multiplies the pixels by $mul, then adds $add. The whole point is that you can do brightness and contrast at the same time, and with a way wider value range than the plain contrast and brightness operators give you.
⚠️ but ngl, because of numerous bugs in XRender implementations, this one can also throw a bunch of visual artifacts at you. so like… vibe check your screen after, bestie πŸ’€

Example β€” bump the contrast by a factor of $c without yeeting the image brightness too far:

muladd $c, (1 - $c) * 0.5, $img
  • blur $radius, $img
  • blur $radius_horz, $radius_vert, $img β€” Gaussian-blurs the image with (roughly) $radius pixels of radius 🌸. You can also pass the two radii separately if you're feeling fancy.

Blurring is often very slow, like at least compared to other operators 😭. And bigger blur radii are slower than smaller ones, so if you don't wanna freeze your screen for a long, long time, start experimenting with low radius values (<5). fr fr, start small.

  • focus_fade $img
  • focus_fade $factor, $img
  • focus_fade $factor, $color, $img β€” fades the image by the given factor (and colour) when focus is lost πŸ’…. Same thing as the -fade/-fadecolor command line options, which also supply the default values for factor and $color. But important: unlike with -fade, $factor here is a real value, not a percentage β€” so 0..1, not 0..100.

Example β€” do the right thing when focus fading is requested 🫢:

focus_fade load "mybg.jpg";

✨ OTHER STUFF ✨

Anything that didn't fit any of the other categories, even after applying force and closing our eyes πŸ™ˆ. We tried, okay?

  • keep { ... } β€” this operator takes a code block as its argument, meaning one or more statements wrapped in braces πŸŽ€.

The trick is that this code block only gets evaluated when the outcome changes β€” on all the other calls, keep simply returns the image it computed previously (yes, it should only be used with images, iykyk). Or, in other words: keep caches the result of the code block so it doesn't need to be computed again. Iconic behaviour, honestly.

This is extremely useful for avoiding redoing slow operations. Like, say your background expression takes the root background, blurs it, and then root-aligns it β€” it would have to blur the root background on every window move or resize 😭.

Another example is load, which can be quite slow.

In fact, urxvt itself encloses the whole expression in some kind of keep block, so it only gets reevaluated as required ✨.

Putting the blur into a keep block means the blur is only done once, while the rootalign is still done each time the window moves:

rootalign keep { blur 10, root }

So this leaves the question of how to force reevaluation of the block, in case the root background changes: if the expression inside the block is sensitive to some event (root background changes, window geometry changes), then it will be reevaluated automatically as needed πŸ‘‘


πŸ¦‹ OLD BACKGROUND IMAGE SETTINGS πŸ¦‹

This extension also provides support for the old options/resources and OSC sequences for setting a background image. These settings are deprecated and will be removed in future versions πŸ’€ β€” so enjoy them while they last, but maybe don't build your whole aesthetic on them.

OPTIONS AND RESOURCES

  • -pixmap file[;oplist]
  • backgroundPixmap: file[;oplist] β€” use the specified image file as the window's background, and optionally give a colon-separated list of operations to modify it 🎨. Note that you may need to quote the ; character when using the command line option, because ; is usually a metacharacter in shells.

Supported operations are:

  • WxH+X+Y β€” sets scale and position. W / H specify the horizontal/vertical scale (percent), and X / Y locate the image centre (percent). A scale of 0 disables scaling.
  • op=tile β€” enables tiling.
  • op=keep-aspect β€” maintain the image aspect ratio when scaling.
  • op=root-align β€” use the position of the terminal window relative to the root window as the image offset, simulating a root window background.

The default scale and position setting is 100x100+50+50. Alternatively, a predefined set of templates can be used to achieve the most common setups πŸ‘‘:

  • style=tiled β€” the image is tiled with no scaling. Equivalent to 0x0+0+0:op=tile.
  • style=aspect-stretched β€” the image is scaled to fill the whole window maintaining the aspect ratio and centered. Equivalent to 100x100+50+50:op=keep-aspect.
  • style=stretched β€” the image is scaled to fill the whole window. Equivalent to 100x100.
  • style=centered β€” the image is centered with no scaling. Equivalent to 0x0+50+50.
  • style=root-tiled β€” the image is tiled with no scaling and using 'root' positioning. Equivalent to 0x0:op=tile:op=root-align.

If multiple templates are specified, the last one wins πŸ’…. Note that a template overrides all the scale, position and operation settings.

If used in conjunction with pseudo-transparency, the specified image will be blended over the transparent background using alpha-blending ✨.

  • -tr / +tr
  • transparent: boolean β€” turn on/off pseudo-transparency by using the root pixmap as background.
  • -tint colour
  • tintColor: colour β€” tint the transparent background with the given colour πŸŽ€. Note that a black tint yields a completely black image, while a white tint yields the image unchanged.
  • -sh number
  • shading: number β€” darken (0 .. 99) or lighten (101 .. 200) the transparent background. A value of 100 means no shading.
  • -blr HxV
  • blurRadius: HxV β€” apply gaussian blur with the specified radius to the transparent background 🌸. If a single number is specified, the vertical and horizontal radii are considered to be the same. Setting one of the radii to 1 and the other to a large number creates interesting effects on some backgrounds. The maximum radius value is 128. A horizontal or vertical radius of 0 disables blurring.

OSC sequences

This extension will react to the following OSC sequences πŸ¦‹. Note that this extension will not be autoloaded when these are used currently, so to make urxvt recognize them, you have to enable the background extension. One way to achieve that is to use the --background-expr '' command line argument, or by specifying an empty URxvt.background.expr: resource.

  • ESC ] 705 ; Pt ST β€” change transparent background tint colour to Pt.
  • ESC ] 20 ; Pt ST β€” change/query background image parameters. The value of Pt can be one of the following commands:
    • ? β€” display scale and position in the title.
    • ;WxH+X+Y β€” change scale and/or position.
    • FILE;WxH+X+Y β€” change background image.

and that's the whole page bestie, you made it to the end πŸŽ‰πŸ«Ά this one ships as version 9.31, dated 2023-12-02 β€” go forth and make your terminal gorgeous, mwah πŸ’…πŸ’–

πŸ“¦ where this page comes from