I am a heavy Vim user and one of the first things I do when I start working on a clean ubuntu installation is to swap the caps lock and esc keys to reduce hand movements over the keyboard.

So far I have been doing it through the keyboard configuration dialog, but I am now writting a set of scripts that will set Ubuntu up the way I like and I have not been able to find a scriptable or programmable way to change this configuration item.

I am not looking for options that swap the keys at any other level than gnome itself, so what I am looking for is for a way to script this change and have it appear in the keyboard configuration that I used to use, should I decide to revert it manually in the future.

  • nice idea! i've been complaining all the time about moving my hand for Esc while working in Vim, but now this is the solution! thanx bro ;-) – sazary Apr 19 '11 at 8:17
  • 1
    Be careful, though, once you get used to having them swapped, it gets awkward when you have to work on anybody else's keyboard :) – Jacobo de Vera Apr 19 '11 at 8:22
  • 2
    Another option is Ctrl-C instead of Esc. (Works on all keyboards. – Eric Wilson Apr 19 '11 at 13:17
  • @FarmBoy, awesome! I didn't know that one! – Jacobo de Vera Apr 19 '11 at 13:33

You can do this through the GUI by open the Keyboard Preferences control panel (under System -> Preferences), and select the Layout tab. Click on the Options... button to open the layout options dialog. Expand the Caps Lock key behaviour section and select Swap ESC and Caps Lock.

There are a few ways you could script this kind of thing.

Directly via Xkb

We can make the change directly with the following:

setxkbmap -option caps:swapescape

You can disable all the current layout options (which will return caps lock to its default behaviour) with:

setxkbmap -option ''

Via GConf

The keyboard preferences control panel stores its configuration via gconf, with the layout actually being applied by gnome-settings-daemon. Therefore, you can cause gnome-settings-daemon to adjust the layout by updating gconf yourself.

The relevant setting in this case appears to be /desktop/gnome/peripherals/keyboard/kbd/options. So you can set the option with:

gconftool-2 --set /desktop/gnome/peripherals/keyboard/kbd/options \
    --type list --list-type string \

In the above, <tab> should be a literal tab character rather than spaces. You can disable the behaviour again by setting the gconf key to an empty list.

| improve this answer | |
  • The gconf option is exactly what I was looking for, however your configuration was turning the caps lock into an escape key and the escape key into a tab key. I changed the setting manually again and took a look at the value in gconf-editor, it was actually [caps<tab>caps:swapescape]. Everything works now, I'm accepting your answer and suggest that you edit it to change the value for future reference. Thanks. – Jacobo de Vera Apr 19 '11 at 9:27
  • @Jacobo In future, you may suggest the edit yourself - just hit the edit button below the answer. Your change will quickly be approved by someone. I've made the edit for you (check if it's right). Anyway, nice answer @James! +1 – Stefano Palazzo Apr 19 '11 at 11:19
  • 1
    @Stefano, thank you, I hadn't noticed the edit option. – Jacobo de Vera Apr 19 '11 at 12:44
  • Is there a way to get this to work in GDM at the login screen? – lindhe Aug 25 '15 at 16:39

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.