sublime text3 安裝與配置

sublime text 3

本文來探究一下Ubuntu和Deepin下sublime text 3的安裝與配置

1. 安裝

1.1 下載與安裝

點擊打開官網

wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -

sudo apt install apt-transport-https

echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list

sudo apt update

sudo apt install sublime-text

1.2 激活

注意:先增加Host 屏蔽,然後輸入祕鑰才行

Windows : c:/windows/system32/drivers/etc/hosts

Linux : /etc/hosts

#127.0.0.1       www.sublimetext.com
127.0.0.1       license.sublimehq.com
----- BEGIN LICENSE -----
sgbteam
Single User License
EA7E-1153259
8891CBB9 F1513E4F 1A3405C1 A865D53F
115F202E 7B91AB2D 0D2A40ED 352B269B
76E84F0B CD69BFC7 59F2DFEF E267328F
215652A3 E88F9D8F 4C38E3BA 5B2DAAE4
969624E7 DC9CD4D5 717FB40C 1B9738CF
20B3C4F1 E917B5B3 87C38D9C ACCE7DD8
5F7EF854 86B9743C FADC04AA FB0DA5C0
F913BE58 42FEA319 F954EFDD AE881E0B
------ END LICENSE ------
用戶名:Caitingting,序列號:E8DD-289C-3F20-AA7D

2. 無法輸入中文

法一

以下方法是最快捷有效的方法,然而,可以輸入中文了,但是,選擇文件->右鍵->用sublime text 3打開,你會發現打不開文件了.但是先打開sublime,然後在裏面選擇文件又可以打開

git clone https://github.com/jfcherng/my_scripts

sudo apt install -y fcitx

sudo apt update

sudo apt install -y build-essential libgtk2.0-dev

cd my_scripts/sublime_text/sublime_text_fcitx

gcc -Os -shared -o libsublime-imfix.so sublime_imfix.c $(pkg-config --libs --cflags gtk+-2.0) -fPIC

sudo mv -f libsublime-imfix.so /opt/sublime_text

sudo cp -f subl "$(which subl)"

sudo cp -f sublime_text.desktop /usr/share/applications/

sudo cp -f sublime_text.desktop /opt/sublime_text

法二:

sudo apt-get update && sudo apt-get upgrade
git clone https://github.com/lyfeyaj/sublime-text-imfix.git
cd sublime-text-imfix && ./sublime-imfix

完成!重新啓動後就可以在Sublime Text 3中使用搜狗輸入法輸入中文了

這裏值得一提的是,博主用的是deepin,然後,一旦將st3固定在下面,然後,很快就又不能輸入中文了,卸載重裝即可

3. 插件

3.1 安裝package control

爲了使用衆多的插件來擴展 Sublime 的功能,你需要安裝一個叫做 Package Control 的插件管理器,一旦你安裝好了以後,你就可以使用 Package Control來安裝,移除或者升級所有的 ST3 插件了。

  1. Ctrl+~打開控制檯
  2. https://packagecontrol.io/installation#st3 獲取安裝代碼,這裏如下
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
  1. 輸入完了按Enter就行

  1. 現在你可以通過快捷鍵 Ctrl+Shift+P 打開 Package Control來安裝其他的插件了。輸入 install 然後你就能看見屏幕上出現了 Package Control: Install Package,點擊回車然後搜索你想要的插件。想裝什麼直接點擊。注意看下面的status bar是顯示進度的地方

3.2 安裝Anaconda python插件

