欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ubuntu 16.04下解决sublime text3无法输中文问题

程序员文章站 2022-05-30 18:45:01
...

首先介绍一下常规方法。文章最后附有shell一键脚本可供懒人使用。

常规方法

一、系统已成功安装搜狗输入法
二、保存下面的代码到文件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);
    }

如果出现以下问题:
Ubuntu 16.04下解决sublime text3无法输中文问题

则输入以下命令后再编译

sudo apt-get install libgtk2.0-dev

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

gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC

四、将libsublime-imfix.so拷贝到sublime_text所在文件夹

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

五、修改文件/usr/bin/subl的内容

sudo gedit /usr/bin/subl

#!/bin/sh
exec /opt/sublime_text/sublime_text "aaa@qq.com"

修改为

#!/bin/sh
LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text "aaa@qq.com"

此时,在命令中执行 subl 将可以使用搜狗for linux的中文输入
六、为了使用鼠标右键打开文件时能够使用中文输入,还需要修改文件sublime_text.desktop的内容
sudo gedit /usr/share/applications/sublime_text.desktop
将[Desktop Entry]中的字符串

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"

将[Desktop Action 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"

将[Desktop Action Document]中的字符串

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"

这样,在图形界面里通过右键打开也可以使用搜狗输入法了。
参考文档:https://my.oschina.net/lee2013/blog/396855

一键脚本

一键脚本完全基于常规方法。只是写成了脚本而已。运行起来没有提示,没有特别意外的情况基本是直接成功的。运行完可以测试一下是否能够正常使用搜狗输入法。
下面贴上代码:

#!/bin/bash
#开始写入文件
echo "#include <gtk/gtkimcontext.h>" > ~/sublime_imfix.c
echo "  void gtk_im_context_set_client_window (GtkIMContext *context," >> ~/sublime_imfix.c
echo "            GdkWindow    *window)" >> ~/sublime_imfix.c
echo "  {" >> ~/sublime_imfix.c
echo "    GtkIMContextClass *klass;" >> ~/sublime_imfix.c
echo "    g_return_if_fail (GTK_IS_IM_CONTEXT (context));" >> ~/sublime_imfix.c
echo "    klass = GTK_IM_CONTEXT_GET_CLASS (context);" >> ~/sublime_imfix.c
echo "    if (klass->set_client_window)" >> ~/sublime_imfix.c
echo "      klass->set_client_window (context, window);" >> ~/sublime_imfix.c
echo '    g_object_set_data(G_OBJECT(context),"window",window);' >> ~/sublime_imfix.c
echo "    if(!GDK_IS_WINDOW (window))" >> ~/sublime_imfix.c
echo "      return;" >> ~/sublime_imfix.c
echo "    int width = gdk_window_get_width(window);" >> ~/sublime_imfix.c
echo "    int height = gdk_window_get_height(window);" >> ~/sublime_imfix.c
echo "    if(width != 0 && height !=0)" >> ~/sublime_imfix.c
echo "      gtk_im_context_focus_in(context);" >> ~/sublime_imfix.c
echo "  }" >> ~/sublime_imfix.c


#检查是否已安装libgtk2.0-dev
((installed=0))
dpkg -s libgtk2.0-dev > /dev/null
((installed=$?))
if  [ "$installed" = 1 ];then
    echo  -e "\033[31mlibgtk2.0-dev\033[36m has not been installed! We have to install it first!"
    echo  -e "\033[Installing \033[31mlibgtk2.0-dev\033[0m..."
    sudo sudo apt-get install libgtk2.0-dev
fi
#编译libsublime-imfix.so
gcc -shared -o ~/libsublime-imfix.so ~/sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC
#清理源码文件
rm ~/sublime_imfix.c
#移动编译文件
sudo mv ~/libsublime-imfix.so /opt/sublime_text/
#修改文件/usr/bin/subl的内容
sudo sh -c 'echo "#!/bin/sh" > /usr/bin/subl'
sudo sh -c "echo 'LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text \"\aaa@qq.com\"' >> /usr/bin/subl"

#修改/usr/share/applications/sublime_text.desktop
sudo sed -i -e 's|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"|' /usr/share/applications/sublime_text.desktop

sudo sed -i -e 's|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"|' /usr/share/applications/sublime_text.desktop

sudo sed -i -e 's|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"|' /usr/share/applications/sublime_text.desktop
echo "All done here! Try it out!"

shell脚本的运行方式
将以上代码保存为任意文件名.sh,例如myscript.sh
然后在终端中输入:

chmod +x myscript.sh
./myscript.sh

即可。