模糊搜索插件(fuzzy finder)算是日常使用 Neovim 必不可少的插件之一。早期我在使用 Vim 时,最开始使用过 ctrlp.vim、unite.vim、leaderf、以及后来切换到的 denite.vim。 因为一直是在 Windows 系统下居多,因此没使用 fzf 系列的插件,最开始的时候 Windows 默认终端支持这些命令可不是那么的友好。因此我更倾向于使用 Vim 内置窗口实现的这类插件。
随着 Neovim 的浮窗功能完善,使用浮窗实现的 telescope.nvim 是我日常主要使用的工具了,我自己也实现了好一些 telescope extensions。 但是 telescope 似乎太复杂了,而且个人感觉维护也变得不是那么的活跃了,因此就自己写了一个 picker.nvim
安装和配置
picker.nvim 的安装很简单,默认是没有任何仓库依赖,可以使用任意插件管理器进行安装,比如 nvim-plug:
require("plug").add({
	{
		"wsdjeg/picker.nvim",
		config = function()
			require("picker").setup({
				filter = {
					ignorecase = false, -- ignorecase (boolean): defaults to false
				},
				window = {
					width = 0.8, -- set picker screen width, default is 0.8 * vim.o.columns
					height = 0.8,
					col = 0.1,
					row = 0.1,
					current_icon = ">",
					current_icon_hl = "CursorLine",
					enable_preview = false,
					preview_timeout = 500,
				},
				highlight = {
					matched = "Tag",
				},
				prompt = {
					position = "bottom", -- set prompt position, bottom or top
					icon = ">",
					icon_hl = "Error",
					insert_timeout = 100,
					title = true, -- display/hide source name
				},
				mappings = {
					close = "<Esc>",
					next_item = "<Tab>",
					previous_item = "<S-Tab>",
					open_item = "<Enter>",
					toggle_preview = "<C-p>",
				},
			})
		end,
	},
})
以上是插件初始化时,默认的配置,也可以参考我的配置文件:nvim-config/plugins/picker.lua, 在我的配置里,我使用 picker.nvim 接管了 Neovim 默认的 vim.ui.select 函数。
基本使用
- :Picker命令看看目前支持的源(sources)
- :Picker source_name指定打开某个源进行匹配搜索。
- --input指定默认初始化输入的内容
- --input=<cword>指定以光标下的词作为默认输入内容。
默认只有四个快捷键,可以在 setup 时指定,默认是以下四个按键:
| key binding | description | 
|---|---|
| Tab | next item | 
| S-Tab | previous item | 
| Enter | default action | 
| Esc | close picker | 
其他的快捷键通过 source 的 action() 函数返回定义。
内置 source
| source | description | 
|---|---|
| buffers | listed buffers | 
| buftags | ctags outline for current buffer | 
| cmd_history | results from :history : | 
| colorscheme | all colorschemes | 
| files | files in current dir | 
| help_tags | neovim help tags source | 
| highlights | highlight group source | 
| jumps | jump list | 
| lines | lines in current buffer | 
| loclist | location list source | 
| lsp_document_symbols | document symbols result from lsp client | 
| lsp_references | lsp references | 
| lsp_workspace_symbols | workspace symbols | 
| marks | marks list | 
| picker_config | picker config source | 
| qflist | quickfix source | 
| registers | registers context | 
其他插件 source
| source | description | 
|---|---|
| mru | most recent used files, need mru.nvim | 
| project | project history, need rooter.nvim | 
| bookmarks | all bookmarks, need bookmarks.nvim | 
| zettelkasten | zettelkasten notes source from zettelkasten.nvim | 
| zettelkasten_tags | zettelkasten tags source from zettelkasten.nvim | 
| git-branch | git branch source from git.nvim | 
| music-player | music-player source form music-player.nvim | 
| plug | plugins source for nvim-plug | 
| async_files | async files source, require job.nvim | 
如何自定义拓展
picker.nvim 的拓展实际上就是一个 Lua table:
local source = {}
---@return PickerItem[]
function source.get() end
---@param entry PickerItem
function source.default_action(entry) end
--- 只有需要使用到预览窗口,才需要定义 preview 函数。
source.preview_win = true
function source.preview(entry, win, buf) end