[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: lines and regexp
From: |
Benno Schulenberg |
Subject: |
Re: lines and regexp |
Date: |
Thu, 8 Sep 2022 09:36:38 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 |
Op 07-09-2022 om 18:57 schreef Mike Scalora:
> I noticed something curious about lines and regexp search and replace in
> nano. If
> I open a file with two lines which nano reports the number of lines on open
> and
> search and "replace all" on the regexp $ (or ^), I get 3 replacements. It
> doesn't
> matter if the second line in the file has a newline or not, I get the same
> result
> because nano adds the second newline automatically. This contradicts nano's
> reporting of the number of lines read and adds yet another line to the file.
This latter thing is why 'nonewlines' became the default in nano-4.0. (But it
was quickly reverted in nano-4.1, because wanting to add something at the end
of the file had become a nuisance: after M-/ one had to type <End> and <Enter>
first.)
The user can easily prevent the unexpected replacement and the addition of the
extra new line by specifying 'set nonewlines' in their ~/.nanorc. But using
this option results in the mentioned nuisance, so I don't think most users
would want that.
Alternatively, we could change nano's behavior as in the attached patch, that
prevents any non-explicit replacement on the magic line. Opinions?
> ==$ nano /tmp/test.txt
> ^w^r$<return><space>pie<return>a^s^x
Apparently you have 'set regexp' in your ~/.nanorc. I have not, and start a
replacement session always with M-R (not ^W^R), followed by another M-R when
I want to do a regex replacement.
> Idea: Remove the trailing newline during the regexp operation.
The patch prevents the replacement on the magic line and thus prevents the
addition of a new magic line.
> I didn't see a way to actually search for a newline
There is none. Section 3.6 in the info manual (`info nano edit search`)
mentions: "A regular expression in a search string always covers just one line".
Suggestions on how to improve the wording in this section are welcome.
Benno
prevent-replacement-on-the-magicline.patch
Description: Text Data
OpenPGP_signature
Description: OpenPGP digital signature