Anaconda 是一個終極 Python 插件。它爲 ST3 增添了多項 IDE 類似的功能,其具體配置文件在 https://github.com/DamnWidget/anaconda 例如:

  • Autocompletion:自動完成,該選項默認開啓,按TABCtrl+Space顯示代碼提示窗口

  • Goto Definitions:能夠在你的整個工程中查找並且顯示任意一個變量,函數,或者類的定義。,鍵盤按下:Ctrl+Alt+g

  • Find Usage 使用此命令,用戶可以找到正在使用符號(變量,函數,方法,類或模塊)的所有位置。鍵盤按下:Ctrl+Alt+f

  • Display Signatures:能夠顯示一個函數或者類的說明性字符串(當然,是在定義了字符串的情況下)

  • Show Documentation: 可以查找並向用戶顯示任何函數,方法,類,模塊或包的文檔字符串。用戶只需將光標放在想要獲取文檔字符串的符號上(或在括號之後,例如在寫入之後sys.exit()),然後鍵盤按下Ctrl+Alt+d

  • Code linting:使用支持 pep8 標準的 PyLint 或者 PyFlakes。因爲我個人使用的是另外的 linting 工具,所以我會在 Anaconda 的配置文件 Anaconda.sublime-settings中將 linting 完全禁用。操作如下: Sublime > Preferences > Package Settings > Anaconda > Settings – User: {"anaconda_linting": false}

  • 最後,配置一下Anaconda

{

"python_interpreter":"/usr/bin/python3",

"anaconda_linting": false,

//保存文件後自動pep8格式化

"auto_formatting": true,

// st3自動補全
"suppress_word_completions": true,
"suppress_explicit_completions": true,
//"complete_parameters": true
}

3.3 SublimeREPL插件

SublimeREPL會新建一個交互式命令行界面,讓你的py可以有輸入

配置python3環境

  1. Preferences->Browse Packages,找到SublimeREPL的文件夾,再進入config文件夾,可以看到許多語言的配置文件,Python也在裏面

  1. config文件夾下新建python3文件夾,在裏面新建Default.sublime-commandsMenu.sublime-menu兩個文件(模仿Python文件夾),因爲我們Python3目前只要能打開shell運行,和運行這個腳本,兩個功能,因此就只要包含Python3Python3 – Run current file兩項就好了
  2. Default.sublime-commands配置如下:
[
    {
        "caption": "SublimeREPL: Python3",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3",
            "file": "config/Python3/Main.sublime-menu"
        }
    },

    {
        "caption": "SublimeREPL: Python3 - RUN current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3_run",
            "file": "config/Python3/Main.sublime-menu"
        }
    }

]
  1. Menu.sublime-menu配置如下:
[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"caption": "Python3",
                "id": "Python3",

                 "children":[
                    {"command": "repl_open",
                     "caption": "Python3",
                     "id": "repl_python3",
                     "mnemonic": "P",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-i", "-u"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "repl_open",
                     "caption": "Python3 - RUN current file",
                     "id": "repl_python3_run",
                     "mnemonic": "R",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-u", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    }
                ]}
            ]
        }]
    }
]
  1. 保存文件後,就可以Tools->SublimeREPL->Python3運行命令了

  1. 爲了好看,採取和Ctr+B同樣的上下佈局

設置key binding

每次這樣到菜單欄裏去找,太慢,能不能像ctrl+B一樣直接運行呢?可以的,只要設置快捷鍵就好了,在Preference->key Bindings-User

寫入如下配置,即可F4運行python2.7 repl, F5 運行python 3 repl,注意id還是要和Menu.sublime-menu文件裏的id要一致,

[
    {
        "keys":["f4"],

        "caption": "SublimeREPL: Python - RUN current file",

        "command": "run_existing_window_command", "args":

        {

            "id": "repl_python_run",

            "file": "config/Python/Main.sublime-menu"

        }

    },

    {
        "keys":["f5"],

        "caption": "SublimeREPL: Python3 - RUN current file",

        "command": "run_existing_window_command", "args":

        {

            "id": "repl_python3_run",

            "file": "config/Python3/Main.sublime-menu"

        }

    }
]

can’t open file ‘$file_basename’: [Errno 2] No such file or directory

這個只要再次用鼠標點擊一下test.py就好了,就可以獲取運行的文件了

3.4 SublimeTmpl

