telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

live_grep without ripgrep

Open mknurs opened this issue 3 years ago • 6 comments

I would really like to use telescope and it's live_grep picker without the extra dependency. I'm trying to replace ripgrep with the regular (gnu) grep and this is for now – as far as I know – the only way:

vimgrep_arguments = {
	"grep",
	"--color=never",
	"--with-filename",
	"--line-number",
	"-b", -- grep doesn't support a `--column` option :(
	"--ignore-case",
	"--recursive",
	"--no-messages",
},

Because grep doesn't have a --column option I have to include the -b option, which outputs the byte offset so the grep output can match with the internal regex.

https://github.com/nvim-telescope/telescope.nvim/blob/b3d3d938696a533c199031418e0da553a8424a46/lua/telescope/make_entry.lua#L165

It would be great to either have an option to configure this internal regex, or to maybe code it as to not impose practically a hard dependency.

mknurs avatar Jul 17 '22 15:07 mknurs

I use

    vimgrep_arguments = {
                "grep",
                "--extended-regexp",
                "--color=never",
                "--with-filename",
                "--line-number",
                "-b", -- grep doesn't support a `--column` option :(
                "--ignore-case",
                "--recursive",
                "--no-messages",
                "--exclude-dir=*cache*",
                "--exclude-dir=*.git",
                "--exclude=.*",
                "--binary-files=without-match"
                -- git grep also works but limits to only git directories,the above works perfectly
                -- "git", "grep", "--full-name", "--line-number", "--column", "--extended-regexp", "--ignore-case",
                -- "--no-color", "--recursive", "--recurse-submodules", "-I"
            },

The git grep version respects .gitignore files. @mknurs I believe it would be better if we could find a way to make the default vimgrep_arguments command git grep and only fallback to gnu grep when not in a git dir

bernardassan avatar Jul 21 '22 00:07 bernardassan

I would really like to use Ag which does have the correct options AFAIK. However whenever I try to set it up the results of live grep are garbage. It would be really nice to not have to rely on ripgrep and it's rust dependency when building my stack from scratch. I'm kind of new to neovim/lua though so maybe I'm setting things up wrong. 🤷‍♂️

psakievich avatar Aug 09 '22 13:08 psakievich

@Ultra-Code do you add that vimgrep_argument , inside the telescope.setup ?

like so ?

			require('telescope').setup {
				defaults = require('telescope.themes').get_ivy {
					mappings = {
						i = { ["<C-h>"] = "which_key" }
					}
				},
				-- Use grep wihtout rg dependency
				vimgrep_arguments = {
					"grep",
					"--extended-regexp",
					"--color=never",
					"--with-filename",
					"--line-number",
					"-b", -- grep doesn't support a `--column` option :(
					"--ignore-case",
					"--recursive",
					"--no-messages",
					"--exclude-dir=*cache*",
					"--exclude-dir=*.git",
					"--exclude=.*",
					"--binary-files=without-match"
				},
			}

motorto avatar Aug 15 '22 17:08 motorto

@motorto vimgrep_arguments is in the default table .ie something like this

bernardassan avatar Aug 15 '22 20:08 bernardassan

Thanks, works now !

motorto avatar Aug 15 '22 21:08 motorto

For anyone interested in here is the setup I used to get it working with ag aka the silver searcher:

require('telescope').setup{
  defaults = {
     vimgrep_arguments = {
        "ag",
        "--nocolor",
        "--noheading",
        "--numbers",
        "--column",
        "--smart-case",
        "--silent",
        "--vimgrep",
    }
  }
}

psakievich avatar Aug 16 '22 15:08 psakievich

On macOs high sierra (10.13.6), trying to configure with native grep (grep (BSD grep) 2.5.1-FreeBSD) with above psakievich setup, but without success. Currently in lua/telescope/builtin/__files.lua:231 function files.find_files there are checks for commands, maybe it's worth to implement in files.live_grep also check for commands and set finder accordingly? Could be:

    if 1 == vim.fn.executable "rg" then
        if grep_open_files then
            search_list = filelist
        elseif search_dirs then
            search_list = search_dirs
        end
        return flatten { args, "--", prompt, search_list }
    elseif 1 == vim.fn.executable "grep" then
        return flatten { "grep", "-rbisn", prompt, opts.cwd }
    elseif 1 == vim.fn.executable "findstr" then
        return flatten { "findstr", "/s /n /o /p", prompt, opts.cwd .. "*.*" }  --windows native
    end

andvisk avatar Nov 05 '22 13:11 andvisk