LazyVim/Tutorial/Search for a Phrase Across Files
Properties
- tags
- Diataxis/Tutorial
Tutorial: Search for a Phrase Across Files in LazyVim
What You'll Do
Before You Start
- Use this garden's current LazyVim setup, where the nvim/Plugin/snacks.nvim/Picker runs searches with rg.
<leader>is the Space key.- Run
:checkhealth lazyvimif the picker reports thatrgis unavailable.
Open the Garden at Its Git Root
Search the Whole Repository
- Press
<leader>/.<leader>sgopens the same repository-root search on a mnemonic key sequence. - Type
the garden's rulesync targetsinto the picker without quotation marks. - The results update while you type. The list includes
README.md, where the complete phrase occurs on one line. This tutorial also appears because it contains the exercise phrase. - Move to the
README.mdresult with<C-j>and<C-k>or the arrow keys, then press Enter. LazyVim opens the file with the cursor on the match. - Notice that the spaces stay inside one search pattern. Quotation marks are unnecessary; typing them would ask
rgto find quotation-mark characters in the file. - LazyVim/Tutorial/Search for a Phrase Across Files/Q/How do we know that rg is used for search in LazyVim?
- Press
Treat Punctuation as Ordinary Text
- Press
<leader>/to open another repository search. - Press
<Alt-r>to turn off regular-expression matching, then typetags:: [[Diataxis/Tutorial]]. - The results include tutorial pages whose frontmatter contains that complete text. With regular expressions turned off, characters such as
[and]are matched literally. - Press
<Alt-r>again when a search needs regular-expression syntax. - LazyVim/Tutorial/Search for a Phrase Across Files/Q/Why does searching for the tags line for Diataxis Tutorial require turning off Regex matching in rg?
- LazyVim/Tutorial/Search for a Phrase Across Files/Q/What does the R indicate in the title of the Grep dialog?
- Press
Search Only the Current Directory
- Run
:cd pages. Neovim's current directory now ends in/pageswhile the Git root remains the garden's top directory. A floatingMessagesnotification reports the new path. - LazyVim/Tutorial/Search for a Phrase Across Files/Q/Why does cd pages show a Messages notification but pwd shows nothing visible?
- LazyVim/Tutorial/Search for a Phrase Across Files/Q/Are cd and pwd built-in vim commands that do something different than the shell version of cd and pwd?
- Press
<leader>sG; the uppercaseGchooses the current-directory form of the grep picker. - Type
Fuzzy file picker scoped to the detected root. - The results include
LazyVim___Keyshort___Search.mdunderpages/. Files outsidepages/are outside this search. - Run
:cd ..when you want Neovim's current directory to be the repository root again.
- Run
Restrict the Search to One File Extension
- Press
<leader>/to open a repository search. - Type
garden -- -g=*.md. Everything before--is the search phrase; everything after it is passed torgas extra arguments, here the rg/-g flag. - The results are limited to files whose name matches
*.md, so files such asmise.tomlor.gitignoreno longer appear even if they contain the wordgarden. -t mdworks the same way:garden -- -t mdrestricts the search torg's built-in Markdown file type, which already covers.md,.markdown,.mdx, and related extensions.- LazyVim/Tutorial/Search for a Phrase Across Files/Q/How does the picker split the phrase from the extra rg arguments?
- Press
Include Hidden or Ignored Files
- Open either grep picker and enter the phrase first.
- Press
<Alt-h>to include hidden files. The picker reruns the search with hidden files enabled. - Press
<Alt-i>to include files excluded by ignore rules. The picker reruns the search without applying those rules. - Press either key again to return to the usual project search.
What You've Learned
<leader>/and<leader>sgsearch the detected project root; in a Git repository, that is the Git root.<leader>sGsearches Neovim's current working directory, which:pwddisplays and:cdchanges.- A phrase containing spaces is one contiguous
rgpattern.<Alt-r>makes punctuation literal, while<Alt-h>and<Alt-i>broaden which files are searched. - Typing
--after the phrase, followed by flags such as-g=*.mdor-t md, restricts a search to one file extension or type. - Enter opens the selected match at the matching line.
11 Linked References
Hierarchy
- LazyVimTutorialSearch for a Phrase Across FilesQAre cd and pwd built-in vim commands that do something different than the shell version of cd and pwd?
- LazyVimTutorialSearch for a Phrase Across FilesQHow do we know that rg is used for search in LazyVim?
- LazyVimTutorialSearch for a Phrase Across FilesQHow does the picker split the phrase from the extra rg arguments?
- LazyVimTutorialSearch for a Phrase Across FilesQIs there a command to cd back to the original directory nvim was launched with, or do you need to remember where you are and use relative directory navigation?
- LazyVimTutorialSearch for a Phrase Across FilesQWhat does the R indicate in the title of the Grep dialog?
- LazyVimTutorialSearch for a Phrase Across FilesQWhy does cd pages show a Messages notification but pwd shows nothing visible?
- LazyVimTutorialSearch for a Phrase Across FilesQWhy does searching for the tags line for Diataxis Tutorial require turning off Regex matching in rg?