How to automatically change keyboard layout on switch to vim normal-mode?

可紊 提交于 2020-12-29 05:30:05

问题


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

回答1:


:help langmap

is likely to provide all the info you need.




回答2:


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



回答3:


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.




回答4:


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  


来源:https://stackoverflow.com/questions/10983604/how-to-automatically-change-keyboard-layout-on-switch-to-vim-normal-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!