I'd like to switch my Ctrl and Alt keys under Linux (to make it more Mac-like). How do I do this?
If it helps, I'm running Ubuntu 9.04.
First off, try looking in Preferences > Keyboard > Layouts > Layout Options under the Alt/Win key behavior if there is a suitable option there for you.
If that doesn't do it for you, these xmodmap commands would switch left Ctrl with left Alt (at least with my keyboard).
xmodmap -e 'keycode 37 = Alt_L ISO_Prev_Group ISO_Prev_Group NoSymbol ISO_Prev_Group'
xmodmap -e 'keycode 64 = Control_L'
To get your keycode, you can run the program xev in a terminal window and press first Ctrl and note the keycode, then Alt to get the keycode.
xmodmap - utility for modifying keymaps and pointer button mappings in X
showkey - examine the codes sent by the keyboard
setkeycodes - load kernel scancode-to-keycode mapping table entries
Do the following steps:
Create a new file. Let's call it remap.txt.
Add the following text to the file:
!
! Swap Alt_L and Control_L
!
remove mod1 = Alt_L
remove Control = Control_L
keysym Control_L = Alt_L
keysym Alt_L = Control_L
add mod1 = Alt_L
add Control = Control_L
The above is like a script for xmodmap. It will change the current key bindings.
Run the following commands to run the script:
# xmodmap remap.txt
(optional) To load the same settings after reboot run:
# sudo dumpkeys > ~/.Xmodmap
I am using Ubuntu 20.03.2 LTS, and for some reason simply re-attributing a given Keysym (Control_L or Alt_L) to a given KeyCode (37 and 64) using xmodmap (as described above as well as here) doesn't work out of the box anymore.
Now you need to first remove the Keysyms from their respective "modifiers", before swapping the Keysym-KeyCode mapping, before adding them back to ensure that they are taken into account across applications (I found the trick thanks to this forum):
1 - Find out which keys belong to which modifiers
xmodmap # Control_L should be in control and Alt_L in mod1
2 - Remove keys from their respective modifiers
xmodmap -e "remove control = Control_L"
xmodmap -e "remove mod1 = Alt_L"
3 - Find out the Keysym-KeyCode mapping on your machine
xmodmap -pk # For me, Control_L is mapped to 37 and Alt_L to 64
4 - Swap the Keysym-KeyCode mapping
xmodmap -e "keycode 37 = Alt_L"
xmodmap -e "keycode 64 = Control_L"
5 - Check with xev that the remapping is correct
xev # Then press left ctrl and alt key to witness that they have been swapped in the terminal readout
6 - Add keys back to their respective modifiers
xmodmap -e "add control = Control_L"
xmodmap -e "add mod1 = Alt_L"
And that't it!
7 - Of course, these changes are not permanent - now that you know that they work, create “swap.desktop” file and put it into ~/.config/autostart with following contents in it:
[Desktop Entry]
Name=Swap
Exec=xmodmap -e "remove control = Control_L" && xmodmap -e "remove mod1 = Alt_L" && xmodmap -e "keycode 37 = Alt_L" && xmodmap -e "keycode 64 = Control_L" && xmodmap -e "add control = Control_L" && xmodmap -e "add mod1 = Alt_L"
Terminal=false
Type=Application
8 - Now make it executable
chmod +x ~/.config/autostart/swap.desktop
And here you go!
A newer (albeit beta at time of writing) option is using a tool like kbct: https://github.com/samvel1024/kbct .
This allows for having a yaml-style configuration and is intended to be more agnostic to whether one is using Wayland or X11. Found this after looking through previous answers and realizing they may not work under Wayland.
Another popular (and likely more stable) one is kmonad: https://github.com/kmonad/kmonad