之前在Linux Mint 17一周使用体验中简单介绍过Sublime Text。
Ubuntu、Linux Mint的软件管理器中已经能够找到Sublime Text 3,直接安装即可。或者去官网下载.deb或tarball安装包,手动安装。
ST最吸引我的第一点就是这个非常棒的扩展管理器
!安装方法也很简单,在ST中按Ctrl+`进入ST的控制台,然后去官网上将ST3的安装命令拷贝到其中执行就可以完成安装了。有了它,在ST中按Ctrl+Shift+P,输入Install或Remove后,就可以随便安装、卸载各种扩展了,包括各种ST的插件、主题等等。
根据Package Control上的流行度统计,推荐安装以下扩展插件:
Package Name | Description |
---|---|
Emmet | Emmet (ex-Zen Coding) for Sublime Text |
SublimeLinter | Interactive code linting framework for Sublime Text 3 |
SideBarEnhancements | Enhancements to Sublime Text sidebar. Files and folders |
BracketHighlighter | Bracket and tag highlighter for Sublime Text |
SublimeCodeIntel | Full-featured code intelligence and smart autocomplete engine |
HTML5 | HTML5 bundle for Sublime Text |
Alignment | Easy alignment of multiple selections and multi-line selections |
Git | Plugin for some git integration into sublime text |
DocBlockr | Simplifies writing DocBlock comments in Javascript, PHP, CoffeeScript, Actionscript, C & C++ |
ConvertToUTF8 | A Sublime Text 2 & 3 plugin for editing and saving files encoded in GBK, BIG5, EUC-KR, EUC-JP, Shift_JIS, etc |
All Autocomplete | Extend Sublime autocompletion to find matches in all open files of the current window |
Markdown Preview | markdown preview and build plugin for sublime text 2/3 |
MarkdownEditing | Powerful Markdown package for Sublime Text with better syntax understanding and good color schemes. |
简单分门别类地介绍一下主要的扩展插件的用途:
Table Editor: Enable for current view
开启。|-
后,按tab键就将进入Table编辑模式。在菜单栏的Preferences->Settings-User中,我们可以对ST进行一些全局上的配置,例如网络代理、Theme主题、Scheme配色等:
{
"theme": "Soda Dark.sublime-theme",
"soda_classic_tabs": true,
"soda_folder_icons": true,
"color_scheme": "Packages/Theme - Spacegray/base16-eighties.dark.tmTheme",
"http_proxy": "ip:8080",
"proxy_password": "******",
"proxy_username": "cdai",
"ignored_packages":
[
"Vintage", "Markdown"
],
"file_exclude_patterns": [".tags", ".tags_sorted_by_file", "*.exe"]
}
ST最吸引我的第二点就是默认的开发环境配置
,其思想就是类似于Rails或Struts2中的Convention Over Configuration,智能地配置好环境,如果用户有特殊需求则自行配置。新建个hello.c文件,ST能自动识别出语言并进行语法高亮。最棒的是Ctrl+Shift+P输入Run后,选择“Run single file with G++”一项就能自动找到G++(如果已经安装的话)并执行。下载个Redis源码包解压开试试,点击“Open Folder”选择Redis根目录添加进来,然后同理Ctrl+Shift+P这次选择“Make”那一项就能自动编译!
下面就是本文的重点了,以C/C++开发为例,着重介绍一下C/C++开发的插件以及在Windows 7平台下,如何利用Cygwin或Mingw开发和交叉编译Linux程序。
首先安装插件SublimeClang智能提示、C++ Snippets代码模板,如有需要也可以自行安装CTags。
Windows平台就不属于上面提到的Convention,所以需要我们自己配置编译选项。打开Tools->Build System->New Build System:
{
"path": "D:\\cygwin64\\bin",
"cmd": ["gcc", "-I", "D:\\cygwin64\\usr\\include", "${file}", "-o", "${file_path}/${file_base_name}", "-lm", "-Wall", "&", "start", "${file_path}/${file_base_name}.exe"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c",
"shell": true,
"variants":
[
{
"name": "Run::Cygwin",
"cmd": [ "start", "${file_path}/${file_base_name}.exe"]
}
]
}
简单解释一下:
默认情况下,Clang找不到Cygwin或Mingw自带的头文件,所以需要配置一下智能提示才能好用。打开Preferences->Package Settings->SublimeClang->Settings-User:__
{
"show_output_panel": false,
"dont_prepend_clang_includes": true,
"inhibit_sublime_completions": false,
"options":
[
"-ID:\\cygwin64\\usr\\include",
"-ID:\\cygwin64\\lib\\gcc\\x86_64-pc-cygwin\\4.9.2\\include",
"-ID:\\cygwin64\\lib\\gcc\\x86_64-pc-cygwin\\4.9.2\\include\\c++",
"-ID:\\cygwin64\\lib\\gcc\\x86_64-pc-cygwin\\4.9.2\\include\\c++\\*"
]
}
注意:SublimeClang的作者已经停止更新,所以不支持Sublime Text 3了。Linux下可以按以下方法fix这个问题,Windows下更简单些,直接从SublimeClang网站上下载zip包后,解压出libclang.dll和libcache.dll放到SublimeClang/internals下就可以了:
// 手动下载SublimeClang源码
cd ~./config/sublime-text-3/Packages
git clone --recursive https://github.com/quarnster/SublimeClang SublimeClang
cd SublimeClang
git pull && git submodule foreach --recursive git pull origin master
// 拷贝libclang.so到internals文件夹
sudo apt-get install libclang-3.5
ldconfig -p | grep clang
cp /usr/lib/i386-linux-gnu/libclang-3.5.so ~/.config/sublime-text-3/Packages/SublimeClang/internals/libclang.so
// 手动编译出libcache.so
cd src
mkdir build
cd build
cmake ..
make
现在启动Sublime Text 3就可以了!
CTags是一个古老的插件,能够实现函数导航和跳转功能,在ST中使用它分为三步:
CTags: Rebuild Tags
菜单。执行后就可以在我们的项目中自由的跳转了!注意:Rebuild Tags后会在根目录下产生.tags和.tags_sorted_by_file两个索引文件。可以在Preferences->Settings-User中添加配置使其隐藏:
"file_exclude_patterns": [".tags", ".tags_sorted_by_file"]
现在就大功告成了!一个简洁快速、外观酷炫的Sublime Text可以使用了!
按照百度经验上的指导,真的好使了。而且此方法不只是针对搜狗输入法,我的Linux Mint装的是谷歌输入法也好使了,应该是各种输入法都通用吧。
1.新建sublime_imfix.c(我是在~/Temp下新建的):
#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);
}
2.编译出.so动态链接库(需要libgtk支持),并拷贝到/opt/sublime_text下:
// 编译出.so动态链接库(需要libgtk支持)
sudo apt-get install libgtk2.0-dev
gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
sudo cp ~/Temp/libsublime-imfix.so /opt/sublime_text/
3.打开/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 "$@"
4.打开/usr/share/applications/sublime_text.desktop
Exec=/opt/sublime_text/sublime_text %F
Exec=/opt/sublime_text/sublime_text -n
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 %F"
Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n"
Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file"
现在打开Sublime Text就可以输入中文了!
2018/06/17 周日
最近升级系统又升级了Sublime,反反复复修复输入法问题。所以还是麻烦一次,写个bash脚本,以后受益无穷。
#!/usr/bin/env bash
tmp="/tmp"
tmpc="sublime_imfix.c"
tmpso="libsublime-imfix.so"
cd $tmp
cat >$tmpc <#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);
}
EOL
apt-get install libgtk2.0-dev
gcc -shared -o $tmpso $tmpc `pkg-config --libs --cflags gtk+-2.0` -fPIC
cp -f $tmpso /opt/sublime_text/
subl=/usr/bin/subl
subldsk=/usr/share/applications/sublime_text.desktop
sed 's#exec /opt/sublime_text/sublime_text "$@"#LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text "$@"#' $subl
sed 's#/opt/sublime_text/sublime_text#bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text"#' $subldsk