Neovim 系列(八):語法高亮與檔案導航——Treesitter + Telescope
這是 Neovim 從零開始系列的第八篇。整個系列共 12 篇文章,將帶你從完全不懂 Vim,到能用 Neovim 打造一個完整的現代化開發環境。 本篇的目標 經過前幾篇的設定,你的 Neovim 已經有了 LSP 和自動補全。這篇要加入三個讓開發體驗再度提升的功能: Treesitter:基於語法樹的精確高亮和文字操作 Telescope:強大的模糊搜尋(取代 VSCode 的 Cmd+P) oil.nvim:檔案瀏覽器(取代 VSCode 的左側邊欄) Treesitter:真正理解你的程式碼 傳統語法高亮 vs Treesitter 傳統的語法高亮用正則表達式來匹配——它只看文字模式,不懂程式碼結構。這導致: 複雜的語法可能標錯顏色 巢狀結構容易混亂 不同語言嵌套(如 HTML 中的 JavaScript)效果差 Treesitter 完全不同——它解析程式碼為語法樹(AST),真正理解程式碼的結構。好處是: 語法高亮更精確(區分函數名、變數、型別、常數等) 支援增量解析(編輯時只重新解析修改的部分,非常快) 提供基於語法結構的文字操作(如選取整個函數) 安裝 Treesitter -- lua/plugins/treesitter.lua return { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate", event = "BufReadPost", dependencies = { "nvim-treesitter/nvim-treesitter-textobjects", }, config = function() require("nvim-treesitter.configs").setup({ ensure_installed = { "lua", "vim", "vimdoc", "javascript", "typescript", "tsx", "go", "gomod", "gosum", "python", "html", "css", "json", "yaml", "toml", "markdown", "markdown_inline", "bash", "dockerfile", "gitcommit", "diff", }, auto_install = true, highlight = { enable = true, }, indent = { enable = true, }, incremental_selection = { enable = true, keymaps = { init_selection = "<C-space>", node_incremental = "<C-space>", scope_incremental = false, node_decremental = "<bs>", }, }, }) end, } 增量選取 上面設定中的 incremental_selection 是一個非常好用的功能: ...