Search and replace behavior for a text editor

The search and replace is one of those features that have a wide variety of different implementations and behaviors across applications. The nice thing with my work I did this summer on GtkSourceView, is that you can build whatever behavior you want by using the search and replace framework. And that’s exactly what I did for LaTeXila, which has a new behavior for the replace button.

Search and replace in LaTeXila
Search and replace in LaTeXila

When we replace an occurrence, we generally have the time and we don’t have a train to take (or we are already in the train). And it’s better to see how the content looks like with the replacement text, to see if everything is fine and make adjustments if required. As a consequence, when you click on the replace button, it just replaces the search match without moving to the next occurrence (which can be far away, so we would not be able to see the replaced text).

When you have just replaced an occurrence, no text is selected. If you click a second time on the replace button, it goes to the next occurrence, and selects it, to repeat the process. Nice, isn’t it?

Voilà, that’s all, I just wanted to share this information. It can be interesting for other applications.

6 thoughts on “Search and replace behavior for a text editor”

  1. Eh, I think I prefer the approach the Eclipse IDE uses, with separate buttons for “Find”, “Replace”, “Replace & Find”, and “Replace All”. It’s a heavier UI (a popup dialog with various options), but it works very very well for someone who uses it extensively. Having a single button that alternates between find and replace just seems confusing…

  2. It would be cool to use my recent undo-history to guess what I want to replace with what.

    If I have just corrected the word “your” to “you’re” twice, then I probably want to apply the same correction to the rest of the text, and therefore “your” and “you’re” could be default values in the search and replace form.

    1. Nice idea. You can take the last two items in the undo history, and with the surrounding content, guess what to put in the search and replace fields. With some machine learning with an input of good and bad results, it should be doable. The problem is that you will encounter cases where the result is not what you want, as always with machine learning.

    1. A regex search is in theory possible, but it gets more complicated if the “other text” is on a different line. A command line utility or a plugin for the text editor would be useful.

Leave a Reply

Your email address will not be published. Required fields are marked *