2

I have seen many questions about how to remap a key, but somehow my problem is that it never remaps permanently.

Here is my ~/bootstrap-custom.sh

#!/bin/sh
# remaps Caps Lock to Ctrl
/usr/bin/setxkbmap -option "ctrl:nocaps"

I have added source ~/boostrap-custom.sh to my startup applications.

The mappings work fine.

However:

  • The mapping doesn't load on startup, or is at least overwritten by something that runs after the startup applications.
  • everytime I plug in my keyboard, the mappings seems to be forgotten.

Any way to make the mapping permanent, but keeping the same method (xkbmap) ?

  • Keep your rep, the answer is over on unix and linux – Foosh Jan 15 '15 at 20:15
  • 1
    You might not want the rep, but I’m pretty sure there’s no way for edi9999 to get it back. – Daniel H Jan 16 '15 at 5:23
  • @Foosh that's not the whole answer to my question, the script gets overwritten on startup too – edi9999 Jan 16 '15 at 14:38
  • @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 Jan 21 '15 at 18:02
  • 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 Jan 22 '15 at 3:59
1
+100

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.

  1. Create a file named .xsession and place it in your home directory.
  2. Add the following to it:
    #!/bin/bash
    /usr/bin/setxkbmap -option "ctrl:nocaps"
  3. Save, and set the file to mode 755 (chmod 755 ~/.xsession)
  4. 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.)

  • Thanks for answering. Sadly this didn't work out: Here's my results: ➜ ~ ll .xsession -rwxr-xr-x 1 edgar edgar 53 Jan 22 08:03 .xsession ➜ ~ cat .xsession #!/bin/bash /usr/bin/setxkbmap -option "ctrl:nocaps" However, the keyboard Caps Lock was still doing the same thing – edi9999 Jan 22 '15 at 7:09
  • Ok, it seems to work now, I had an error in my .xsession-errors that was probably causing .xsession not to execute. It works now. Thanks a lot – edi9999 Jan 22 '15 at 7:20
0

You could try to put the shell code in your .profile file, OR the more complicated version is that you use XKB. Here is a quick overview.

For complete overview try this arch wiki article.

  • Putting the shell script in .profile doesn't change anything (still not working after a reboot). I'm already using xkb, so I don't understand why you say that – edi9999 Jan 20 '15 at 14:10
0

Apparently this is a bug.

"I use xfce in arch linux and what I do is edit the ~/.config/xfce4/panel/xkb-plugin-##.rc and add a new line or something and it works for some sessions. Another temporary fix may be what #31 suggests.

  1. Set the desired options in the plugin
  2. Copy the ~/.config/xfce4/panel/xkb-plugin-##.rc to some other file e.g. ~/.config/xfce4/panel/goodxkb.rc
  3. Add this command to a startup job:

    sh -c "cp ~/.config/xfce4/panel/goodxkb.rc ~/.config/xfce4/panel/xkb-plugin-##.rc && pkill xkb" (change the ## to your number)

The pkill part is crucial to reload the plugin and its config."

From https://askubuntu.com/questions/66096/how-to-set-up-xfce4-xkb-plugin-to-remember-settings-over-reboots

  • I don't think they is much in common with my question. The answer t your question iis whe you use the keyboard settings from xubuntu to change the keyboard layout (eg EN,US,FR,...). These options can't be set from the settings. So I don't see how this could help – edi9999 Jan 20 '15 at 14:40
-1

Edit /etc/rc.local and add your path of your script. Make sure is executable and it has the #!/bin/bash at the top. Do not reboot your computer shut it down and switch it on again. This should work.

  • Sorry, this doesn't work either. – edi9999 Jan 21 '15 at 14:56
  • Edit /etc/rc.local and add your path of your script. Make sure is executable and it has the #!/bin/bash at the top. – root-expert Jan 21 '15 at 15:13
  • setxkbmap affects the X11 session it is executed within. The man page even gives the name of the tool as setxkbmap - set the keyboard using the X Keyboard Extension. Since /etc/rc.local does not run from within a X session on any Linux distribution I can imagine, nothing run from within /etc/rc.local will affect a running X11. – a CVn Jan 21 '15 at 17:38

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.