0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Vim] ノーマルモードと日本語の入力モードの行き来を楽にするスクリプト

Posted at

はじめに

Vimと日本語入力の相性の悪さは誰でも感じるだろう.日本語入力しているとき,ノーマルモードにした際,入力メソッドの変更を忘れたままカーソル移動キーを押すこともかなり多く,ストレスを感じるだろう.そんな不便さを少しでも解消したく,以下のスクリプトを作成したので,init.vim の適当な場所に追記してみると良い.

なお,以下はOSがUbuntuで日本語入力メソッドに fcitx5-mozc を利用している場合に正常に動作していることを確認しただけなので,使用する際は自身の入力メソッド環境に注意して適宜変更すること.

本題

最も簡単な方法は以下のようなフローをスクリプトにすることだろう:

  1. 入力モードから抜けるとき(InsertLeave),入力メソッドの状態(fcitx-remote -n)を記憶しておく
  2. ノーマルモードで入力メソッドを無効化する(fcitx-remote -c
  3. 入力モードに入るとき(InsertEnter),記憶した入力メソッドに戻す(fcitx-remote -c

これにより,英文のドキュメントを入力するときに日本語入力モードになることはなく,日本語入力のときのみ入力メソッドの有無を変更する仕様となる.

" Input method modification
function! GetFcitxStatus()
    return trim(system('fcitx5-remote -n'))
endfunction
function! OnInsertLeave()
    " Save fcitx status when leaving insert mode
    let g:fcitx_status = GetFcitxStatus()
    " Disable (Japanese) input method 
    call system('fcitx5-remote -c')
endfunction
function! Enable(status)
    " 'keyboard-us'は環境依存だと思う
    if a:status != 'keyboard-us'
        call system('fcitx5-remote -o')
    endif
endfunction
let g:fcitx_status = GetFcitxStatus()
autocmd InsertLeave * call OnInsertLeave()
autocmd InsertEnter * call Enable(g:fcitx_status)

注意点

私はUSキーボードを使っている関係上,日本語入力でない英数入力時の fcitx-remote -n の戻り値は keyboard-us となるため,関数 Enable(status) の if 文には keyboard-us かどうかで判定しているが,人によっては異なると思う.一度,ターミナル上で英数入力時の fcitx-remote -n を確認しておこう.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?