## Introduction[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#introduction) Maintaining an organized and efficient codebase is essential for every developer. Neovim, a powerful text editor, offers a range of tools that can help you make global changes throughout your project with ease. In this blog post, we’ll explore how to perform a project-wide find and replace using Neovim, Telescope, the Quickfix List, and the `:cdo` command. No complicated jargon, just straightforward guidance to enhance your coding experience. ## Prerequisites[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#prerequisites) Before you start, ensure you have the following prerequisites in place: 1. Neovim installed. 2. The Telescope plugin installed. 3. A project directory opened in Neovim. ## Using Telescope for Live Grep[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#using-telescope-for-live-grep) ### 1. Initiate Telescope[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#1-initiate-telescope) In Neovim’s Normal mode, execute the following command: ```shell :Telescope live_grep ``` This opens the Telescope live grep prompt, ready for your query. ### 2. Enter Your Search Query[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#2-enter-your-search-query) Type your search query. Telescope will display a list of search results throughout your project. ### 3. Open Results in Quickfix List[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#3-open-results-in-quickfix-list) After searching for your query, press `Ctrl + q` to open the search results in the Quickfix List. ## Utilizing `:cdo` for Find and Replace[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#utilizing-cdo-for-find-and-replace) ### 4. Run the `:cdo` Command[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#4-run-the-cdo-command) With the Quickfix List open, you can now perform the find and replace operation. Execute the following command: ```shell :cdo s/query_string/replacement_string/gc ``` - `:cdo` applies a command to each entry in the Quickfix List. - `s/query_string/replacement_string/gc` is the find and replace command, with `g` for global replacement. - The `/c` flag provides confirmation before each replacement. Press Enter to initiate the find and replace with confirmation. ### 5. Save Changes[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#5-save-changes) If you’re satisfied with the changes and want to save them across all files in your project, you can use the `:wa` command: ```shell :wa ``` ## Conclusion[](https://blog.chaitanyashahare.com/posts/project-wide-find-and-replace-in-neovim/#conclusion) Performing a project-wide find and replace in Neovim using Telescope, the Quickfix List, and the `:cdo` command is a potent approach that simplifies codebase maintenance. It allows you to make comprehensive changes while retaining full control over the replacement process, with the added `/c` flag providing a confirmation step for each replacement. By incorporating these techniques into your Neovim workflow, you’ll significantly improve your coding experience and efficiency. Use `:wa` to save your changes across all files and ensure your project is up to date. # Bsp Zeilen mit Treffern löschen Es gibt zwei leicht verschiedene Dinge: 1. Zeilen **in den Dateien** löschen 2. Einträge **aus der Quickfix‑Liste** löschen Ich nehme an, du meinst 1., also die Zeilen in den Dateien entfernen, wo `live_grep` dein Wort gefunden hat. Ich zeige aber beide Varianten. --- ## 1. Zeilen in den Dateien löschen (empfohlen) Du hast `live_grep` mit z.B. `wits` gemacht und die Treffer in die Quickfix-Liste geschickt (``). Jetzt willst du **alle Zeilen in den Dateien löschen, die `wits` enthalten**. Das geht am saubersten mit `:cfdo` + `:global`: ```vim :cfdo g/wits/d | update ``` Erklärung: - `cfdo` – führe den folgenden Befehl für jede **Datei** aus, die in der Quickfix-Liste vorkommt - `g/wits/d` – in dieser Datei: lösche **alle Zeilen**, die `wits` enthalten - `| update` – speichere die Datei, wenn sie geändert wurde Damit sind nachher in allen Dateien, die in der Quickfix-Liste sind, sämtliche Zeilen mit `wits` weg. ### Variante ohne hartkodiertes Muster Wenn du das Suchmuster noch einmal mit `/` gesetzt hast, kannst du es statt `wits` auch aus der letzten Suche nehmen: ```vim /"dein muster" :cfdo g//d | update ``` `g//` benutzt dann das aktuelle Suchmuster aus `/`. --- ## 2. Zeilen *aus der Quickfix-Liste* löschen Falls du **nur die Einträge in der Quickfix-Liste selbst** filtern oder löschen willst (nicht die Dateien): Quickfix-Fenster öffnen: ```vim :copen ``` ### Einträge mit bestimmtem Muster entfernen ```vim :cfilter! wits ``` - `:cfilter {pat}` – behält nur Einträge, die `{pat}` matchen - `:cfilter! {pat}` – entfernt Einträge, die `{pat}` matchen Mit `:cfilter! wits` würdest du also alle Quickfix-Einträge wegwerfen, die `wits` enthalten (die Dateien bleiben unverändert). --- ## 3. Warum nicht `:cdo normal! dd`? Rein technisch könntest du auch: ```vim :cdo normal! dd ``` Das löscht in jeder Datei die jeweilige Trefferzeile. Aber: `:cdo` läuft pro **Quickfix-Eintrag**; wenn du Zeilen löschst, können die nachfolgenden Einträge (mit alten Zeilennummern) verrutschen. Deshalb ist `:cfdo g/pattern/d` robuster – da wird pro Datei einmal gearbeitet, nicht pro Eintrag. --- Kurzfassung für dein konkretes Beispiel (`wits` aus den Dateien löschen): ```vim :cfdo g/wits/d | update ```