Why setxkbmap in .bashrc is intermittently working? Why it takes awhile to load after PC has already started?

I am using this on my .bashrc file:

## SWAP CONTROL KEY

setxkbmap -option ctrl:swapcaps

I did this, basically, because I am using Emacs a lot. It seems to work most of the time.

Unfortunately, 2 problems keep happening:

(i) - After the PC starts, it takes a while to work. It is not instantaneous which intrigues me, since this seems to be something "small" to change;

(ii) - Sometimes, out of the blue, it has stopped working, and then, suddenly, the swap is back. This happened one time today.

Although it was just one time, it is really annoying because I have changed the keybinding recently. So, I am already making mistakes sometimes while getting used to it. If I cannot trust this keybinding, the mistakes are going to be even more frequent.

I have a huge knowledge gap on .bashrc file configurations. Maybe, there is some inconsistency in my file. Here is a repository on GitHub showing the whole .bashrc file.

Thanks.


So, one of the problems you're encountering is the evil that is ".bashrc is only read for Bash shells in the terminal". Your GUI is NOT a Bash shell, so .bashrc is NOT read by the GUI until you open the Terminal and then it activates/applies the keychange.

If we want it to work globally it needs to execute as part of the system independent of the .bashrc. There're two options: Your user crontab for @reboot which is a special case name. OR, a SystemD unit.

(I've tested neither solution, so I can't comment if one will work better than the other, if at all).

Let's start with the cron job option first.

Cron job approach

Open your terminal, and run crontab -e.

Add this line to it:

@reboot setxkbmap -option ctrl:swapcaps

Reboot your system, and see if this works.

If it works, great. If not, then we have to try another approach.

Fallback Solution: Autorun gnome-terminal after login

If all else fails, add gnome-terminal to your startup applications.

First, install gnome-startup-applications:

sudo apt install gnome-startup-applications

Secondly, search your applications for "Startup Applications Preferences"

Add an item for gnome-terminal. That way it'll autorun the terminal automatically.