Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remap Win, Ctrl and Alt keys on Linux

The question:

How can I remap (swap) modifier keys system-wide on Linux?

Explanation:

I want to install Debian Jessie on my PC to use it with Emacs. I have a MacBook with Emacs and use it primarily. Here is how keys are arranged on laptop:

fn ctrl alt cmd spacebar cmd alt

fn is unused in my Emacs workflow, other keys configured as listed below:

  • ctrl acts as Hyper
  • alt acts as Meta
  • cmd acts as Control

These configuration eases for me Emacs experience (big thumb hits Control cmd key) and I got used to this kind of key bindings.

by default alt is used as Meta, while ctrl is used as Control, the keys are swapped and big thumb lays on Meta

I want to configure my keys on Linux same way, here is what I have on my PC's keyboard:

ctrl win alt spacebar alt win menu ctrl

So, to correspond to my laptop keys, I want win keys to be mapped to Alt modifier, and alt keys to be mapped to Control modifier. In this case ctrl could be used as Super modifier, while menu could be kept untouched.

Is it possible to make such configuration system-wide on Linux (Debian)?


Finally, I have managed to achieve my goal with following configuration:

!file ~/.Xmodmap

clear control
clear mod1
clear mod4


keycode 37  = Super_L     
! left  Ctrl becomes Super
keycode 64  = Control_L
! left  Alt  becomes Control 
keycode 133 = Alt_L Meta_L
! left  Win  becomes Alt
keycode 108 = Control_R
! right Alt  becomes Control 
keycode 134 = Alt_R Meta_R
! right Win  becomes Alt
keycode 105 = Super_R
! right Ctrl becomes Super


add control = Control_L Control_R
add mod1 = Alt_L Meta_L
add mod4 = Super_L Super_R

Also, here is similar question with quite helpful and simple guides.

like image 534
Geradlus_RU Avatar asked Aug 21 '15 20:08

Geradlus_RU


People also ask

How do I remap keys in KDE?

In Global Shortcuts, you will find keyboard combinations added to the system by both KDE and any installed applications. To change a shortcut that already exists, click on it and choose Custom instead of Default. Then, click on the button on the right of Custom and press your desired key combination when asked.


2 Answers

Additionally to and possibly easier than using xmodmap might be to check in the keyboard layout options given by setxkbmap. These options implement common xmodmap changes, so they might well contain what you want.

For example, I like -option ctrl:nocaps which adds me another control where the caps lock was. Consider the file /usr/share/X11/xkb/rules/base.lst for a list of the standard options.

However, neither xmodmap nor setxkbmap include keyboard settings for the text-only tty terminals (non-X).

like image 199
Hannes Avatar answered Oct 06 '22 01:10

Hannes


xmodmap is the place to start, keeping in mind that X knows nothing about Hyper or Meta, so you have to investigate to see which numbered modifier (mod4, mod5, etc), emacs uses for these modifiers.

Here are a few useful links:

  • XModMap
  • How to separate alt and meta keys in emacs
  • MetaKeyProblems
  • Emacs does not use swapped alt&win after xmodmap runs? (GUI emacs)
like image 35
Thomas Dickey Avatar answered Oct 06 '22 00:10

Thomas Dickey