15

Sometimes I use vim to write non-US text, and when I wanna use any command in normal mode, I need to change layout to US. It's possible to do that automatically?

PS. I can do mapping like this, but in this case command looks like :ц instead :w - not pretty and typo-risk.

Update

I don't want to use keymap option, because I prefer switch languages by CapsLock. I've try to write autocmd for InsertLeave event, but failed...

Update 2

Probably anybody know, why the following not work?

function SetUsLayout()
  !setxkbmap us,ru
endfunction

autocmd InsertLeave * call SetUsLayout()
Cœur
  • 37,241
  • 25
  • 195
  • 267
Aleksey Bakin
  • 1,506
  • 13
  • 27
  • The answer here is relevant: http://stackoverflow.com/a/3777557/329063 – glts Jun 11 '12 at 17:37
  • See [my answer](http://stackoverflow.com/a/8152137/254635) to a similar question "[Vim “annoyance” with keyboard layouts](http://stackoverflow.com/q/8015231/254635)" rather than the one linked in the previous comment, since it covers effectively the same issue and it is more thorough. – ib. Jun 12 '12 at 03:21
  • I already write in Update, that don't want to use keymap option, because it's not possible to map CapsLock in vim. – Aleksey Bakin Jun 12 '12 at 08:01

4 Answers4

5
:help langmap

is likely to provide all the info you need.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • "Characters entered in Command-line mode will NOT be affected by this option." So hjkl works fine, but commands not. – Aleksey Bakin Jun 11 '12 at 16:58
  • This deals with what you asked for: normal mode commands. See `:help keymap` for the command mode. – romainl Jun 11 '12 at 17:06
  • Ok, I can set keymap=russian-jcukenwin, but to switch to US I need to use ctrl+^ (or map any other), but not very convenient CapsLock. – Aleksey Bakin Jun 11 '12 at 18:15
  • I'm not really in a position to test it out (french layout) but you could try something like `:nnoremap : :`. What's the deal with CapsLock? – romainl Jun 11 '12 at 18:58
  • http://stackoverflow.com/a/2176558/656610 At now I try to use OS-dependent solution: setxkbmap to switch layout automaticly on InsertLeave event – Aleksey Bakin Jun 11 '12 at 19:09
  • This approach is limited in that some keyboard layouts, for example the Canadian Multilingual layout, map keys not to characters but to the first keystroke of a digraph. For example, the key which in the US Standard layout produces `[` will, when pressed with the Canadian Multilingual Layout active, initiate the creation of a circumflex-accented character, e.g. `ê`. – intuited Apr 09 '13 at 18:37
4

Looks like, that cross-platform solution doesn't exist... So, under KDE I use the following:

function! SetUsLayout()
  silent !qdbus org.kde.keyboard /Layouts setLayout us > /dev/null
endfunction

autocmd InsertLeave * call SetUsLayout()
Aleksey Bakin
  • 1,506
  • 13
  • 27
1

For me, using qdbus is the best option. I've made a simple but fragile plugin that works really well for me: https://github.com/ironhouzi/bikey-vim/tree/master/plugin

I call it fragile, since it doesn't have much robustness to it if anybody else wants to use it.

I mostly want English when I'm using Vim, with a few exceptions. When I want to write in my native language, I hit 'leader'-k and my airline status bar will show that I've switched language. When the language is not English, the script will ensure that every time I enter insert mode, my native language is set through qdbus. Every time I leave insert mode, the language is set back to English. It also supports individual settings between buffers. Even though this might not be the best way to do things, I thought I'd share it, in case someone else might get some use out of it.

jollyroger
  • 659
  • 1
  • 10
  • 19
0

In Ubuntu I use the following:

function! SetUsLayout()
  silent !qdbus org.gnome.SettingsDaemon.Keyboard /org/gnome/SettingsDaemon/Keyboard org.gnome.SettingsDaemon.Keyboard.SetInputSource 0 > /dev/null
endfunction

autocmd InsertLeave * call SetUsLayout()

or shorter

silent !gsettings set org.gnome.desktop.input-sources current 0  
Yurii Holskyi
  • 878
  • 1
  • 13
  • 28