Sublime text2 的幾個小技巧和資料彙總

Sublime text2是一個編輯神器,這是毋容置疑的。從vim轉到sublime text2後,我感覺更爲輕鬆了。當然,我也喜歡vim。所以,我喜歡把我的sublime text2設置成vim模式。

這樣,就可以結合vim和sublime text2的優點了。本文,我做一些簡單的總結,希望對大家有所幫助。

雖然,現在已經有sublime text3了,但是還是喜歡 sublime text2。:-) 本文是介紹sublime text2 。

環境: mac + sublime text2

  1. 安裝 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 即可

  2. 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的用戶

  3. 置自己的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"]
            }
        ]
    }



  4. ST2的插件
    1. All autocomplete
      自動補全插件,先裝着吧。 可以結合後面的ctags 做補全。

    2. 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 

    3. PlainTasks
      簡單的todo-list task管理工具

    4. markdown preview
      在ST中,查看markdown 文檔,非常的方便

    5. Fetch 可以從遠程 fetch 代碼到本地


    6. snippet 系列插件: jquery mobile snippet  等


    7. AdvanceNewFile
      提高我們創建和修改文件名稱的快捷操作,還是有其的功能

    8. Pretty JSON
      格式化我們的json代碼,有時候特別有用

    9. Bracket​Highlighter
      括號高亮,看代碼的利器啊

    10. Prefixr
      寫跨平臺的css的插件,能自動生成。綁定的快捷鍵是 super+alt+x


    11. 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"]
      }


    12. 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
    13. zenCoding
      提高開發速度的神器,少寫很多代碼,只是有態度的內容需要記了

    14. SideBarEnhancements
      增強我們的邊欄功能

    15. Shell-Turtlestein
      在sublime text2中直接執行shell命令,因爲跟 simple rails nav 插件的快捷鍵有衝突,需要我們自己手動修改
      {"keys": [" ", "c", "m"], "command": "shell_prompt"}




  5. 打造自己的Rails編輯器
    1. Rails Related File
      快速定位到我們需要的相關的Rails 文件,比如通過controller 尋找 helper/views 等
      命令是 command + shift+ O

    2. 使用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的信息


    3. 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  



  6. 常用的快捷鍵和命令
    光標操作我們代碼
    $ 移動到 行首
    0 移動到行尾
    ^ 移動到第一個非空字符
    j 下
    k 上
    h 左
    l 右邊
    v+j... 選中

    移動10行,使用 10+j

  7. 我的快捷鍵設置分享
    [
    	{
    		"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




發佈了198 篇原創文章 · 獲贊 8 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章