I have a bash script that I use to configure a vanilla Ubuntu (10.10 Maverick Meerkat) installation to be exactly the way I want it. I make extensive use of gconftool-2 to configure the desktop, set up shortcut keys, etc.

Now, I'm trying to swap the CTRL and CAPS keys. I have found two ways of doing this:

  • In Gnome, go to System -> Preferences -> Keyboard -> Layout -> Options and make the change in there. This works well, but I don't know how to script this; the setting doesn't seem to be stored in the usual place as I can't find it with gconf-editor.

  • Add the line setxkbmap -option "ctrl:swapcaps" to my .bashrc file. That works too, until I suspend the machine & then resume it. At that point the CTRL and CAPS behaviour return to normal, until I cause .bashrc to be run again by opening a new shell. This behaviour has been reported as a bug in RedHat.

Could someone please suggest a way of switching those keys that is both permanent, and can be scripted? I'm sure I must be missing something obvious here ...


I have accomplished this on my RHEL system in a generic way by adding this to the beginning of `~/.xinitrc:

xmodmap ~/.xmodmaprc

And the contents of .xmodmaprc:

!! Swap Caps_Lock and Control_L
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L

There's an even quicker Gnome-only solution. Thanks to this post from gmargo over on Ubuntu Forums:

If you're running Gnome, then all you need to do is create a file $HOME/.Xkbmap with this content:

-option ctrl:swapcaps

Log out then back in.

Update: I just confirmed that the setting lasts across a suspend/resume.

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.