Skip to content
0

Spell Check in Neovim

I have a couple of problems with the spell check: a, it appears to skip apparent spell errors inside a list (ordered or unordered), and b, the inline code style appears to overwrite the error style, therefore you don't see that red wavy line beneath the miss-spelled words.

跟折叠功能不同,拼写检查默认没有开启。运行命令可以在当前 buffer 开启拼写检查:

:setlocal spell spelllang=en_us
  • :set spell turn on spelling, if not already on
  • :set nospell turn off spelling

此时,在 Markdown 文件内测试,故意把 The 拼写成 Teh 并不出现红色波浪下划线报错。可能还需要设置其他的什么选项?

当光标位于 Teh 上时,按下 z= 出弹出拼写建议菜单。

  • Thsis are some intended typos
  • FedEx
  • Yourr

全局开启拼写检查

我们在 options.lua 内添加以下代码:

lua
-- Enable spell checking for English and Chinese
opt.spell = true
opt.spelllang = "en_us,cjk"

如此设置之后,适用于所有文件都默认开启拼写检查。其实,适用于开启拼写检查的文件类型仅限于 Markdown 就够了。其他的类型的代码文件或者配置文件,不怎么需要做拼写检查。

仅限 Markdown 拼写检查

这种仅限 Markdown 文件才开启拼写检查的配置方法,在 keymaps.lua 添加以下配置:

lua
-- FileType autocmd for markdown files
-- https://github.com/Piotr1215/dotfiles/blob/master/.config/nvim/ftplugin/markdown.lua
vim.api.nvim_create_autocmd("FileType", {
	pattern = { "markdown", "mdx", "mdown", "mkd", "mkdn", "mdwn" },
	callback = function()
		-- Local settings
		vim.opt_local.conceallevel = 0
		vim.opt_local.spell = true
		vim.opt_local.spelllang = "en_us,cjk"
		vim.opt_local.expandtab = true
		vim.opt_local.shiftwidth = 4
		vim.opt_local.softtabstop = 4
		vim.opt_local.autoindent = true

		-- Arrow abbreviations
		local arrows = { [">>"] = "→", ["<<"] = "←", ["^^"] = "↑", ["VV"] = "↓" }
		for key, val in pairs(arrows) do
			vim.cmd(string.format("iabbrev %s %s", key, val))
		end

		-- Handle code blocks
		local function MarkdownCodeBlock(outside)
			vim.cmd("call search('```', 'cb')")
			vim.cmd(outside and "normal! Vo" or "normal! j0Vo")
			vim.cmd("call search('```')")
			if not outside then
				vim.cmd("normal! k")
			end
		end

		-- Set keymaps
		local function set_keymaps()
			local maps = {
				{ "n", "]]", "<Plug>Markdown_MoveToNextHeader" },
				{ "n", "[[", "<Plug>Markdown_MoveToPreviousHeader" },
				{ "n", "]c", "]c", { noremap = true } },
				{ "n", "<leader>mp", ":MarkdownPreview<CR>:silent !bash -c 'wmctrl -a Firefox'<CR>" },
				{ "n", "<leader>pi", ":call mdip#MarkdownClipboardImage()<CR>" },
				{ "n", "ctd", "4wvg$y" },
				{ "v", "<leader>hi", ":HeaderIncrease<CR>" },
				{ "v", "<Leader>b", ":lua BoldMe()<CR>" },
				{ "x", "<Leader>b", ":lua BoldMe()<CR>" },
				{ "v", ",wl", [[c[<c-r>"]()<esc>]], { noremap = false } },
			}

		for _, map in ipairs(maps) do
				vim.keymap.set(
					map[1],
					map[2],
					map[3],
					vim.tbl_extend("force", { buffer = 0, silent = true }, map[4] or {})
				)
			end

			-- Code block text objects
			for _, mode in ipairs({ "o", "x" }) do
				for _, mapping in ipairs({
					{ "am", true },
					{ "im", false },
				}) do
					vim.keymap.set(mode, mapping[1], function()
						MarkdownCodeBlock(mapping[2])
					end, { buffer = 0 })
				end
			end
		end

		pcall(function()
			vim.keymap.del("n", "]c", { buffer = 0 })
		end)
		set_keymaps()
	end,
})

拼写检查开关切换

lua
-- Spell check
keymap.set("n", "<leader>ss", "<cmd>set spell!<CR>", { desc = "Toggle spell On/Off" })

常见按键命令

KeyDescription
]sMove to the next misspelt word
[sMove to the previous misspelt word
z=Provide suggestions
zgAdd a word to dictionary
zugRemove a word to dictionary
zwMark word as misspelled
最近更新