emptiness

Jihang Li

neuims: An Input Method Switcher for (Neo)Vim

2020-02-21 14:50

1 Background

People who use CJK or other non-Latin input methods can relate how pain it is to toggle input methods between insert and normal mode in (Neo)Vim. The first time I came across this problem several years ago, VimIM was the tool I found. But I always had problems with getting expected candidates or setting up on a new machine. I read the manual but it didn't change anything. (Yea, I must be dumb.) Also, the switching performance of VimIM was poor, and I'm not sure about the reason - my potato laptop, or the plugin itself. For a while, I use other editors instead of Vim to do my "CJK" job.

But finally I have had enough of it, because I always accidentally typed in "h", "j", "k", "l". A new search was started again. Tools like vim-barbaric and vim-xkbswitch, they don't have support for Windows OS or require the user to read the manual for an hour and to configure for another.

I'm probably wrong about them, and actually, I just want to write some code. So here is "neuims", neur1n's input method switcher.

Screenshot

2 Requirement

Windows 7/10

Currently this plugin is only tested on Windows 7 and 10. And since commit fe2abf1, Python dependencies were removed and a binary written in C++ is used instead. The binary is static-linked, so it should work even there are no VC++ redistributables installed in your system.

Linux/Unix

Currently this plugin is only test on Ubuntu 16.04, and it is limited to IBus. The ibus engine [ENGINE_NAME] command is called to switched the input method.

3 Installation

vim-plug

Put

    Plug 'neur1n/neuims'
    

into your configuration file. But using the on-demand loading feature of vim-plug is recommended:

    Plug 'neur1n/neuims', {'on': 'IMSToggle'}
    

4 Usage

4.1 Enable

This plugin needs to be manually enabled before being used to switch input methods. There are two ways to enable:

    nnoremap <silent> <leader>it :call neuims#Toggle()<CR>
    

4.2 Disable

While in enabled status (can be checked with :echo g:neuims.status), use :IMSToggle or press <leader>it again.

4.3 Switch

Usually, the switching is automatically controlled by the InsertEnter and InsertLeave event. However, one may call the function neuims#Switch(0) to switch manually, or using a key mapping, e.g.:

      nnoremap <silent> <leader>is :call neuims#Switch()<CR>
    

5 Configuration

There are default configurations in the source code:

    let s:win = {
          \ 'im': 'US Keyboard',
          \ 'status': 0,
          \ 'keyboards': {
          \   'US Keyboard': 0x0409,
          \   'Microsoft Pinyin': 0x0804,
          \ },
          \ }

    let s:ibus = {
          \ 'im': 'English (US)',
          \ 'status': 0,
          \ 'keyboards': {
          \   'English (US)': 'xkb:us::eng',
          \   'Pinyin': 'pinyin',
          \ },
          \ }
    

This should work well with Windows's (7/10) and Ubuntu's (16.04) built-in input methods. Let's break it down:

For example, if one uses RIME (IBus based) in Ubuntu, the configuration may be:

    let g:neuims = {
          \ 'im': 'English (US)',
          \ 'status': 0,
          \ 'keyboards': {
          \   'English (US)': 'xkb:us::eng',
          \   'Rime': 'rime',
          \ },
          \ }