This might be only a partial solution (I honestly don't know how it reacts to keyboard unplugging/replugging), but since you already have a script that does what you want, this might be close enough.
On startup, X11 executes several special programs, if they are present on the system. For our purposes, the most interesting is likely ~/.xsession, which is executed after you have logged in through a display manager, which is the normal setup for a graphical environment these days. (It used to be that you normally logged in to a shell and then ran the command startx
to start X; if so, you used ~/.xinitrc for the same purpose.)
We can leverage this to execute commands virtually regardless of what display manager (gdm, gdm3, kdm, ...) and desktop environment (GNOME, Xfce, KDE, ...) you are using.
- Create a file named
.xsession
and place it in your home directory.
- Add the following to it:
#!/bin/bash
/usr/bin/setxkbmap -option "ctrl:nocaps"
- Save, and set the file to mode 755 (
chmod 755 ~/.xsession
)
- Log out and back on, or reboot
This should set your keyboard mapping on login. It should also execute late enough that setxkbmap actually works.
If for any reason this fails to work, log in to a text terminal (Ctrl+Alt+F2 for any F2 in [F1..F6] should be your friend) and delete the file by issuing the command rm ~/.xsession
.
For reference, here is my ~/.xsession, which with mode 755 drops me into a Xfce4 session (this can serve as a "known good" example):
#!/bin/sh
xscreensaver &
exec xfce4-session
The exec
directive at the end replaces the script itself with the given command. Anything that does not exit more or less immediately will need to be backgrounded, hence the &
. (I expect setxkbmap to exit almost immediately, hence no backgrounding of it needed.)
Keep your rep, the answer is over on unix and linux
– Foosh – 2015-01-15T20:15:23.6301You might not want the rep, but I’m pretty sure there’s no way for edi9999 to get it back. – Daniel H – 2015-01-16T05:23:42.647
@Foosh that's not the whole answer to my question, the script gets overwritten on startup too – edi9999 – 2015-01-16T14:38:17.837
@DanielH is right, bounties are non-refundable. Once you finish setting the bounty, there's no way to get that rep back, even if it doesn't result in any answers at all. – a CVn – 2015-01-21T18:02:43.917
Or, in the case with a question I have open, I did more research in the remaining week and answered the question in a way that would earn my bounty if somebody else answered it. Too bad I can’t claim my own bounty. – Daniel H – 2015-01-22T03:59:53.773