Ubuntu – Command to toggle keyboard layout

command linekeyboard-layoutvim

I am using a keyboard with a different layout than qwerty. This is a pain when I use vim editor so I decided to change my keyboard layout to qwerty only when I am using vim. I came up with an idea of using autocommand that changes the layout when I enter/exit Vim. (I am a vim newbie and I don't know much about vim and that was all I could think of.)

My Ubuntu is 18.04. I clicked the option key in Region & Language, Settings to "allow different sources for each window". The setxkbmap command changes the keyboard layout system wide. Do you know how to make it work well or any other suitable command?

I would also appreciate any suggestions on how to configure vim (though this might be more appropriate to post in Vim's community site then).

Best Answer

It should be enough to just add this line to ~/.vimrc:

set keymap=foo

Where foo is the name of your keymap. For instance, I tested with set keymap=greek and I was typing in Greek when I opened vim (of course, then I couldn't exit the thing but that's vim for you).


If that doesn't work for some reason, here's an ugly, hacky approach you could try instead. Add this to your ~/.bashrc:

vim(){
  setxkbmap XX ## change this to whatever keymap you use for vim
  command vim  ## launch the actual vim executable
  setxkbmap YY ## change to whatever your default layout is
}

Now, open a new terminal and use vim. The keyboard layout will be automatically changed for the vim session and changed back when you exit vim.

This is not a very good solution, however, because the keyboard layout will be changed for all applications. It simply does it automatically and changes back when you exit, but you will still have the new layout for all other windows, as well as vim. The first approach is much better.

Related Question