How do I change currently selected keyboard layout from command line?

26,468

Solution 1

You could use xkb-switch (-n switches to next layout):

xkb-switch -n

or xkblayout-state (with set +1 to wrap around, in your case) :

xkblayout-state set +1

or xte from xautomation to simulate Control_L+Shift_L key press/release:

xte 'keydown Control_L' 'keydown Shift_L' 'keyup Shift_L' 'keyup Control_L'

Solution 2

Searches of Stack Exchange for kbdmap find this page with no mention of that word. For clarity, here's an answer (not exactly fitting with the X11 tag of the question, but it should help someone) …

When limited to a virtual console

without X.

kbdmap(1)

kbdmap, vidfont -- front end for syscons and vt

… allows easy setting of available keymaps …

Truly, it's easy and (for me, changing to a UK keyboard layout) effective.

When using X

– for example, with FreeBSD-based TrueOS Desktop.

From post 5 under Change keyboard layout? | The FreeBSD Forums (2015-08-03):

kbdmap is for the console. setxkbmap is the utility you want for X11. …

There's a warning, for example when changing to United Kingdom:

$ kbdmap
kbdcontrol: getting keymap: Inappropriate ioctl for device
You are not on a virtual console - expect certain strange side-effects
lang_default = en
dialect = en_...UTF-8
lang_abk = en

…

keymap="uk.kbd"
$ 

Solution 3

Depending on the distro for the keyboard layouts, the command is

loadkeys <path/to/file>

loadkey /lib/kbd/keymaps/i386/qwerty/pt_PT.map.gz

If I remember correctly. Also check manpage for loadkeys if you want to assign specific keys to specific behavior.

Solution 4

Take a look at this howto forge article titled: Changing The Language & Keyboard Layout On Various Distributions, for the various commands to change your keyboard layout on different distros.

Most of these commands present as GUIs but if you look most of them can be run from the command line as well. For example I'm using Fedora (A Red Hat based distro). The command system-config-keyboard when run with no arguments shows me a GUI.

But if I run system-config-keyboard --help it can be run from the command line too:

$ system-config-keyboard --help
Usage: system-config-keyboard [--help] [--noui] [--text] [<keyboardtype>]
       --help            Print out this message.
       --noui            Run in command line mode.
       --text            Run in text interface mode.

       <keyboardtype> options are: ar-azerty, ar-azerty-digits, ar-digits, ar-qwerty, ar-qwerty-digits, be-latin1, ben, ben-probhat, bg_bds-utf8, bg_pho-utf8, br-abnt2, cf, croat, cz-lat2, cz-us-qwertz, de, de-latin1, de-latin1-nodeadkeys, dev, dk, dk-latin1, dvorak, es, et, fi, fi-latin1, fr, fr-latin1, fr-latin9, fr-pc, fr_CH, fr_CH-latin1, gr, guj, gur, hu, hu101, ie, is-latin1, it, it-ibm, it2, jp106, ko, la-latin1, mk-utf, nl, no, pl2, pt-latin1, ro, ro-cedilla, ro-std, ro-std-cedilla, ru, sg, sg-latin1, sk-qwerty, slovene, sr-cy, sr-latin, sv-latin1, tj, tml-inscript, tml-uni, trq, ua-utf, uk, us, us-acentos

So to change my keyboard layout to Russian I could run the following command:

$ system-config-keyboard ru
Share:
26,468

Related videos on Youtube

Rogach
Author by

Rogach

Updated on September 18, 2022

Comments

  • Rogach
    Rogach over 1 year

    I have my keyboard layouts (two of them) and switching between them configured via the following command:

    setxkbmap -layout us,ru -option -option "grp:lctrl_lshift_toggle,ctrl:nocaps"
    

    Now I want to switch to us layout, using some command line command. Is that possible?

  • Rogach
    Rogach almost 11 years
    The problem is that after I do (for example) loadkeys us, it resets the settings I created with setxkbmap (two layouts and switching between them)
  • BitsOfNix
    BitsOfNix almost 11 years
    as far as I know this is the command to change layouts, if you have a specific, then I guess the best way would be to have an alias in your shell that would call the loadkeys with yout settings. ex: load_us would load the us keyboard and actually would be calling the loadkey /lib/kbd/keymaps/i386/qwerty/us.map.gz or similar. And the same for the other layout.
  • Rogach
    Rogach almost 11 years
    But how would I change keyboard settings afterwards? Currently, I change layouts by pressing Ctrl-Shift, using loadkeys screws it.
  • BitsOfNix
    BitsOfNix almost 11 years
    let make a step back here, from command line I'm assuming that you are mentioning a tty terminal. Meaning that you are not using xterm/terminology/gnome-terminal or any other emulator but in fact you are in a environment without X server. Taking this then you need to use loadkeys, as per your question, I gave you the command line, what you actually want seems to be a key shortcut. If you have specific keys to be mapped, then you need to create your own layouts and load then on a request with load keys from a cli point of view. If you want a shortcut instead, then update your question.
  • Rogach
    Rogach almost 11 years
    No, I am, indeed, using graphical environment and gnome-terminal in particular. It's just that I need to change X layout programmaticaly, in reaction to some events - thus, tty stuff is not going to work.
  • Rogach
    Rogach almost 11 years
    xkblayout-state is great, thanks! I just use xkblayout-state set 1, and I instantly get us layout.