Skip to content

Instantly share code, notes, and snippets.

@humorless
Last active April 2, 2025 19:41
Show Gist options
  • Save humorless/fd9f316f8b788ff7224cdd2f4c8d6dfa to your computer and use it in GitHub Desktop.
Save humorless/fd9f316f8b788ff7224cdd2f4c8d6dfa to your computer and use it in GitHub Desktop.
VimScript 小整理

檢查狀態

  1. :verbose echo $variable 顯示變量的值和它的設定來源
  2. :checkhealth $Plugin 查看 Neovim 插件的狀態, 例如 :checkhealth nvim-treesitter
  3. :scriptnames 列出當前加載的所有腳本
  4. :map 列出當前所有的 mapping
  5. :nmap 列出 normal mode 的所有 mapping
  6. :autocmd 列出所有 auto command
  7. :set 列出所有的選項
  8. :echo g: 列出所有的全域變數
  9. :lua vim.print(something) 查看 lua 變數

全域變數

  • 變數如果用 g: 開始,這種是全域變數,通常用於微調插件。

選項

  • 選項不是變數,不能用 :let 來改,一定要用 :set 來修改。

符號

驚嘆號的語意

  1. 強制執行或是覆寫
  2. 常套用於 command, function => command!, function!
  3. 最常見的案例 :w!

冒號的語意

  • 在命令列時,命令要用 : 開始。
  • 命令寫在 init.vim 時,則不需要。

常用的 VimScript 命令 (command)

  • 基礎設定

    1. 安裝插件
    2. 輸出 (echo, echom, verbose echo)
    3. 內部的狀態 (scriptnames, checkhealth)
    4. 選項設定 (set)
    5. 維調插件的全域變數 -> (let)
  • 編輯器功能

    1. 快捷鍵映射 (nnoremap、tnoremap)
    2. 自定義命令 (command)
    3. 自動命令 (autocmd)
  • 模組

    1. 變數設定 (let)
    2. 呼叫函數 (call)
    3. 函數定義 (function)
    4. 動態執行命令 (execute) -> 類似許多程式語言裡的 eval
    5. 使用 lua script (lua, luaeval)
  • 進階

    • 檔案匹配 (glob)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment