Files
it-know-how/(Neo) Vim/_alt/Project-Wide Find and Replace in Neovim with Telescope and Quickfix List.md
T
2026-04-02 17:39:43 +02:00

5.7 KiB
Raw Blame History

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, well 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

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

1. Initiate Telescope

In Neovims Normal mode, execute the following command:

:Telescope live_grep

This opens the Telescope live grep prompt, ready for your query.

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

After searching for your query, press Ctrl + q to open the search results in the Quickfix List.

Utilizing :cdo for Find and Replace

4. Run the :cdo Command

With the Quickfix List open, you can now perform the find and replace operation. Execute the following command:

: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

If youre satisfied with the changes and want to save them across all files in your project, you can use the :wa command:

:wa

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, youll 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 QuickfixListe 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 (<C-q>).

Jetzt willst du alle Zeilen in den Dateien löschen, die wits enthalten.
Das geht am saubersten mit :cfdo + :global:

: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:

/"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:

:copen

Einträge mit bestimmtem Muster entfernen

: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:

: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):

:cfdo g/wits/d | update