Score:1

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

in flag

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.

ru flag
`.bashrc` is only referenced with the terminal opening into a login shell. Unless you're logging directly into a terminal login shell (Bash) it won't load at start. It also may not be persistent. GUI environments though don't refer to `.bashrc` when you load up on boot.
Pedro Delfino avatar
in flag
Thanks, @ThomasWard. This explanation makes a lot of sense. Do you recommend another place for me to insert this `setxkbmap` definition? I would like to have it as a persistent change and loading up on boot.
ru flag
I have a couple of potential solutions but i am on the tail end of a very long day and heading to sleep, will share my thoughts tomorrow after sleep.
Pedro Delfino avatar
in flag
No problem. Have a nice rest :)
ru flag
TYPICALLY this is done in `/etc/rc.local` but that's more or less deprecated nowadays in favor of SystemD oneshot units at boot time. Which would work for literally the entire system (or they should anyways). Let me write up an example in an answer. If it works, great, if not, we'll keep chugging along on potential solutions.
ru flag
I'm assuming my cronjob answer solved the issue for you? Was about to write up the SystemD approach but won't if the cronjob solved it.
Score:1
ru flag

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.

Pedro Delfino avatar
in flag
Thanks. It *almost* works. After start-up, it does not work. But if I open the shell it works. This was already possible. But now I can close the shell (terminal) and it keeps working!
Pedro Delfino avatar
in flag
I will try to insert `gnome-terminal` on my start-up preferences. So, I think it will work.
ru flag
OK then this looks like a case of just adding gnome-terminal to your startup apps.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.