The i3 FAQ has migrated to All content here is read-only.
Ask Your Question

Reverse perl matches in 'criteria' in i3 config

asked 2015-12-13 22:24:09 +0000

basletic gravatar image


I switched to i3 recently (from pekwm), to see if I could persuade myself to start to use tiling WM. So far, so good, I find i3 very configurable and maleable. If not directly, then by simple scripting and a bit of hacking.

However, one thing is bothering me ... let me explain: I regularly use VirtualBox; very often I want to close certain window inside particular virtual machine, and instinctively press the i3 'kill' keyboard shortcut $mod+Shift+q. Of course, it closes entire virtual machine ... which makes me cry and mad :)

What I need is to disable 'kill' action on windows with [instance="VirtualBox"]. I tried:

bindsym $mod+Shift+q [instance="(?!VirtualBox)"] kill

However, it doesn't do what I need ... instead, to my horror, it closes all other windows!

Is there a simple solution to this, or I must resort to the IPC (this would be overkill for such a simple operation, wouldn't it)?

Unfortunately, programming in C/C++ is over my head, but if there is a chance, I would vouch for adding such config option: 'do something to focused widow, unless window satisfies such-and-such criteria".

(Note: my question is connected to this one [How do I use an exclude regex in i3 config] here in this FAQ, so I'm not the only one in a need for such thing.)

Thanks a lot for all your answers.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2015-12-14 01:06:03 +0000

Airblader gravatar image

updated 2015-12-15 23:43:34 +0000

What you need to do is extend the match to match on the focused window AND on the negation.

i3 4.11 and older

bindsym $mod+Shift+q exec --no-startup-id i3-msg [id="$(xdotool getactivewindow)" instance="^(?!VirtualBox).*$"] kill

This requires xdotool to be installed, of course.

i3 4.12 and newer

Starting with i3 4.12 and newer, you can ditch this little workaround and instead do this:

bindsym $mod+Shift+q [con_id="__focused__" instance="^(?!VirtualBox).*$"] kill

At the time of writing this answer, 4.12 is not yet released, so building the development branch from source is required for the time being.

edit flag offensive delete link more


Thanks to your work on issue #2014 ( the workaround with `i3msg` and `xdotool` should not be needed any longer starting with the next release (current release is 4.11). Then `bindsym _ [con_id=__focused__ instance='(?!VirtualBox)'] kill` should be sufficient.

Adaephon gravatar imageAdaephon ( 2015-12-14 22:03:01 +0000 )edit

@Adaephon Dangit, I did consider that but we seem to have forgotten to document that con_id allows this special value. The other (documented) ones were simply insufficient. I'll fix this in the documentation and update my answer.

Airblader gravatar imageAirblader ( 2015-12-14 23:02:28 +0000 )edit

We did actually document it correctly, my mistake was using the currently released userguide for reference. My vacation is getting to me. Thanks for the hint!

Airblader gravatar imageAirblader ( 2015-12-14 23:07:40 +0000 )edit

Thanks Airblader and Adaephon a lot for information and possible solutions. So far I'm novice in i3, but probably I should have looked at dev branch. I'm on debian unstable, but I will not wait for offical package; instead I will try to compile 4.12 by myself. I will report here if it doesn't work.

basletic gravatar imagebasletic ( 2015-12-14 23:18:09 +0000 )edit

@basletic Instead of compiling yourself, you may want to have a look at for instructions on how to get the latest developer version of i3 as ready-made package.

Adaephon gravatar imageAdaephon ( 2015-12-15 08:19:34 +0000 )edit

answered 2015-12-15 09:34:06 +0000

basletic gravatar image

updated 2015-12-15 19:09:33 +0000

OK, it doesn't work as expected. Or I'm doing something wrong.

Let me describe what I have found out in more details (I will play with 'galculator' instead of 'VirtualBox', and simplify regex to 'match' the string instaed of 'reverse match').

1) with the config

bindsym $mod+Shift+q [instance="galculator"] kill

I get expected behaviour. I can kill only the galculator app. I cannot kill other clients (for example: terminal). And galculator is killed irrespective of currently focused window.

2) with the config

bindsym $mod+Shift+q [instance="galculator" con_id="__focused__"] kill

I can kill any currently focused client. This is not really expected. It looks like, contrary to what I expect, the 'instance' and 'con_id' are logically OR-ed, instead of AND-ed.

And I know that the 'instance' and 'class' are logically AND-ed: bindsym $mod+Shift+q [instance="galculator" class="randomstringfgfgfg"] kill wouldn't kill galculator nor anything other.

I hope what I write above makes sense ... Is it intended behaviour? Do I have to do something more? Like mark window or something?

EDIT: I forgot to specify that I now use i3 repository, with i3 version:

$ i3 -v

i3 version 4.11-134-g04be42f (2015-12-04, branch "next") © 2009 Michael Stapelberg and contributors

EDIT1: I'm a bit confused with the fact that I cannot add yet another answer (about xdotool way), so here it is, below:

As for the 'xdotool' way ... it also doesn't work. Either I'm doing something wrong, or certain features of i3 don't work as expected. (Admittedly, what I need might be a corner case ...)

To simplify things, let's go back to simple 'galculator', and let's try to do it without 'xdotool'.

1) this

bindsym $mod+Shift+q [id="31457283"] kill

where '31457283' is 'galculator' window id (as obtained by 'xwininfo -int') works as expected: it kills (only) 'galculator', no matter what window has focus.

2) however, this

bindsym $mod+Shift+q [id="31457283" instance="(?!galculator)"] kill

doesn't work as expected - it, again, kills (only) 'galculator' window, no matter what window has focus. As if the 'id' criteria cause any other criteria to be ignored, similar to 'con_id' case. Is it possible?

Therefore, the solution for original problem using 'xdotool' doesn't seem to work, either.

edit flag offensive delete link more


That does sound odd. I'm going to check it out later.

Airblader gravatar imageAirblader ( 2015-12-15 11:21:18 +0000 )edit

Yeah, you're right. con_id and mark are criteria that cause any other criteria to be ignored. I think you need to use the xdotool solution I mentioned. Let us know whether that works. Meanwhile I'll open a bug.

Airblader gravatar imageAirblader ( 2015-12-15 11:37:12 +0000 )edit

Airblader gravatar imageAirblader ( 2015-12-15 12:16:35 +0000 )edit

@basletic Sorry for the confusion. Using id+instance does work, but the negative lookahead must look slightly different. I've updated my answer accordingly. Please try it the way it's described there, i.e., bindsym $mod+Shift+q [id=31457283 instance="^(?!galculator).*$"] kill

Airblader gravatar imageAirblader ( 2015-12-15 19:47:36 +0000 )edit

@Airblader Thanks a lot. This now works as required: bindsym $mod+Shift+q exec --no-startup-id i3-msg [id="$(xdotool getactivewindow)" instance="^(?!galculator).*$"] kill (but only with this particular quoting; your inital quoting doesn't work!). I apologize for my nonfamiliarity with PCRE.

basletic gravatar imagebasletic ( 2015-12-15 20:03:11 +0000 )edit

Question Tools

1 follower


Asked: 2015-12-13 22:24:09 +0000

Seen: 106 times

Last updated: Dec 15