telescope.nvim is built on configurable search tools such as fd and ripgrep. Both search tools support glob patterns. This plugin offers some helpers for scoping searches to a custom glob pattern in the current instance of nvim.
This plugin is inspired by a similar vscode feature:
- Type in a custom glob with
:GlobUpdate. If you intend to glob an entire directory, use:GlobDiras it will directly place your cursor between the asterisks (e.g.**/|/**/*). - Get a list of glob suggestions with
:Telescope glob. The suggestions are based on the shape of the current directory. After choosing a glob pattern, future telescope pickers will be scoped to that glob pattern. To clear the pattern, run:Telescope globonce more and select "Clear existing glob".
This plugin doesn't do a whole lot. This doc probably contain more lines than the actual code, so feel free to rip out the functionality right into your config instead of installing this plugin. But anyways:
- You give it a glob pattern through commands such as
:GlobUpdate,:Telescope glob, orrequire('telescope-glob').set_glob({ value = '**/*.lua' }). - The glob is stored as a string in a global variable.
- You can ask for the current glob value with
require('telescope-glob').get_glob().
{
'thenbe/telescope-glob.nvim',
dependencies = { 'nvim-telescope/telescope.nvim' },
config = function() require('telescope').load_extension('glob') end,
keys = {
{ '<leader>fg', '<cmd> Telescope glob <CR>', desc = 'Pick a glob' },
{ '<leader>mgg', '<cmd>GlobUpdate<cr>', desc = 'Enter a glob' },
{ '<leader>mgd', '<cmd>GlobDir<cr>', desc = 'Enter a directory glob' },
{ '<leader>mgc', function() require('telescope-glob').set_glob({ value = '' }) end, desc = 'Clear the glob' },
{ '<leader>mgl', function() require('telescope-glob').set_glob({ value = '**/*.lua' }) end, desc = 'Set the glob (lua)' },
},
}In order for the selected glob pattern to take effect, we need to apply it to our pickers.
The process may be slightly different for every picker. For instance, the find_files picker uses fd under the hood. On the command line, applying a custom glob to fd looks like this: fd --full-path --glob '**/mydir/**/*.lua'. Therefore, we must tell telescope to pass similar flags to fd.
Below are some examples on how to apply our glob pattern to some of the popular telescope pickers.
find_files = {
find_command = function()
local args = {
'fd',
'--no-ignore',
'--hidden',
'--type',
'f',
'--strip-cwd-prefix',
-- glob config:
'--full-path',
'--glob',
require('telescope-glob').get_glob() or '',
}
return args
end,
},To filter the results, see the fd docs.
live_grep = {
additional_args = function()
local args = {
'--no-ignore',
'--hidden',
'--glob',
require('telescope-glob').get_glob() or '**/*',
}
return args
end,
},To filter the results, see the ripgrep docs.
Optional. When configured, it'll show the current glob pattern in the statusline. As per lualine.nvim docs:
sections = {
lualine_x = {
-- telescope-glob.nvim
{
function()
local glob_pattern = require('telescope-glob').get_glob()
return glob_pattern and (' ' .. glob_pattern) or ''
end,
cond = function()
return package.loaded['telescope'].extensions.glob and require('telescope-glob').get_glob() ~= ''
end,
},
},
}- Glob tester: https://globster.xyz
- Glob primer: https://github.com/isaacs/node-glob/blob/main/README.md#glob-primer