How to Display and Input Japanese in Kterm/Vim/Gvim using Kinput2 with Canna

I had a hard time trying to get Japanese display and input working in gvim with X11. So here is a list of the stuff I did to get it to work, and some handy referrences. I'm running the Debian distribution of linux, version 2.3 (unstable, "woody"), so expect some differences if you are setting up a different type of distro.



  1. Common steps for Setup
  2. Modifying Xresources
  3. Modifying Xsession
  4. Running Vim in kterm or kon
  5. Running Gvim in X
  6. Tips and Caveats
  7. References

Common steps for Setup:

  1. Download and Install the relevant software:
    I use Debian, so apt-get install <package name> suffices.

  2. Start the input conversion dictionary server:
    You must start cannaserver for conversion to take place. Debian automatically has this start on install and puts it into the rc startup scripts.


  3. Configure the appropriate X resources:
    Add the following lines to your .Xresources (.Xdefaults for those non-Debian users out there):
    ! Set the default encoding for Kterm
    *kanjiMode: euc
    *international: True
    
    ! Supposedly optional with X11R6
    *inputMethod: kinput2
    
    ! Supposedly optional with X11R6
    ! Choose OverTheSpot, OffTheSpot, or Root.  (OnTheSpot is not yet supported by vim5.6)
    *.preeditType: OverTheSpot
    
    ! Font for the Vim text area and the menus.
    Vim.font: -misc-fixed-medium-r-normal--14-*
    Vim*fontSet: -misc-fixed-medium-r-normal--14-*
    Vim*fontList: -misc-fixed-medium-r-normal--14-*
    	    


    If you're in X already, be sure to run "% xrdb -merge .Xresources" or whatever to have your changes take effect.


  4. Modify your .xsession:
    Add the following to your .xsession, or whatever script file you use to start X or your window manager. (In Debian, using startx or xdm should automatically source .xsession in your home directory if it exists.)

    kinput2 -canna&
    export XMODIFIERS="@im=kinput2"&

    To localize all your menus and such to Japanese, and any programs that you start under your window manager, you can also set the following:

    export LANG=ja_JP.eucJP
    export LINGUAS=ja

    Of course, if you're in X just run these in a terminal somewhere. Anything you run from that terminal will then, of course, be set for Japanese.

    Setting the XMODIFIERS environment variable seems to have no effect for me, but in the Vim help files they say: export XMODIFIERS="@im=your_input_server_name". And, in a post on comp.editors, export XMODIFIERS='@im=vje', though I don't know what vje means. Using @im=kinput2, I get weird behavior with kterm, which will not switch to xim unless I use the Kanji key.

    Update: Sunday November 5, 2000 18:40 PST: Since installing Xfree86 4.0.1 and the new "locales" Debian package, I can't seem to get input in Vim working without setting this variable. Also, starting kterm with the -xim switch no longer works.


Running Vim in kterm:

  1. Modify your .vimrc:
    Add the following lines to your .vimrc:
    if $LANG =~ "ja_JP.*" || $TERM == "kterm" || $TERM == "kon"
        set fe=japan
    endif
    	
    This will also setup Vim to display Japanese text in Kon as well, though I have yet to figure out input in kon.


  2. Start your kanji terminal:
    In X, use kterm. Before installing Xfree 4.0.1, without using the -xim argument, I was not able to begin Kanji input/conversion with shift-space, only ctrl-Kanji seems to work. Also, Vim would not handle the input from the canna server if the -xim option was not used.

    Now, I cannot use the -xim option at all, but at least ctrl-Kanji still works, both in kterm and in Vim run from kterm. ^_^

    I have also seen rxvt used, but I have not experimented with this.

    Try kon at the linux console.

  3. Run Vim and have fun!
    Start vim. Shift-Space or ctrl-Kanji starts input conversion.

Using Gvim in X11 to view/input Kanji:

  1. Modify your .gvimrc:
    Add the following lines to your .gvimrc:
    "Setup vim for japanese encoding if the language is japanese
    if $LANG =~ "ja_JP.*" 
        set fileencoding=japan
        set guifontset=-misc-fixed-medium-r-normal--14-*
        set columns=142 lines=58
    else
        set guifont=-ttf-lucida\ console-medium-r-normal-*-*-90-*-*-m-*-iso8859-1,Courier_New:h7:w5,Lucida_Console:h9:w5,6x10
        set columns=142 lines=66
    endif
    	

    The important parts, of course, are setting the fileencoding and the guifontset.
    Note: do not set guifont if you want to display Japanese. Make sure it is not set in any of your other startup files.


  2. Write a startup script:
    I put a convenient start script called kvim into my path. It sets the locale LANG and starts gvim.
    #!/bin/bash
    
    
    # Check if the input server is running; run it if not.
    ps u -C kinput2 |grep $USER > /dev/null || kinput2 -canna&
    # set LANG to the locale of your choice, though sjis and jis may not work well with vim.
    LANG=ja_JP.eucJP gvim $@
    	

    Alternatively, you could just set this on the command line before running gvim, with something like:
    % LANG=ja_JP.eucJP; gvim


  3. Run your script and have fun!
    Start the vim gui with your script (% kvim). If you have your LANG environment variable set already, just start gvim: % vim -g. Press Shift-Space or ctrl-Kanji to begin input conversion.


Tips:


References:



Steven Mueller, diffusor@ugcs.caltech.edu
This page created: Sat Apr 01 16:21:54 PST 2000
Last modified: Sun Nov 05 19:00:16 PST 2000
(created/edited with Vim)