Allow sublime editor to enter Chinese on Ubuntu

Source: Internet
Author: User
Tags gtk sublime editor sublime text sublime text editor

Sublime text is a very powerful lightweight code editor, the introduction and use of features can be seen in my other article description http://www.cnblogs.com/jaxu/p/5037547.html

However, on the Ubuntu system Sublime text does not support the input of Chinese, how to solve it?

Search for Baidu and Google, found on GitHub a solution, after trying to use.

The basic idea is to write a hotfix in C and then compile it into a shared library, move to the installation directory of sublime text, and bring the shared library with you when you start. Steps and shell commands have been written very clearly on GitHub, and I'll describe them in more detail here.

1. Write the sublime-imfix.c source file and put it in the ~/home directory (in fact any directory can). Source code files are downloaded on GitHub, I also give a copy here, convenient copy:

/*Sublime-imfix.cuse ld_preload to interpose some function to fix sublime Input method support for Linux. by Cjacker Huang <jianzhong.huang at I-soft.com.cn>gcc-shared-o libsublime-imfix.so sublime_imfix.c ' Pkg-config- -libs--cflags gtk+-2.0 '-fpicld_preload=./libsublime-imfix.so sublime_text*/#include<gtk/gtk.h>#include<gdk/gdkx.h>typedef gdksegment Gdkregionbox;struct_gdkregion{Longsize; Longnumrects; Gdkregionbox*rects; Gdkregionbox extents;}; Gtkimcontext*Local_context;voidGdk_region_get_clipbox (ConstGdkregion *Region , Gdkrectangle*rectangle) {G_return_if_fail ( region!=NULL); G_return_if_fail (Rectangle!=NULL); Rectangle->x = region->extents.x1; Rectangle->y = region->extents.y1; Rectangle->width = region->extents.x2-region->extents.x1; Rectangle->height = region->extents.y2-region->extents.y1;  Gdkrectangle rect; Rect.x= rectangle->x; Rect.y= rectangle->y; Rect.width=0; Rect.height= rectangle->height; //The caret width is 2; //maybe sometimes we'll make a mistake, but for most of the time, it should is the caret.  if(Rectangle->width = =2&&Gtk_is_im_context (Local_context))  {gtk_im_context_set_cursor_location (local_context, rectangle); }}//This was needed, for example, if you input something in file dialog and return to the edit area//context would lost, so here we set it again.StaticGdkfilterreturn Event_filter (gdkxevent *xevent, Gdkevent *Event, Gpointer Im_context) {XEvent*xev = (XEvent *) xevent; if(Xev->type = = Keyrelease &&Gtk_is_im_context (Im_context)) {Gdkwindow* win = G_object_get_data (G_object (Im_context),"window"); if(Gdk_is_window (Win)) Gtk_im_context_set_client_window (Im_context, Win); }    returngdk_filter_continue;}voidGtk_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); if(!Gdk_is_window (WINDOW))return; G_object_set_data (G_object (context),"window", window); intwidth =gdk_window_get_width (window); intHeight =gdk_window_get_height (window); if(Width! =0&& Height! =0) {gtk_im_context_focus_in (context); Local_context=context; } gdk_window_add_filter (window, event_filter, context); }

2. Install the c\c++ compilation environment and GTK Libgtk2.0-dev with the following command.

sudo Install build-essential libgtk2. 0-dev

3. Execute the following command to compile the shared library. The directory where the sublime-imfix.c file is located is required to execute the command.

gcc -shared-o libsublime-imfix.so sublime-imfix.c 'pkg-config --libs--cflags gtk+-2.0 '- FPIC

4. Move the compiled library to the installation directory of the sublime text. If you downloaded the. deb file directly from the sublime text website and installed it, the default location on my machine is /opt/sublime_text

MV Libsublime-imfix.so/opt/sublime_text

Note the directory where the sublime-imfix.c file is located is required to execute the above command.

5. Start the sublime text editor with the command below to see if you can enter Chinese?

Ld_preload=./libsublime-imfix.so./sublime_text

6. Of course you don't want to be at the terminal every time by entering the command to open the sublime text editor, switch to the /usr/share/applications directory, edit the contents of the Sublime_text.desktop file as follows.

cd/usr/share/applications/
[Desktop entry]version=1.0Type=ApplicationName=Sublime Textgenericname=Text editorcomment=sophisticated Text Editor forcode, markup and Prose#exec=/opt/sublime_text/sublime_text%FExec =bash-c "ld_preload=/opt/sublime_text/libsublime-imfix.so/opt/sublime_text/sublime_text "Terminal=falseMimeType=text/Plain;icon=sublime-textcategories=texteditor;development; Startupnotify=trueActions=window;document; [Desktop Action Window] Name=New windowexec=/opt/sublime_text/sublime_text-Nonlyshowin=Unity; [Desktop Action Document] Name=New fileexec=/opt/sublime_text/sublime_text--Command New_fileonlyshowin=unity;

Notice the highlighted section above and replace the start command in the 5th step with the position of the relative path.

7. Then there is no then. Click on the sublime text icon in the dash or launch bar, create a new file in the open window, and switch input to Chinese!

Allow sublime editor to enter Chinese on Ubuntu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.