Where to put xmodmap command execution for remapping CapsLock to Esc?

Solution 1:

The problem is that xmodmap is deprecated, and the keyboard layout is resetted at strange times. See if https://askubuntu.com/a/464618/16395 can help you.

In gnome-shell you can achieve this thing by setting, in gnome-tweak-tool, one of these options:

gnome tweak tool screenshot editing caps lock behavior

You should be able to find an equivalent thing for Elementary.

See also https://askubuntu.com/a/629043/16395 and Map AltGr-[a..z] and Shift-AltGr-[A..Z] to greek letter