1

I am on Ubuntu 18.04 and trying swap Ctrl and CapsLock using xmodmap. But failed to find a way of doing that automatically: .[X|x]modmap[rc] and .config/autostart didn't work. What other ways are there? Could it be possible throgh systemd?

SHORT: Desktop entry in .config/autostart or /etc/xdg/autostart. Exec is not a full-fledged shell command, so sh -c might be required

[Desktop Entry]
Type=Application
Exec=sh -c "xmodmap ~/.xmodmaprc"

2 Answers2

2

Since Ubuntu switched back from Unity to Gnome in version 17.10, you should be able to use the Gnome autostart mechanism (if it is sufficient that the shell command is launched on login).

To do so:

  • you will need sudo privileges
  • create a shell script that runs the necessary command (say switch_ctrl_capslock.sh) and place it in /usr/local/bin
  • create a .desktop file /etc/xdg/autostart/switch_ctrl_capslock.desktop with (more or less) the following content:
    [Desktop Entry]
    Type=Application
    Exec=/usr/local/bin/switch_ctrl_capslock.sh
    

If everything is setup correctly, the script should be run once when the user logs into Gnome.

For further reading, have a look at

AdminBee
  • 22,803
  • How would this be used to swap the keys before login? (Not that I know why you'd use the Ctrl key at the gdm login screen, but there might be something I don't know about.) – RonJohn Dec 23 '20 at 09:03
  • @RonJohn That is a little more involved. One problem is that the desktop environment/window manager often uses input methods that supersede the "raw" input interpretation (as can be found on the virtual consoles e.g.), but since they are run by the user logging in, doing that before login may prove to be ineffective. The solution would probably depend on the exact circumstances (graphics server, desktop manager, window manager). – AdminBee Dec 23 '20 at 09:06
  • 1
    Yep, desktop entry is the way to go. I've created one in .config/autostart. I've tried it before, but it failed because Exec is not a full-fledged shell command and don't expand tildes, variables and substitutions – vatosarmat Dec 23 '20 at 10:35
2

The systemd-way:

Upon login

[Unit]
Description=Change keyboard layout AFTER LOGIN TO GUI session
After=graphical.target

[Service] #execute once only Type=oneshot ExecStart=/usr/bin/xmodmap home/<user>/.xmodmaprc

[Install] WantedBy=graphical.target

To be put under ~/.config/systemd/user/xmodmap.service (note do NOT replace user with your username!).

To enable the service to be autoloaded:

systemctl --user enable xmodmap.service

To start it the first time without need for a relogin;

 systemctl --user start xmodmap.service

Running it as soon as gdm is up

Same file as above, but rather place it at /etc/systemd/system/xmodmap.service and add the following line to the [Unit] section to be save:

 After=display-manager.service

As root (or via sudo):

systemctl enable xmodmap.service

Of course it will then affect all users.

FelixJN
  • 13,566