nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.56k stars 827 forks source link

How to configure live_grep to search hidden files #855

Closed JohnObla closed 3 years ago

JohnObla commented 3 years ago

Description

Hi Telescope Team,

Excuse my ignorance but I can't work out how to config the live_grep builtin to:

  1. Search hidden files
  2. Exclude node modules
  3. Exclude git files

The default behaviour of live_grep prevents me from including my .gitignore, .gimodules, files etc.

The config was easy to setup for find_files after reading issue #696, but I couldn't find anything that does the same for live_grep.

Please let me know how I can configure the live_grep equivalent of:

local find_command = "{ 'rg', '--files', '--hidden', '-g', '!node_modules/**', '-g', '!.git/**', }"
map('n','<leader>ff','<cmd>lua require(\'telescope.builtin\').find_files({find_command = ' .. find_command .. ' })<cr>')

Thank you, and thank you for Telescope! John

Expected Behavior

When I configure live_grep, I am able to to search the contents of all files in my directory, excluding node_modules and git/ files.

Actual Behavior

I don't know how to configure the desired behaviour.

Details

Reproduce 1. `:Telescope live_grep` 2. Here is the issue, `live_grep` doesn't have the desired config.
Environment - nvim --version output: NVIM v0.5.0-dev+1318-g61aefaf29 - Operating system: macOS Big Sur - Version 11.2.3 (20D91) - Telescope commit: 69eb5ea
Configuration

``` -- plugins.lua (packer.nvim) ... use { 'nvim-telescope/telescope.nvim', requires = { {'nvim-lua/popup.nvim'}, {'nvim-lua/plenary.nvim'} }, config = function () require('config.telescope').setup() end } ... ``` ``` -- config.telescope.lua local module = {} function module.setup() local function map(mode, lhs, rhs, opts) local options = {noremap = true} if opts then options = vim.tbl_extend('force', options, opts) end vim.api.nvim_set_keymap(mode, lhs, rhs, options) end local find_command = "{ 'rg', '--files', '--hidden', '-g', '!node_modules/**', '-g', '!.git/**', }" -- Find files using Telescope command-line sugar. map('n','ff','lua require(\'telescope.builtin\').find_files({find_command = ' .. find_command .. ' })') map('n','fg','lua require(\'telescope.builtin\').live_grep({find_command = ' .. find_command .. ' })') map('n','fb','Telescope buffers') map('n','fh','Telescope help_tags') end return module ```

Conni2461 commented 3 years ago

Those this comment https://github.com/nvim-telescope/telescope.nvim/issues/470#issuecomment-767904334 help?

Also to ignore certain directories use file_ignore_patterns

Example : file_ignore_patterns = { 'node_modules', '.git' }

See also:

(We need way better documentation here)

JohnObla commented 3 years ago

@Conni2461 Thank you, this is exactly what I was looking for!

I'm not with my laptop now, but I'm happy to close this issue and only open it if I don't end up getting it working.

Thank you for taking the time to help me 🙂

djalan commented 2 years ago

Hello @JohnObla and @Conni2461

Here is how I pass the --hidden to rg. I found this by reading :help live_grep.

require("telescope").setup { 
    pickers = {
        live_grep = {
            additional_args = function(opts)
                return {"--hidden"}
            end
        },
    },
}
rainer2208 commented 1 year ago

hi guys, this is leaving me bonkers for the last couple days ...

