Share This Post

快速開始

建議使用 iterm2 ,在透過下面來選擇自己喜歡的字體,下面是帶有icon的字體,有些可能是不支援所使用icon的自己斟酌,目前我使用的是 MesloLGS NF

Nerd Fonts – Iconic font aggregator, glyphs/icons collection, & fonts patcher

brew install --cask iterm2
git clone <https://github.com/yongde2900/aio-script.git>

sh nvim.sh

windows

cp .\\nvim $HOME\\AppData\\Local\\

參考 https://github.com/nvim-telescope/telescope-fzf-native.nvim/issues/43 解決fzf問題

接下來到你向要開啟專案的位置輸入nvim就可以打開neovim了

你也可以指定檔案

nvim index.js

今天的重點

  • 簡單的基本設置
  • packer使用方法
  • LSP設置
├── init.lua //在此處引入所有套件設定檔案
├── lua
│   └── yongde
│       ├── core
│       │   ├── colorscheme.lua.      //設定主題顏色
│       │   ├── keymaps.lua           //設定快捷鍵
│       │   └── options.lua           //編輯器相關設定
│       ├── plugins                   //套件設定檔放置處
│       │   ├── autopairs.lua         
│       │   ├── comment.lua           
│       │   ├── gitsigns.lua          
│       │   ├── indent-blandline.lua  
│       │   ├── lazygit.lua           //沒設置完全,目前沒任何作用
│       │   ├── lsp                   //lsp套件設置
│       │   │   ├── lspconfig.lua
│       │   │   ├── lspsaga.lua
│       │   │   ├── mason.lua
│       │   │   └── null-ls.lua
│       │   ├── lualine.lua
│       │   ├── nvim-cmp.lua
│       │   ├── nvim-tree.lua
│       │   ├── nvim-ufo.lua
│       │   ├── telescope.lua
│       │   ├── toggleterm.lua
│       │   └── treesitter.lua
│       └── plugins-setup.lua          //套件載入的地方
└── plugin
    └── packer_compiled.lua            //編譯後的檔案放置處(不用理他

在以下檔案中的 packer.startup的function裡面寫入想要使用的套件,儲存後就會幫你下載下來了,不想用想刪掉,也是直接在這邊把他移除就可以了。nvim的套件結尾通常會是.nvim

// plugins-setup.lua
return packer.startup(function(use)
	-- packer can manage itself
	use("wbthomason/packer.nvim")

	use("folke/tokyonight.nvim")
	if packer_bootstrap then
		require("packer").sync()
	end
end)

想要改變主題樣式,可以在github上尋找#nvim-colorschema,透過以上載入套件的方法後,在以下檔案colorscheme 後面改成你的colorscheme名稱就可以了。

https://github.com/topics/neovim-colorscheme

// colorscheme.lua
-- nightfly
-- tokyonight
-- tokyonight-(moon, day, storm, night)

local status, _ = pcall(vim.cmd, "colorscheme tokyonight-moon")
if not status then
	print("Colorscheme not found!") -- print error if colorscheme not installed
	return
end

LSP、formatter、linter套件下載

我們會透過mason這個套件來下載 lsp、formatter、linter,你可以透過:Mason手動下載,也可以在以下設置來讓他自動下載, mason_lspconfig 是下載 lsp,mason_null_ls下載 formatter 和 linter

// mason.lua
mason_lspconfig.setup({
	-- list of servers for mason to install
	ensure_installed = {
		"tsserver",
		"html",
		"cssls",
		"tailwindcss",
		"lua_ls",
		"emmet_ls",
		"intelephense",
		"pyre",
		"bashls",
		"eslint",
	},
	-- auto-install configured servers (with lspconfig)
	automatic_installation = true, -- not the same as ensure_installed
})

mason_null_ls.setup({
	-- list of formatters & linters for mason to install
	ensure_installed = {
		"prettier", -- ts/js formatter
		"stylua", -- lua formatter
		"eslint_d", -- ts/js linter
		"pint", --php formatter
		"phpstan", -- php linter
	},
	-- auto-install configured formatters & linters (with null-ls)
	automatic_installation = true,
}

LSP 設置

套件下載完後要進行設置,大部分都非常簡單,只要在lspconfig.lua裡,將已經寫好的 on_attach 和capabilities指定好就好,你也可以自己設置選項,可以參考以下網址來進行設置。

nvim-lspconfig/server_configurations.md at master · neovim/nvim-lspconfig

// lspconfig.lua

-- configure tailwindcss server
lspconfig["tailwindcss"].setup({
	capabilities = capabilities,
	on_attach = on_attach,
})

Formatter/Linter設置

Formatter和lLinter的設置也是很簡單的,在null-ls.lua裡設置formatting和diagnostics就可以了,你也可以自己設置一些選項,可以參考以下網址。

null-ls.nvim/BUILTINS.md at main · jose-elias-alvarez/null-ls.nvim

// null-ls.lua
null_ls.setup({
	-- setup formatters & linters
	sources = {
		formatting.prettier.with({
			disabled_filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact", "vue" },
		}), -- js/ts formatter
		formatting.eslint_d,
		formatting.pint, -- php formatter
		formatting.stylua, -- lua formatter
		diagnostics.phpstan, -- php linter
	},

語法高亮

我們透過tree-sitter來完成語法高亮的設置,在以下網址來確認所支援的語言然後寫在treesitter.lua 裡面的ensure_installed,就完成了。

ensure_installed = {
		"json",
		"javascript",
		"typescript",
		"tsx",
		"yaml",
		"html",
		"css",
		"markdown",
		"markdown_inline",
		"svelte",
		"graphql",
		"bash",
		"lua",
		"vim",
		"dockerfile",
		"gitignore",
		"php",
	},

其他套件設置

其他套件的設置,可以參考他們各自的gihub頁面,在plugins-setup裡面use的字串就是他們各自的github了。

NvChad

NvChad是一個neovim-configuration,幫你設置好大部分所需的套件,有好看的UI,重點是透過lazy.nvim,來動態載入套件,在有使用套件時在載入,大幅加快編輯器打開的時間。

https://github.com/NvChad/NvChad

如果你不喜歡NcChad你也可以使用其他人的config ,在github尋找 #neovim-configuration,就可以找到各路大神的設置 。

https://github.com/topics/neovim-configuration

訂閱研究文章

Get updates and learn from the best

More To Explore

Scroll to Top

hurry up !

軟體工程師培訓

限時免費報名中

藉由與「真實世界軟體專案」相同的技術、工具與開發流程,化簡成與商業機密無關、門檻較低更容易上手的「模擬專案」,讓你有機會在職場前輩的陪伴下,完成真槍實彈的練習,動手解決真實的問題,快速累積個人的經驗與作品,而不只是「學習技術」而已。