如何使用 toml 配置 SpaceVim


配置 SpaceVim 主要包括以下幾個內容:

  • 設置 SpaceVim 選項
  • 啟動/禁用模塊
  • 添加自定義插件
  • 添加自定義按鍵映射以及插件配置

設置SpaceVim選項

原先,在 init.vim 文件內,我們可以通過 let g:spacevim_* 這樣的語句來設置SpaceVim選項。在新版的 SpaceVim 中,
我們采用了 toml 作為默認配置文件,如果不熟悉 toml 語法的,可以先閱讀一下 toml 的基本語法,當然不讀也沒關系,
toml 已經是最簡單的配置文件格式了。

所有的 SpaceVim 選項配置在一個字典里,key 為原先的選項名去除 g:spacevim_ 前綴:

g:spacevim_enable_guicolors -> enable_guicolors

這一選項的值可為 true 或者 false,於是,寫入配置即為:

[options]
    enable_guicolors = false

一些其他選項,有的值是數字,有的是字符串,字符串的格式和 vim script 類似,可以用單引號,也可以用雙引號,比如:

[options]
    enable_guicolors = false
    snippet_engine = "neosnippet"
    statusline_separator = 'arrow'
    sidebar_width = 30

啟用/禁用 模塊

SpaceVim 內置了很多模塊,每一個模塊由一些插件和相關配置組成,用於提供一些特定的功能,比如提供模糊搜索的模塊,
提供版本控制的模塊,以及提供語言開發支持的語言模塊。

啟用或者禁用模塊,需要遵循一定的語法結構,並且配到 layers 列表內,比如我現在需要啟用 shell 模塊,設置模塊選項
default_positiondefault_height, 這兩個選項分別控制這 shell 窗口打開位置和高度:

[[layers]]
    name = "shell"
    default_position = "top"
    default_height = 30

如果要禁用一個模塊,需要增添一個選項 enable, 並賦值 false,默認這個是 true。比如,我需要禁用 shell 模塊,
可以這么寫, 禁用模塊時,除了 enable 這選項,其他選項可寫可不寫,因為已經不會生效。當然如果為了快速啟用/禁用模塊,
可以保持其他選項不變。

[[layers]]
    name = "shell"
    enable = false

添加自定義插件

自定義插件配置語法和模塊有點類似,將需要配置的插件,配置進 custom_plugins 列表。比如,我需要添加 2 個插件,
可以參考以下語法:

[[custom_plugins]]
    name = "lilydjwg/colorizer"
    merged = 0

[[custom_plugins]]
    name = "tpope/vim-scriptease"
    merged = 0
    on_cmd = "Scriptnames"

大家可以看到,在添加自定義插件時,我們支持很多選項,這歸功於dein, dein 支持如下常用
選項:

Option Type Description
name string A name for the plugin. If it is omitted, the tail of the repository name will be used
rev string The revision number or branch/tag name for the repo
build string Command to run after the plugin is installed
on_ft string or list Load a plugin for the current filetype
on_cmd string or list Load the plugin for these commands
rtp string You can use it when the repository has the Vim plugin in a subdirectory
if string or number If it is String, dein will eval it.
merged number If set to 0, dein doesn't merge the plugin directory.

自定義快捷鍵及插件配置

最后,我們來說下,如果添加自定義配置,和自定義快捷鍵。在使用 toml 配置 SpaceVim 時,我們提供了兩個選項,位於 [options] 下:
bootstrap_beforebootstrap_after, 這兩個選項接受一個字符串最為值,該字符串值得是一個 vim 方法名。顧名思義,你可以通過這
兩個選項定義兩個 vim 方法,分別在載入配置時,和 vim 啟動后被調用,在方法內,你可以加入一些 vim 腳本,比如快捷鍵,
比如插件的選項。

比如,在配置文件內加入如下內容:

[options]
    enable_guicolors = false
    snippet_engine = "neosnippet"
    statusline_separator = 'arrow'
    sidebar_width = 30
    bootstrap_before = "myspacevim#before"

新建 ~/.SpaceVim.d/autoload/myspacevim.vim, 加入內容:

func! myspacevim#before() abort
    let g:neomake_enabled_c_makers = ['clang']
    nnoremap jk <esc>
endf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM