TIL: Switching Keyboard Layout
Today I learned (TIL) how to make my routine smoother by switching between Brazilian Portuguese and English on the keyboard. My keyboard is American by default, meaning it doesn't have the รง key that is common on "br" keyboards.
In my day to day I need to write in Portuguese when I'm in meetings, chats and e-mails, but I prefer to type in English when I'm coding or writing technical documents.
The previous solution was typing the setxkbmap command every time I needed to change the keyboard layout. Tired of that, I wrote a shell script that switched between the layouts, and even after setting up a shortcut key to call that script I thought there should be a better way.
The solution was to configure a simple shortcut to switch between the two keyboard layouts directly in setxkbmap.
In the manual page, in the explanation of the layout option, there is a
passage that explains exactly the detail I was looking for.
"...Multiple layouts can be specified as a comma-separated list."
The command
setxkbmap -layout 'us,us' -variant ',intl' -option 'grp:win_space_toggle'
What does it do?
-layout 'us,us': sets the default layout to 'us' (US) and adds a second variation, alsous.-variant ',intl': the second option is configured as US International, allowing accents for Portuguese.-option 'grp:win_space_toggle': I set theWin + Spaceshortcut to switch between the layouts.
Extra
If you want to use other shortcut keys to switch between layouts, the xkeyboard-config manual has a section with all the possibilities.