How To Map RAlt to Ctrl for Emacs

In a recent post I was talking about the benefits of mapping the RAlt key to the Ctrl key and this set up is so far still feeling very comfortable.

However, there are many ways to set up the mapping, so below are instructions on how to map the right Alt key (RAlt) to Ctrl for different platforms. These are just the methods I have used in the past, I’m sure there are a multitude of other options out there.



Before you dive in, here is a rough plantuml diagram to give you a top level diagrammatical overview of what I am describing below (note: kmonad is not included):

Linux

X11 or Wayland

xkb (X Keyboard Extension)

First generate an xkb file which defines all keybindings for the current layout by running the follwing command:

xkbcomp $DISPLAY -xkb keymap.xkb

Add to the keymap.xkb file along with the other defined modifier_maps:

modifier_map Mod1 { <RALT> };

and set the definition of key to:

key <RALT> { [ Alt_L, Meta_L ] };

Now load the modified file back in again:

xkbcomp keymap.xkb $DISPLAY

If you want it to be applied every time you log in, you can add this command to your startup applications or add it to your .bashrc or .profile file.

X11 only

xmodmap

Xmodmap is a little archaic these days and is seen as being a little deprecated and soon to become obsolete (especially with the incoming Wayland protocol) but especially on X11 it can still be used if you don’t want to fiddle around with xkb files.

To map the right Alt key (RAlt) to Ctrl using xmodmap in Linux, you can follow these steps:

Create a file named .Xmodmap in your home directory if it doesn’t exist already.

Add the following line to the file:

keycode <keycode_of_RAlt> = Control_L

Replace with the keycode of your right Alt key.

To find the keycode, you can use the xev command. Open a terminal and run:

xev

This will open a small window. Move your cursor into that window and press the right Alt key. Look for the keycode in the terminal output. It should look something like keycode 108 (maybe exactly like!)

To apply the changes, run:

xmodmap ~/.Xmodmap

Now your right Alt key should behave like the Ctrl key. Keep in mind that this mapping will only persist for your current session. If you want it to be applied every time you log in, you can add the xmodmap ~/.Xmodmap command to your startup applications or add it to your .bashrc or .profile file.

setxkbmap

Simply run the following command:

setxkbmap -option ctrl:ralt_rctrl

If you want it to be applied every time you log in, you can add this command to your startup applications or add it to your .bashrc or .profile file.

Wayland Tiling Window Managers

Sway

Perform the xkb steps defined above and then modify the sway config file as follows:

input type:keyboard {
   xkb_file keymap.xkb
}

alternatively modify the sway config file as follow:

input type:keyboard {
    xkb_options ctrl:ralt_rctrl
}

As this is modifying the basic config file it will be guaranteed to be loaded every time you log in.

Hyprland

As the xkb steps above and then modify the hyprland config file as follows:

input {
   kb_file = keymap.xkb
}

alternatively modify the hyprland config file as follow:

input {
   kb_options = caps:ctrl_modifier
}

As this is modifying the basic config file it will be guaranteed to be loaded every time you log in.


Windows

AutoHotKey

Install AutoHotkey if you haven’t already. You can download it from the official website: https://www.autohotkey.com/

Once AutoHotkey is installed, right-click on your desktop or in a folder, hover over “New,” and select “AutoHotkey Script.” Name the script whatever you like, for example, RAlt_to_Ctrl.ahk.

Right-click the newly created script file and select “Edit Script” to open it in your default text editor.

Add the following line to the script:

RAlt::Ctrl

Save the script and close the text editor.

Double-click the script file to run it. You should see a green “H” icon in the system tray indicating that the script is running.

Now, whenever you press the right Alt key, it will function as the Ctrl key. To stop the remapping, right-click the AutoHotkey icon in the system tray and select “Exit.”

This remapping will persist until you close the script or disable AutoHotkey. If you want the remapping to be applied automatically every time you start your computer, you can place a shortcut to the script in the “Startup” folder of your Start Menu.


Linux or Windows

kmonad

Install kmonad if you haven’t already. You can find installation instructions on the GitHub repository: https://github.com/kmonad/kmonad

Once kmonad is installed, create a configuration file called kmonad.hs

linux

Add the following lines :

(defcfg
  input  (device-file "/dev/input/by-path/<device>")
  output (uinput-sink "My KMonad output")

  ;; Comment this if you want unhandled events not to be emitted
  fallthrough true

  ;; Set this to false to disable any command-execution in KMonad
  allow-cmd true
  )

(defsrc ralt)

(deflayer default lctrl)

Now on the command line run up:

kmonad kmonad.hs

To ensure that the mapping persists place the command to your startup applications or add it to your .bashrc or .profile file.

windows

(defcfg
  input (low-level-hook)
  output (send-event-sink)
  fallthrough true)

(defsrc ralt)

(deflayer default lctrl)

Now create a batch script called kmonad.bat and add:

kmonad.exe kmonad.hs

This remapping will persist until you close kmonad.exe. If you want the remapping to be applied automatically every time you start your computer, you can place a shortcut to the script in the “Startup” folder of your Start Menu.


This post may end up turning into some kind of living document when I find out other ways to perform the mapping, for example I might start to look at Kanata and also see if PowerToys can perform this mapping on Windows.

Comments

comments powered by Disqus