Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nano insert newline in search and replace

Basically, I need to doublespace a part of text. I know I can do it with:

sed G

but it would be great if I could do it directly in Nano.

I tried to search for the end of line ($) and then replace it with things like \n or [:newline:] but it seems that RegExp is recognized only for search and not for replace -- it always inserts the literal expression.

However, this question suggests it could be possible. Though I can't figure out how. And yes, I have:

set regexp

in my .nanorc

like image 263
Tom Karger Avatar asked Sep 21 '14 13:09

Tom Karger


People also ask

How do I search and replace in nano?

Find and Replace: Press Alt-R (hold down the ALT key and press the R key). You'll be asked what to search for. Enter it, then press the Enter or Return key. You'll be asked for replacement text.

How do I create a new line in Vim?

To get a newline, use \r . When searching for a newline, you'd still use \n , however. This asymmetry is due to the fact that \n and \r do slightly different things: \n matches an end of line (newline), whereas \r matches a carriage return.

How do I insert a line feed in vi?

The lowercase letter "o" lets you open a new line just below your current line. When you use this command, vi creates a new blank line below your current line, and puts you in insert mode at that position. The uppercase letter "o" lets you open a new line just above your current line.


1 Answers

set regexp in your config file will:

Do extended regular expression searches by default.

You can also activate it interactively with Meta + R*

But that's it! Search using RegEx, but as replacement Nano accepts only strings, except referenced captured groups \1 to \9 .

Nano's RegEx flavour is a POSIX implementation, and for what would interest you it does not compute [:newline:] but accepts \s, [:cntrl:] (Control characters) and [:space:] (All whitespace characters, including line breaks) also negated POSIX classes: [^[:graph:]]

Answer

It can't be done, nothing works, tried:

  • Whitespace Display Toggle Meta + P - no visible change
  • Verbatim Input Meta + Shift + V then pressing Enter would translate into a visible ^M encoding (but works for a Tab input)
  • "Special Functions" Esc + Esc then a 3 digit sequence - control characters not accepted (but accepts visible chars 013 would actually interpret it like the user pressed Enter)
  • Unicode Input Ctrl + Shift + U - control characters not accepted (but accepts visible chars)

ASCII codes for some control characters:

      DEC   HEX   
(\t)    9  0x09   Horizontal Tab
(\n)   10  0x0a   New Line
(\r)   13  0x0d   Carriage Return
 ( )   32  0x20   Space

Nano commands:

  • Search Ctrl + W
  • Find Next Meta + W*
  • Toggle replace while searching Ctrl + R
  • Search & Replace Ctrl + \

*Meta is usually the Alt key


PS: "sometimes failure is the answer, or someone should prove me wrong"
PPS: please
like image 99
CSᵠ Avatar answered Sep 19 '22 08:09

CSᵠ