How to include specific hidden file/folder in search result when using telescope.nvim?
Asked Answered
E

6

10

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?

Eucaine answered 9/4, 2022 at 14:48 Comment(0)
A
7

Use default filter ignore pattern like this:

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.

Alkmaar answered 8/6, 2022 at 12:10 Comment(2)
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.Acantho
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.Acantho
G
9

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.

Gametangium answered 11/2, 2023 at 23:51 Comment(1)
This is not the answer to the question, but it is the answer that I was looking for. Thanks!Camden
A
7

Use default filter ignore pattern like this:

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.

Alkmaar answered 8/6, 2022 at 12:10 Comment(2)
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.Acantho
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.Acantho
L
5

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",
      },
    },
  },
})
Lisk answered 28/8, 2023 at 9:9 Comment(2)
pickers should be outside defaults, like this: require('telescope').setup { defaults = { ... }, pickers = { find_files = { find_command = { "rg", "--files", "--hidden", }, }, }, }Keslie
Yes @invis, you're right. Thanks, I've already updated the example!Lisk
R
4

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 you 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 .config/nvim/lua/find_hidden.lua:

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>
Romero answered 19/2, 2023 at 14:3 Comment(0)
F
4

After much pain and suffering, I've read the telescope.txt help and maanged to show hidden files in both find_files and live_grep:

find_files

When running find_files, use the no_ignore option, available in Vimscript (help).

:Telescope find_files no_ignore=true

live_grep

But for live_grep we need to pass the additional_args option which is (correct me if I'm wrong) only available in Lua (help).

require('telescope.builtin').live_grep({ additional_args = { '-u' } })

The -u flag is an rg flag that shows ignored files.

Some notes

I guess you can affect these in the settings, if someone comments that for both I'll add them to my answer.

I, personally, need to have a keymap for each, so here's my lazy.nvim keys config:

  keys = {
    -- this one is double space by default in LazyVim, uncomment if necessary
    -- { "<leader>fi", "<cmd>Telescope find_files<cr>", desc = "Telescope Files" }, 
    { "<leader>fh", "<cmd>Telescope find_files no_ignore=true<cr>", desc = "Telescope Files Hidden" },
    { "<leader>hi", "<cmd>Telescope oldfiles<cr>", desc = "Telescope Old" },
    { "<leader>ag", "<cmd>Telescope live_grep<cr>", desc = "Telescope Grep" },
    {
      "<leader>ah",
      function()
        require("telescope.builtin").live_grep({ additional_args = { "-u" } })
      end,
      desc = "Telescope Grep Hidden",
    },
  },

I wished interfaces that dealt with file filters would let you specify these:

  • god globs: a glob that when matched is always included.
  • ignore ignore: ignore a specific ignore file, such as a .gitignore.
Frenulum answered 11/9, 2023 at 23:12 Comment(0)
D
1

My solution was to use a .ignore file. According to the docs, you can create rules that ignore the ripgrep defaults. I have a git repo full of hidden files that I would like to include in the search results, so the .ignore looks like this:

!*
.git/

First, I include all the files in the repo. Then I remove the .git directory again. Now all my dot files are shown in Telescope.

Davide answered 3/3 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.