4

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).

  • 1
    Isn't it enough to assign a keyboard shortcut to the layout switch and just switch when you open vim? – terdon Jul 12 '18 at 11:08
  • My attempt is automization of that routine. – kazuki Jul 12 '18 at 11:10
  • OK. How do you launch vim? Always from the command line? – terdon Jul 12 '18 at 11:15
  • Yes. It is not gvim or any other vim variant. The one on the terminal. – kazuki Jul 12 '18 at 11:19
  • What keyboard layout do you want? – terdon Jul 12 '18 at 11:40
3

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.

  • The Text Entry Settings GUI (at least the one from good old Unity) allows you to enable changing the keyboard layout for single windows only. I wonder how they do it under the hood and whether it could be replicated in the terminal... – Byte Commander Jul 12 '18 at 11:58
  • @ByteCommander yes, that's mentioned in the question. It's an old feature, presumably deep X since it's been around long before Unity. But as far as I know, that just lets you change the layout while one window has focus, and then switch to another window, change layout there and then have each window remember its layout when you go back and forth between them. – terdon Jul 12 '18 at 12:01
  • In fact, I found it helpful to use langmap because my keyboard is already English and my problem was such things as bad hjkl positions. (I should have been more specific.) Though, from your answer it was easy for me to guess the opposite setting exists and this led to the resolution. Many thanks! – kazuki Jul 12 '18 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.