How to change your keyboard layout from the terminal on Linux


Very short post today. Since I use a computer with a British keyboard I need to constantly change to Spanish when I want to type something in my mother tongue. We have a few characters that don’t exist in the English alphabet and even though I could use ASCII codes changing the layout is much easier and faster.

I work with the terminal a lot so I just use the following command

$  setxkbmap es #or other layout code

In my case I use two codes, es for the Spanish keyboard and gb for the British one.

To make this even faster and easier I’ve created a couple of aliases. I keep thing in ~/.zshrc because I use Z shell (zsh), but depending on your shell you’ll need to configure them in other files, for example, ~/.bashrc for Bash.

Simply edit the file and create the alises, these are mine:

# Edit ~/.zshrc
$ vim ~/.zshrc

alias lan_gb='setxkbmap gb' #Change to British keyboard
alias lan_es='setxkbmap es'  #Change to Spanish keyboard

Once you’ve done this save and quit the editor. You can close and reopen your terminal or just source ~/.zshrc. You’ll be able to use your aliases after that.

Important! These changes aren’t persistent. When you reboot or close your session the keyboard layout will go back to whatever its default is.

Leave a Reply

Your email address will not be published. Required fields are marked *