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.