zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.43k stars 1.16k forks source link

`replace \$\$ \*` causes micro to freeze #2440

Open andrejpodzimek opened 2 years ago

andrejpodzimek commented 2 years ago

Description of the problem or steps to reproduce

  1. Open a document that contains $$ at least once. Or open an empty one and type $$ into it.
  2. Press Ctrl+E and input replace \$\$ \*. The editor will freeze.

Specifications

Commit hash: micro -version doesn’t show any hash. It says “Version: 2.0.8”. OS: Linux Terminal: Guake, Yakuake, Konsole

dmaluka commented 3 months ago

It doesn't freeze actually, it just doesn't replace the $$, instead it "replaces" the empty space at the end of the line, i.e. just inserts * at the end of the line. The dollar signs remain unreplaced no matter how many times the user says "yes", so it might seem to the user that nothing happens.

Unfortunately this is a "feature, not a bug". The $ is interpreted as the end of the line, and escaping it with a single \ doesn't help, since this \ is already taken by the command-line argument parser (i.e. what the regex parser receives as an input is $$, not \$\$).

To solve this, escape with double backslashes (\\):

replace \\$\\$ \*

or use single quotes:

replace '\$\$' \*
dmaluka commented 3 months ago

Unfortunately this is a "feature, not a bug".

Actually, since this isn't the first or the last time when this "feature" is causing a lot of confusion, perhaps we should consider finally fixing it, by making an exception for the 1st argument of the replace command: parse it directly with the regex parser, bypassing the command argument parser.

JoeKar commented 3 months ago

Will it then still work with single- or double quotes given around the pattern? Just thinking about it, since it's somehow common to us a kind of quotes with egrep and sed too. But on the other hand it's most probably not sufficient to add exactly this to the documentation.