在使用TeleScope.nvim时,如何在搜索结果中包含特定的隐藏文件/文件夹?

发布于 2025-01-20 04:58:52 字数 178 浏览 4 评论 0原文

我正在使用neovim和望远镜作为查找器(find_files,live_grep,file_browser),默认情况下,望远镜忽略了.gitignore中包含的隐藏的文件和文件 - 我如何添加异常(例如.gitlab -ci file/fileser)到此列表中?因此,除了.gitlab-ci文件/文件夹外,望远镜仍然会忽略隐藏的文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深海蓝天 2025-01-27 04:58:52

望远镜使用 ripgrep以搜索文件。默认情况下,ripgrep忽略了几组文件,包括隐藏的文件(dotfiles)和git忽略的文件。添加- NO-EIGNORE-VCS- 隐藏标志将使它浏览这些文件。

可以通过defaults.vimgrep_arguments来配置RIPGREP的参数。
在您的情况下,要搜索不在中的隐藏文件

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

。望远镜配置:

rg ... --hidden <search string>

AS - 隐藏将通过隐藏文件启用搜索,您可能需要查看.ignore.rgignore文件。他们告诉ripgrep在搜索过程中要忽略哪些文件。参见

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.

人疚 2025-01-27 04:58:52

使用像这样的默认过滤器忽略模式:

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

我知道你可以在某人的 vimrc 文件或 望远镜文档。但可能对一些新手有帮助。

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.

初吻给了烟 2025-01-27 04:58:52

我还在搜索一种配置,该配置使我在使用望远镜插件时可以在隐藏文件中搜索/中。解决方案的核心是使用 ripgrep带有-G/ - - -glob参数。

信息:在望远镜中使用ripgrep时,ripgreps将尊重您的.gitignore搜索时 - 参考

允许我的简单配置,看起来如下:

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",
      },
    },
  },
})

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",
      },
    },
  },
})
眼藏柔 2025-01-27 04:58:52

经过数周的激烈阅读和测试,我发现了最简单的解决方案在这里

为了找到隐藏的文件,我将以下行添加到.config/nvim/init.vim

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

启动neovim ui时,按fff弹出望远镜<代码> find_file 屏幕,然后按ffh时,您可以搜索隐藏的文件。

要在live_grep中搜索隐藏的文件,工作要困难得多。

从我的角度来看,最优雅的解决方案是首先创建以下.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
          },
     },
  }

将以下几行添加到您的init.vim :

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

总结一下:使用find_hidded.lua文件和init.vim中的以下行telescope_find_filestelescope_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>

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>
飘逸的'云 2025-01-27 04:58:52

经过一番痛苦和磨难后,我阅读了 telescope.txt 帮助,并打算在 find_fileslive_grep 中显示隐藏文件:

find_files

When运行 find_files,使用 Vimscript 中提供的 no_ignore 选项(帮助)。

:Telescope find_files no_ignore=true

live_grep

但是对于 live_grep 我们需要传递 additional_args 选项,该选项(如果我错了请纠正我)仅在 Lua 中可用(帮助)。

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

-u 标志是一个 rg 标志,用于显示被忽略的文件。

注释

我想你可以在设置中影响这些

,如果有人评论说我会将它们添加到我的答案中。我个人需要为每个键映射,所以这是我的 lazy.nvim 键配置:

  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",
    },
  },

我希望处理文件过滤器的接口允许您指定这些:

  • god globs :匹配时始终包含的 glob。
  • ignore忽略:忽略特定的忽略文件,例如.gitignore。

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.
春庭雪 2025-01-27 04:58:52

我的解决方案是使用 .ignore 文件。根据文档,您可以创建规则忽略 ripgrep 默认值。我有一个充满隐藏文件的 git 存储库,我希望将其包含在搜索结果中,因此 .ignore 如下所示:

!*
.git/

首先,我包含存储库中的所有文件。然后我再次删除 .git 目录。现在我的所有点文件都显示在 Telescope 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文