5

I'm using neovim and Telescope as a finder (find_files, live_grep, file_browser), by default Telescope is ignoring hidden files and files included in .gitignore - how can I add exception (e.g. .gitlab-ci file/folder) to this list? so Telescope will still ignore hidden files except .gitlab-ci file/folder?

Michu
  • 69
  • 1
  • 5

4 Answers4

6

Use default filter ignore pattern for that like that

telescope.setup {
  ....
  defaults = {
   ....
   file_ignore_patterns = {
     "node_modules", "build", "dist", "yarn.lock"
   },
  }
}

I know you can find this in someone's vimrc files or telescope docs. But may help some newbies.

Ashok
  • 2,846
  • 1
  • 12
  • 20
  • 3
    This adds to the file ignore patterns, but is there an inverse? I think that is what the question is asking. If my `.gitignore` includes `private.local.file`, the question is asking how to always include that specific file while continuing to ignore it for git commits. – radiomime Jan 05 '23 at 19:51
  • 1
    For myself, I have a whichkey binding that uses telescope.find_files() with the `search_dirs` and `search_file` flags for files that are commonly ignored but I still want to see. – radiomime Jan 05 '23 at 20:00
3

Telescope uses ripgrep to search through files. By default, ripgrep ignores several groups of files, including hidden files (dotfiles) and files ignored by git. Adding --no-ignore-vcs and --hidden flags will make it to search through those files.

Arguments for ripgrep can be configured via defaults.vimgrep_arguments. In your case, to search through hidden files, which are not in .gitignore --hidden flag should be added:

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

You can always test the command from the terminal before changing the Telescope configuration:

rg ... --hidden <search string>

As --hidden will enable search through all hidden files, you might want to look into .ignore or .rgignore files. They tell ripgrep which files to ignore during search. See ripgrep's documentation for more info.

German Lashevich
  • 2,193
  • 27
  • 38
2

After several weeks of intense reading and testing, I have found the for me easiest solution here.

In order to find HIDDEN FILES, I have added the following lines to .config/nvim/init.vim

nnoremap fff <cmd>Telescope find_files <cr>
nnoremap ffh <cmd>Telescope find_files hidden=true<cr>

When yo launch Neovim UI, pressing fff pops up the Telescope find_file screen, and when you press ffh, you can search for HIDDEN files.

To search for hidden files in LIVE_GREP, the work was a bit more difficult.

From my perspective, the most elegant solution is to first create the following file : .config/nvim/lua/find_hidden.lua

Inside goes :

local telescope = require("telescope")
local telescopeConfig = require("telescope.config")

-- Clone the default Telescope configuration
local vimgrep_arguments = { unpack(telescopeConfig.values.vimgrep_arguments) }

-- I want to search in hidden/dot files.
table.insert(vimgrep_arguments, "--hidden")
-- I don't want to search in the `.git` directory.
table.insert(vimgrep_arguments, "--glob")
table.insert(vimgrep_arguments, "!**/.git/*")

telescope.setup({
    defaults = {
        -- `hidden = true` is not supported in text grep commands.
        hidden = true,
        vimgrep_arguments = vimgrep_arguments,
    },
    pickers = {
        find_files= {
            hidden = true,
            -- `hidden = true` will still show the inside of `.git/` as it's not `.gitignore`d.
            find_command = { "rg", "--files", "--hidden", "--glob", "!**/.git/*" },
        },
     },
})
----- This activates the search for hidden files in live_grep
require("telescope").setup { 
       pickers = {
            live_grep = {
               additional_args = function(_ts)
                   return {"--hidden"}
               end
            },
       },
    }

The add the following the following lines to your init.vim :

:lua require('find_hidden')
nnoremap gre <cmd>Telescope live_grep <cr>

To sum it up: with the find_hidden.lua file and the following lines in your init.vim, you can easily search for hidden and non-hidden files both in telescope_find_files and telescope_live_grep :

:lua require('find_hidden')

 nnoremap fff <cmd>Telescope find_files <cr>
 nnoremap ffh <cmd>Telescope find_files hidden=true<cr>
 nnoremap gre <cmd>Telescope live_grep <cr>
rainer
  • 3,295
  • 5
  • 34
  • 50
0

I was also searching for a configuration that would allow me to search for/in hidden files while using telescope plugin. The core of solution is to use ripgrep with -g / --glob parameter.

Info: When using ripgrep in telesope, ripgreps will respects your .gitignore while searching - reference

The simple config that allowed me this, looks like following:

require('telescope').setup({
  defaults = {
    -- configure to use ripgrep
    vimgrep_arguments = {
      "rg",
      "--follow",        -- Follow symbolic links
      "--hidden",        -- Search for hidden files
      "--no-heading",    -- Don't group matches by each file
      "--with-filename", -- Print the file path with the matched lines
      "--line-number",   -- Show line numbers
      "--column",        -- Show column numbers
      "--smart-case",    -- Smart case search

      -- Exclude some patterns from search
      "--glob=!**/.git/*",
      "--glob=!**/.idea/*",
      "--glob=!**/.vscode/*",
      "--glob=!**/build/*",
      "--glob=!**/dist/*",
      "--glob=!**/yarn.lock",
      "--glob=!**/package-lock.json",
    },

    pickers = {
      find_files = {
        hidden = true,
        -- needed to exclude some files & dirs from general search
        -- when not included or specified in .gitignore
        find_command = {
          "rg",
          "--files",
          "--hidden",
          "--glob=!**/.git/*",
          "--glob=!**/.idea/*",
          "--glob=!**/.vscode/*",
          "--glob=!**/build/*",
          "--glob=!**/dist/*",
          "--glob=!**/yarn.lock",
          "--glob=!**/package-lock.json",
        },
      },
    },

    ...
 
  },
})
Egel
  • 1,796
  • 2
  • 24
  • 35