require('telescope').setup{
  defaults = {
    vimgrep_arguments = {
      'rg',
      '--color=never',
      '--no-heading',
      '--with-filename',
      '--line-number',
      '--column',
      '--smart-case',
      '-u' -- thats the new thing
    },
}

i am using nvim and on my entire hard-drive there is not one files that contains the line require('telescope').setup ..

in which file exactly do i put the snippet, or flag, to have live grep search in hidden files??

brandoncc commented 1 year ago

hi guys, this is leaving me bonkers for the last couple days ...

require('telescope').setup{
  defaults = {
    vimgrep_arguments = {
      'rg',
      '--color=never',
      '--no-heading',
      '--with-filename',
      '--line-number',
      '--column',
      '--smart-case',
      '-u' -- thats the new thing
    },
}

i am using nvim and on my entire hard-drive there is not one files that contains the line require('telescope').setup ..

in which file exactly do i put the snippet, or flag, to have live grep search in hidden files??

You can put it in init.lua or any other lua file that gets executed when you start neovim. You could even put the lua code in a vimscript file like this. I hope this helps @rainer2208

rainer2208 commented 1 year ago

@brandoncc , many thanks for leaving this comment, very helpful, indeed..

I have found a similar solution already, but I will try it definitely

NinnjA254 commented 1 year ago

Here's is my understanding from tinkering around with my config today:

To configure the command and flags to be used by findfiles:

    find_files = {
        -- config specific to find_files goes here. :h telescope.builtin.find_files() for more info
        find_command = { --the command used by find_files
            'rg',
            '--hidden',
            '--your flags go here',
            '--as comma separated strings',
            '--think about how you want find files to behave, look at ripgrep documentation, add those flags here',

            -- some examples
            '--hidden',
            '--glob',  -- this flag allows you to hide exclude these files and folders from your search 👇
            '!{**/.git/*,**/node_modules/*,**/package-lock.json,**/yarn.lock}', 
        }
    }

the command and flags to be used by livegrep and grepstring can be configured through vimgrep_arguments, like this:

defaults = {
    vimgrep_arguments = { -- :h telescope.defaults.vimgrep_arguments for more info
            'rg',

            '--your flags go here',
            '--as comma separated strings',
            '--think about how you want find files to behave, look at ripgrep documentation, add those flags here,'

            -- some examples
            '--hidden',
            '--glob',  -- this flag allows you to hide exclude these files and folders from your search 👇
            '!{**/.git/*,**/node_modules/*,**/package-lock.json,**/yarn.lock}', 
                }
        },

documentation about vimgrep_arguments says:

vimgrep_arguments:
        Defines the command that will be used for `live_grep` and `grep_string`
        pickers.
        Hint: Make sure that color is currently set to `never` because we do
        not yet interpret color codes
        Hint 2: Make sure that these options are in your changes arguments:
          "--no-heading", "--with-filename", "--line-number", "--column"
        because we need them so the ripgrep output is in the correct format.

Finally, if there's a particular picker or part of telescope whose behavior you wish to modify, do :h telescope, then search for that picker in the resulting docs

I tried using file_ignore_patterns to ignore certain patterns, but it seems to be very slow, so I prefer to use --globs instead👍

In my case, I chose to show all hidden files(except those in gitignore), but used --globs to not search some files and folders Here's all of my telescope config:

image

gsf commented 7 months ago

To further boil down all of the great advice above, here is my working configuration in init.lua to include hidden files in telescope (relying on fd for find_files and rg for grep_string and live_grep):

require("telescope").setup{
  pickers = {
    find_files = {
      hidden = true
    },
    grep_string = {
      additional_args = {"--hidden"}
    },
    live_grep = {
      additional_args = {"--hidden"}
    },
  },
}
FanchenBao commented 7 months ago

For whatever reason, the hidden = true config did not work for me. I have to change the find_command itself.

require('telescope').setup{
  pickers = {
    find_files = {
      find_command = { 'rg', '--files', '--iglob', '!.git', '--hidden' },
    },
    grep_string = {
      additional_args = {'--hidden'}
    },
    live_grep = {
      additional_args = {'--hidden'}
    }
  }
}
Drllap commented 7 months ago

Does anyone know if it is possible to do this via the :Telescope command, i.e. :Telescope live_grep additional_args=--hidden

GitMurf commented 5 months ago

To further boil down all of the great advice above, here is my working configuration in init.lua to include hidden files in telescope (relying on fd for find_files and rg for grep_string and live_grep):

require("telescope").setup{
  pickers = {
    find_files = {
      hidden = true
    },
    grep_string = {
      additional_args = {"--hidden"}
    },
    live_grep = {
      additional_args = {"--hidden"}
    },
  },
}

Thank you @gsf ! this is exactly what I have been looking for :)

patlux commented 2 days ago

As an extra keymap:


local telescope = require("telescope.builtin")
map("n", "<leader>si", function()
  telescope.live_grep({
    additional_args = function()
      return { "--no-ignore" }
    end,
  })
end, { desc = "Live grep for all files" })
map("n", "<leader>fi", function()
  telescope.find_files({
    no_ignore = true, -- Include ignored files
    hidden = true, -- Optionally include hidden files
  })
end, { desc = "Find all files" })