您的位置:首页 > 其它

Sublime text2 的几个小技巧和资料汇总

2012-11-20 13:32 399 查看
Sublime text2是一个编辑神器,这是毋容置疑的。从vim转到sublime text2后,我感觉更为轻松了。当然,我也喜欢vim。所以,我喜欢把我的sublime text2设置成vim模式。

这样,就可以结合vim和sublime text2的优点了。本文,我做一些简单的总结,希望对大家有所帮助。

虽然,现在已经有sublime text3了,但是还是喜欢 sublime text2。:-) 本文是介绍sublime text2 。

环境: mac + sublime text2

安装 Package Control 插件

PC插件是我们安装其他插件的必要条件,安装也特别简单。

command+`


然后输入

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'


回车即可

如何调用 Package Control ?

command + shift +p, 然后输入 package ,选中 Package Control: install package 即可

default user settings

如下是我的default user settings

{
	"auto_complete": true,
	"bold_folder_labels": true,
	"caret_style": "phase",
	"default_line_ending": "unix",
	"ensure_newline_at_eof_on_save": true,
	"font_size": 14.0,
	"highlight_line": true,
        "ignored_packages": [],
	"rulers":
	[
		100
	],
	"scroll_past_end": true,
	"tab_size": 2,
	"translate_tabs_to_spaces": true,
	"trim_trailing_white_space_on_save": true,
	"vintage_ctrl_keys": true,
	"vintage_start_in_command_mode": true
}


"ignored_packages": [],
ST2 默认情况下,会disable掉 vim 的功能支持,如果习惯用vim的用户

置自己的Project

我们在用sublime text2 打开一个文件夹后,可以将该文件夹保存成一个Project。这样的好处是,我们可以往一个Project下增加不同的目录,并且,可以设置名称。

还可以通过快捷键切换不同的Project.

具体的操作是: Project -> save project

参考文件是

{
    "folders":
    [
        {
            "path": "src",
            "folder_exclude_patterns": ["backup"]
        },
        {
            "path": "docs",
            "name": "Documentation",
            "file_exclude_patterns": ["*.css"]
        }
    ],
    "settings":
    {
        "tab_size": 2
    },
    "build_systems":
    [
        {
            "name": "List",
            "cmd": ["ls"]
        }
    ]
}


ST2的插件

All autocomplete

自动补全插件,先装着吧。 可以结合后面的ctags 做补全。

Gist

在ST2中,集成github的gist插件。有了它,我们可以在ST2中创建,修改,删除和使用gist代码。

Gist的配置

Applications -> Personal Access Tokens -> generate a token

Use the token to Gist setting file like this:

{
  "token": "614f564034a3400a7d4ad5bb5b0d18f2c3c5d889"
}


command+shift+p: input gist: open xxx

PlainTasks

简单的todo-list task管理工具

markdown preview

在ST中,查看markdown 文档,非常的方便

Fetch 可以从远程 fetch 代码到本地

snippet 系列插件: jquery mobile snippet 等

AdvanceNewFile

提高我们创建和修改文件名称的快捷操作,还是有其的功能

Pretty JSON

格式化我们的json代码,有时候特别有用

Bracket​Highlighter

括号高亮,看代码的利器啊

Prefixr

写跨平台的css的插件,能自动生成。绑定的快捷键是 super+alt+x

ctags

必装插件,用于代码之间的跳转

mac自带的 ctags 不行,换成 brew install ctags 的方法

sudo rm /usr/bin/ctags
sudo ln -s /usr/local/Cellar/ctags/5.8/bin/ctags /usr/bin/ctags


如下的code可以为我们的Gem tag,在阅读代码的时候特别有用

1. 在项目下,新建一个ctags_for_ruby 的文件,code 如下

#!/usr/bin/env ruby
system "find . -name '*.rb' | ctags -f .tags -L -"

if File.exist? './Gemfile'
  require 'bundler'
  paths = Bundler.load.specs.map(&:full_gem_path).join(' ')
  system "ctags -R -f .gemtags #{paths}"
end

2. 运行 ctags_for_ruby

3. 修改ctags的配置文件为

{
    "debug"           :  false,
    "autocomplete": false,
    "command"   :  "ctags_for_ruby",
    "filters"         :  {
        "source.python": {"type":"^i$"}
    },
    "definition_filters": {
        "source.php": {"type":"^v$"}
    },
    "definition_current_first": true,
    "show_context_menus": true,
    "extra_tag_paths" :  [ [["source.python", "windows"], "C:\\Python27\\Lib\\tags"]],
    "extra_tag_files" : [".gemtags", ".tags"]
}


Gem Browser

可以方便的打开一个Gem文件目录, 不同的rvm 可以参考 https://github.com/fblee/sublime-gem-browser-gemset
使用方法: command + shift + p 输入 List Gems
https://github.com/NaN1488/sublime-gem-browser
cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
  git clone https://github.com/NaN1488/sublime-gem-browser.git[/code] 
zenCoding

提高开发速度的神器,少写很多代码,只是有态度的内容需要记了

SideBarEnhancements

增强我们的边栏功能

Shell-Turtlestein

在sublime text2中直接执行shell命令,因为跟 simple rails nav 插件的快捷键有冲突,需要我们自己手动修改

{"keys": [" ", "c", "m"], "command": "shell_prompt"}


打造自己的Rails编辑器

Rails Related File

快速定位到我们需要的相关的Rails 文件,比如通过controller 寻找 helper/views 等

命令是 command + shift+ O

使用RubyTest插件测试我们的ruby
https://github.com/maltize/sublime-text-2-ruby-tests#note
几处需要修改的配置

"check_for_rbenv": true,
  "check_for_rvm": true,
  "run_rspec_command": "bundle exec rspec {relative_path}",
  "run_single_rspec_command": "bundle exec rspec {relative_path} -l{line_number}",
  "ruby_use_scratch" : true,
  "save_on_run": true,


ruby_use_scratch
设置成true 后,ST2会在一个新的tab输出rspec的信息

simple rails nav

Rails 文件的更快的跳转

安装后,需要自己配置快捷键,如下可供参考

// simple navigate rails
	{ "keys": ["super+shift+m"], "command": "list_rails_models" },
	{ "keys": ["super+shift+c"], "command": "list_rails_controllers" },
	{ "keys": ["super+shift+v"], "command": "list_rails_views" },
	{ "keys": ["super+shift+h"], "command": "list_rails_helpers" },
	{ "keys": ["super+shift+x"], "command": "list_rails_fixtures" },
	{ "keys": ["super+shift+t"], "command": "list_rails_tests" },
	{ "keys": ["super+shift+i"], "command": "list_rails_javascripts" },
	{ "keys": ["super+shift+y"], "command": "list_rails_stylesheets" },
	{ "keys": [" ", "m"], "command": "list_rails_models", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "c"], "command": "list_rails_controllers", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "v"], "command": "list_rails_views", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "h"], "command": "list_rails_helpers", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "x"], "command": "list_rails_fixtures", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "t"], "command": "list_rails_tests", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "i"], "command": "list_rails_javascripts", "context": [{"key": "setting.command_mode"}] },
	{ "keys": [" ", "y"], "command": "list_rails_stylesheets", "context": [{"key": "setting.command_mode"}] }
https://github.com/noklesta/SublimeRailsNav

常用的快捷键和命令

光标操作我们代码

$ 移动到 行首
0 移动到行尾
^ 移动到第一个非空字符
j 下
k 上
h 左
l 右边
v+j... 选中


移动10行,使用 10+j

我的快捷键设置分享

[
{
"keys": ["super+o"],
"command": "side_bar_open_in_browser",
"args":{ "paths":[], "type":"production" }
},
{ "keys": ["alt+r"], "command": "side_bar_rename" },
{ "keys": ["alt+n"], "command": "advanced_new_file"},
{ "keys": ["super+shift+up"], "command": "select_lines", "args": {"forward": false} },
{ "keys": ["super+shift+down"], "command": "select_lines", "args": {"forward": true} },
{"command": "navigate_to_definition", "keys": ["alt+command+down"] },
{"command": "jump_back", "keys": ["alt+command+up"] },
{"command": "rebuild_tags", "keys": ["alt+command+r"] },
{"keys": ["ctrl+shift+a"], "command": "alignment"},

{
"keys": ["super+alt+left"],
"command": "set_layout",
"args":
{
"cols": [0.0, 0.33, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
}
},

{
"keys": ["super+alt+right"],
"command": "set_layout",
"args":
{
"cols": [0.0, 0.66, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
}
},

{ "keys": ["alt+1"], "command": "move_to_group", "args": { "group": 0 } },
{ "keys": ["alt+2"], "command": "move_to_group", "args": { "group": 1 } },
{ "keys": ["alt+3"], "command": "move_to_group", "args": { "group": 2 } },
{ "keys": ["alt+4"], "command": "move_to_group", "args": { "group": 3 } },

//simple special file nav: https://github.com/fblee/rails_special_file_nav { "keys": ["super+ctrl+g"], "command": "gemfile_navigation"},
{ "keys": ["super+ctrl+r"], "command": "rakefile_navigation"},

// simple navigate rails { "keys": ["super+shift+m"], "command": "list_rails_models" }, { "keys": ["super+shift+c"], "command": "list_rails_controllers" }, { "keys": ["super+shift+v"], "command": "list_rails_views" }, { "keys": ["super+shift+h"], "command": "list_rails_helpers" }, { "keys": ["super+shift+x"], "command": "list_rails_fixtures" }, { "keys": ["super+shift+t"], "command": "list_rails_tests" }, { "keys": ["super+shift+i"], "command": "list_rails_javascripts" }, { "keys": ["super+shift+y"], "command": "list_rails_stylesheets" }, { "keys": [" ", "m"], "command": "list_rails_models", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "c"], "command": "list_rails_controllers", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "v"], "command": "list_rails_views", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "h"], "command": "list_rails_helpers", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "x"], "command": "list_rails_fixtures", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "t"], "command": "list_rails_tests", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "i"], "command": "list_rails_javascripts", "context": [{"key": "setting.command_mode"}] }, { "keys": [" ", "y"], "command": "list_rails_stylesheets", "context": [{"key": "setting.command_mode"}] }

//rubytest
// {"command": "run_single_rspec_command", "keys": ["super+s", "super+shift+r"]}
]


参考资料

视频: http://net.tutsplus.com/articles/news/perfect-workflow-in-sublime-text-free-course/
ctags 和 auto complete 结合: https://gist.github.com/BlackMac/1825401 https://thunderboltlabs.com/blog/2013/11/19/efficiency-with-sublime-text-and-ruby/ http://www.cnblogs.com/xiaowu/archive/2012/08/27/2658534.html http://blog.alainmeier.com/post/27255145114/some-things-beginners-might-not-know-about-sublime-text
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: