Skip to content

gw31415/fzyselect.vim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fzyselect.vim

fzyselect.vim is a simple fuzzy selector plugin of Vim.

2022-09-04.23.04.54.mov

Introduction

The vim.ui.select implemented in Neovim is simple and extensible. There are also several well-designed extension plugins based on it (e.g. dressing.nvim).

This plugin is one such extensions. As a feature, it uses a built-in matchfuzzypos function to speed up the search process. Also, for the minimalists (or following the UNIX philosophy), it has few extra functions, keymaps and commands as possible.

You can use this plugin in Vim as well as Neovim. Except exporting code for Lua, the all of this plugin is written in only Vim scripts (not Vim9 scripts).

Installation

Plug.vim

Plug 'gw31415/fzyselect.vim'

dein.vim

call dein#add('gw31415/fzyselect.vim')

packer.nvim

use 'gw31415/fzyselect.vim'

Key mappings

No default keymaps are provided.

Vim Scripts

fu! s:fzy_keymap()
	nmap <buffer> i <Plug>(fzyselect-fzy)
	nmap <buffer> <cr> <Plug>(fzyselect-retu)
	nmap <buffer> <esc> <cmd>clo<cr>
endfu
au FileType fzyselect cal <SID>fzy_keymap()

Lua

vim.api.nvim_create_autocmd('FileType', {
	pattern = 'fzyselect',
	callback = function ()
		vim.keymap.set('n', 'i','<Plug>(fzyselect-fzy)', { buffer = true })
		vim.keymap.set('n', '<cr>','<Plug>(fzyselect-retu)', { buffer = true })
		vim.keymap.set('n', '<esc>','<cmd>clo<cr>', { buffer = true })
	end
})

For Neovim users: vim.ui.select

If you want to replace vim.ui.select with this plugin's, you can directly assign the function require 'fzyselect'.start.

vim.ui.select = require 'fzyselect'.start

How to use

Only one function fzyselect#start is provided (two if you includes require 'fzyselect'.start exported for Lua).

You can use similar to vim.ui.select. Please see this document.

Vim Scripts

cal fzyselect#start(['apple', 'banana', 'chocolate'],
	\ {}, {i->append('.', i)})

Lua

require 'fzyselect'.start({'apple', 'banana', 'chocolate'}, {},
	function(i)
		vim.fn.append('.', i)
	end)

On the split window that appears then you can use the usual keymaps you set up. To operate fuzzy selecting, you need to set up two keymaps:

Key Usage
<Plug>(fzyselect-fzy) Launch fuzzy search.
<Plug>(fzyselect-retu) Select the item.

Configuration Example

Lines

Fuzzy search for lines of the current buffer.

nn gl <cmd>cal fzyselect#start(getline(1, '$'), #{prompt:'Fuzzy search'}, {_,i->i==v:null?v:null:cursor(i, 0)})<cr>

image

Files

Fuzzy search for files of the working directory.

fu! s:glob(path)
	let li = []
	for f in readdir(a:path)
		let p = a:path .. '/' .. f
		if isdirectory(p)
			cal extend(li, s:glob(p))
		else
			cal add(li, p)
		en
	endfo
	return li
endfu
fu! s:edit(path) abort
	if a:path != v:null
		exe 'e ' .. a:path
	en
endfu
nn <c-p> <cmd>cal fzyselect#start(<SID>glob('.'), {}, {p-><SID>edit(p)})<cr>

image

Buffers

Fuzzy switcher of buffers.

fu! s:buffer(i) abort
	if a:i != v:null
		exe 'b ' .. a:i
	en
endfu
nn B <cmd>cal fzyselect#start(
			\ filter(range(1, bufnr('$')), 'buflisted(v:val)'),
			\ #{prompt:'Select buffer',format_item:{i->split(execute('ls!'), "\n")[i-1]}},
			\ {li-><SID>buffer(li)})<cr>

image

About

The simplest fuzzy selector plugin of Vim

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published