4

With gnome-terminal, I can use the --role command line argument to make the new terminal window uniquely identifiable by the window manager or other applications, for example:

gnome-terminal --role=irc

This sets the X window role to "irc", as displayed by xprop:

WM_WINDOW_ROLE(STRING) = "foo"

Is there a way to do the same with GVim? I have combed through gvim --help and Googled to no avail.

4

You can use xdotool to set window's WM_WINDOW_ROLE property:

$ xdotool set_window --role <ROLE_STRING> <WINDOW_ID>

You can find WINDOW_ID by PID:

$ xdotool search --onlyvisible --pid <PID>

Note that there is a harmless bug message printed in xdotool 2.x when using this method:

$ xdotool search --onlyvisible --pid 16076
Can't consume 1 args; are only 0 available. This is a bug.
23068675

You can use xdotool 3.x to get rid of this bug.

That being said, one could create a custom wrapper to start a gvim instance, wait for a window to show up and set a new role string on it. It could look like this:

#!/bin/sh

role="$1"
shift

gvim --nofork "$@" &
pid="$!"
window_id=

while true; do
    window_id=$(xdotool search --onlyvisible --pid "$pid" 2>&1 | tail -1)
    case $window_id in
    ''|*[!0-9]*) continue ;;
    *) break ;;
    esac
done

xdotool set_window --role "$role" "$window_id"

Usage:

$ ./gvim.sh FANTASTIC-NEW-ROLE <FILENAME> <OTHER-GVIM-PARAMS> <...>

This line:

window_id=$(xdotool search --onlyvisible --pid "$pid" 2>&1 | tail -1)

will work well with both xdotool 2.x and 3.x. It takes some time from starting a gvim process to window being shown so either while loop like here or sleep() is necessary. --nofork option is needed to be able to get gvim instance PID. Portable way for checking if variable contains a number is copied from this SO answer.

If you already use some kind of hook on newly created windows such as devilspie this code may not work for you. xdotool must be used with --onlyvisible because a new instance of gvim will create 2 windows but only one visible. If devilspie will, for example, move gvim window to a different workspace it will not be visible any more.

| improve this answer | |
  • This is an unfortunately complicated but none the less awesome answer! I will try it out as soon as possible – Hubro Apr 22 '15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.