新建文件模板

  1. settings-user寫入以下信息
{
    "disable_keymap_actions": false, // "all"; "html,css"
    "date_format" : "%Y-%m-%d %H:%M:%S",
    "attr": {
        "author": "江南小蟲蟲",
        "email": "[email protected]",
        "link": "http://www.jnxcc.top"
    }
}

  1. Ctlr+Alt+Shift+P:默認創建python文件,這裏進行修改.在key bindings-user中添加了以下信息,意思ctrl+alt+n就可以創建一個新的Python模板
     {
         "caption": "Tmpl: Create python", "command": "sublime_tmpl",
         "keys": ["ctrl+alt+n"], "args": {"type": "python"}
     },

4. 配置

4.1 隱藏mipmap和打開packages所在目錄

  1. View->Hide minimap:隱藏minimap

  1. Preferences->Browse Packages:打開packages所在目錄

4.2 設置運行python3

sublime默認的是python2.7如果我想讓他運行python3,怎麼辦呢?

  1. 運行which命令找到python3的路徑
which python3`

  1. 自定義環境:Tools->Build System->New Build System,會彈出一個後綴爲sublime-build的文件。

將以下配置複製粘貼進去,如果發現pyqt5運行的時候啥也不顯示,可將"shell":"true"刪掉

{
    "cmd": ["/usr/bin/python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "shell":"true"
}

windows粘貼下面的:

{
	"cmd":["E:\\Python\\Python36-32\\python.exe","-u","$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
	"encoding": "utf-8" ,
	"env": {"PYTHONIOENCODING": "utf8"},
	"shell":"true"
}

記住,其中的python3運行路徑要和你係統中的路徑一致,然後按Ctlr+S保存文件,文件名改爲爲python3.sublime-build保存的路徑就是Crtl+S後默認的路徑,然後你在Tools->Build System,可以看到python3了,選擇它再運行python,就會使用python3而不是python2.7了

4.3 一些常用的配置

打開Preferences->Settings,可以看到右邊有個settings-User,我們修改這個文件就行

.將一下配置粘貼進去就行

{
    "font_size": 13,
    "ignored_packages":
    [
    	"Vintage"
    ],
    "theme": "Boxy Monokai.sublime-theme",
    // 使用空格代替tab
    "translate_tabs_to_spaces": true,
    // 高亮未保存文件
    "highlight_modified_tabs": true,
    // 默認編碼格式
    "default_encoding": "UTF-8",
    // 窗口失焦立即保存文件
    "save_on_focus_lost": true,
    // 保存時自動去除行末空白
    "trim_trailing_white_space_on_save": true,
    // 保存時自動增加文件末尾換行,這樣 git 提交時不會生產額外的 diff
    "ensure_newline_at_eof_on_save": true,
    // 當前行高亮
    "highlight_line": true,
    // 設置行間距,看起來不那麼"擠"
    "line_padding_bottom": 1,
    "line_padding_top": 1
}

另一種的

{
	"color_scheme": "Packages/Boxy Theme/schemes/Boxy Monokai.tmTheme",
	"default_encoding": "UTF-8",
	"ensure_newline_at_eof_on_save": true,
	"font_size": 12,
	"highlight_line": true,
	"highlight_modified_tabs": true,
	"ignored_packages":
	[
		"Vintage"
	],
	"line_padding_bottom": 1,
	"line_padding_top": 1,
	"save_on_focus_lost": true,
	"theme": "Boxy Monokai.sublime-theme",
	"translate_tabs_to_spaces": true,
	"trim_trailing_white_space_on_save": true,
	"update_check": false
}

4.4 Sublime常用快捷鍵

Ctrl+] 向右縮進
Ctrl+[ 向左縮進
Ctrl+Z 撤銷。
Ctrl+Y 恢復撤銷。
Ctrl+F 打開底部搜索框,查找關鍵字。
Ctrl+A 選中全文
Ctrl+B 運行python
Ctrl+Shift+P 調出命令窗
Ctrl+shift+R 格式化
Ctrl+shift+V 格式化粘貼 這個很有用,可以把網上的代碼按照我的格式粘到我的文檔裏
Ctrl+//註釋 這個比較厲害,如果是python,就是加#號的,想取消再弄一次就好了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章