Ubuntu下Sublime Text 3无法输入中文问题.md

注意:必须是官方安装方法。snap安装方式因为没有权限改不了。。。官方安装sublime text 3方法参考: https://www.sublimetext.com/d…

  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. 将 sublime_imfix.c 编译成共享库libsublime-imfix.so,命令如下:

    # ps: 如果缺少组件,则:sudo apt-get install libgtk2.0-dev
    gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC
  3. 然后将libsublime-imfix.so拷贝到sublime_text所在文件夹

    sudo mv libsublime-imfix.so /opt/sublime_text/
  4. 修改文件/usr/bin/subl的内容

    sudo 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 将可以使用搜狗for linux的中文输入
  5. 完美解决问题:还需要修改文件sublime_text.desktop的内容

    sudo vim /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"
    
    注意:修改时请注意双引号"",否则会导致不能打开带有空格文件名的文件。
    原文作者:xinhaip
    原文地址: https://segmentfault.com/a/1190000016902690
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