Ubuntu下Sublime Text 3解决无法输入中文的方法

准备:为了方便直接切换到切换到root

sudo -s

1.保存下面的代码到文件sublime_imfix.c(位于~目录)

#include <gtk/gtkimcontext.h>

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

}

2.将上一步的代码编译成共享库libsublime-imfix.so,命令:

cd ~

gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC
注意:如果出现fatal error: gtk/gtkimcontext.h: 没有那个文件或目录那么只需要执行命令:apt-get install libgtk2.0-dev,之后再编译sublime_imfix.c文件即可

3.然后将libsublime-imfix.so拷贝到sublime_text所在文件夹

sudo mv libsublime-imfix.so /opt/sublime_text/

4.修改文件/usr/bin/subl的内容

vim /usr/bin/subl
修改的内容如下:
#!/bin/sh
exec /opt/sublime_text/sublime_text “$@”
修改为:
#!/bin/sh

LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text "$@"

此时,在命令中执行 subl 将可以使用中文输入,效果如下:

5.为了使用鼠标右键打开文件时能够使用中文输入,还需要修改文件sublime_text.desktop的内容。

vim /usr/share/applications/sublime-text.desktop
如果不会用vim可以使用gedit /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=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=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=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,但可以正常使用了。

opt/sublime_text/目录下的sublime-text.desktop可以修改,也可不修改。修改的内容一样就可以。

 

posted @ 2017-06-30 12:05  果冻栋吖  阅读(367)  评论(0编辑  收藏  举报