解决Sublime在LinuxMint下无法输入中文的问题

@lianst  November 18, 2016

环境说明:
操作系统: Linux Mint 18 MATE
输入法管理:fcitx

一、创建sublime_imfix.c文件

进入sublime安装目录/opt/sublime_text创建sublime_imfix.c文件并复制以下代码到文件中。

#include 
void gtk_im_context_set_client_window (GtkIMContext *context,
         GdkWindow    *window)
{
 GtkIMContextClass *klass;
 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
 klass = GTK_IM_CONTEXT_GET_CLASS (context);
 if (klass->set_client_window)
   klass->set_client_window (context, window);
 g_object_set_data(G_OBJECT(context),"window",window);
 if(!GDK_IS_WINDOW (window))
   return;
 int width = gdk_window_get_width(window);
 int height = gdk_window_get_height(window);
 if(width != 0 && height !=0)
   gtk_im_context_focus_in(context);
}

二、编译共享库

stone-Inspiron-N5110 sublime_text # gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC

三、修改subl内容

stone-Inspiron-N5110 sublime_text # which subl
/usr/bin/subl
stone-Inspiron-N5110 sublime_text # vim /usr/bin/subl
#!/bin/sh
LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text "$@"

此时,在命令行运行subl已经可以中文了。

四、修改桌面图标

为了使桌面图标打开也能输入中文,我们还需要做如下修改。
修改/usr/share/applications/sublime_text.desktop文件中的如下内容:

stone-Inspiron-N5110 sublime_text # vim /usr/share/applications/sublime_text.desktop
....
[Desktop Entry]
Version=1.0
Type=Application
Name=Sublime Text
GenericName=Text Editor
Comment=Sophisticated text editor for code, markup and prose
#Exec=/opt/sublime_text/sublime_text %F  //注释原有的
Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text %F"  //修改为这一句 
Terminal=false
MimeType=text/plain;
Icon=sublime-text
Categories=TextEditor;Development;
StartupNotify=true
Actions=Window;Document;

[Desktop Action Window]
Name=New Window
#Exec=/opt/sublime_text/sublime_text -n  //注释原有的
Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n" //修改为这一句 
OnlyShowIn=Unity;

[Desktop Action Document]
Name=New File
#Exec=/opt/sublime_text/sublime_text --command new_file  //注释原有的
Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file" //修改为这一句 
OnlyShowIn=Unity;

此处仅修改了/usr/share/applications/sublime-text.desktop,在终端执行subl打开sublime可以正常输入中文了。

但是点击文件右键选择sublime打开的文件还是无法输入中文,这时需要把/usr/share/applications/sublime-text.desktop文件复制到opt/sublime_text/目录下:

stone-Inspiron-N5110 桌面 # cp /usr/share/applications/sublime_text.desktop /opt/sublime_text/

五、测试

参考资料:http://jingyan.baidu.com/article/f3ad7d0ff8731609c3345b3b.html


添加新评论