19

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

Community
  • 1
  • 1
Tom Karger
  • 305
  • 1
  • 2
  • 6

5 Answers5

15

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
CSᵠ
  • 10,049
  • 9
  • 41
  • 64
6

Not only it is currently not possible to do this in nano, but my feature request was also marked as "wontfix, too hard" in the bug tracker as well.

5
  1. Select the text block.
  2. Press insert command.
  3. Press flipexecute command.
  4. Press flippipe command.
  5. Press flipnewbuffer command.
  6. Execute whatever external command you want. The command will be piped the selection into stdin and the selection will be replaced by stdout of the command.

Furthermore, you can automate this by recording and running macros. You can also bind keys to macro-sequences.

MarkWeston
  • 740
  • 4
  • 15
  • 1
    @SmallBoy What versions of nano and sed are you using? Your example of duplicating new lines isn't working for me. I'm using sed 4.8 and nano 6.3. – Brōtsyorfuzthrāx Aug 03 '22 at 10:58
  • 1
    @Brōtsyorfuzthrāx Forgot `-z` which is used in GNU sed to change delimiter from new-line to null byte. Sorry. Deleting the wrong one and re-writing: The exact steps are: 1.Select the whole search area(`Shift+Arrows`, or `Alt+A`). 2.Open `Execute` menu(`Ctrl+T`). 3.Type `|sed -z 's/to_be_cut/to_be_put/g'`. i.e. to replace newlines with 2 newlines: `|sed -z 's/\n/\n\n/g'`. 4.Press Enter. – Small Boy Aug 04 '22 at 20:12
0

In Nano 6.4, you can do it like this (or a similar way you could figure out yourself if you don't like my particular method):

Open your ~/.nanorc file with Nano (not some other editor).

Type bind F7 "|reg "" """ main

Then put the cursor between the "| that you typed. Then press Alt+v and then press Ctrl+t. It should output some characters. Then go to just before the last double quote and press Alt+v; then press the left arrow key; repeat that (with Alt+v and then the left arrow key) three more times (It should output some characters; so, it should end up looking something like bind F7 "^T|reg "" ""^[[D^[[D^[[D^[[D" main.

Save and exit the .nanorc file.

Then make this Python script (call it reg; you don't need to add .py to the end, unless you want to; make it executable with such as chmod +x reg) and put it in your path (if you're on Ubuntu, if you make a ~/bin directory (so you can put it at ~/bin/reg), it'll be in your path ever after once you restart your computer (otherwise, you can set your desired path in your ~/.bashrc file); anyway, here's the code for the reg file:

#!/usr/bin/env python3

import re, sys

text=sys.stdin.read()
reg=""
if len(sys.argv)==3:
    #Usage: ctrl+t; |reg.py "my regex" "my replacement" (don't forget the quotes, or some such)
    reg=sys.argv[1]
    rep=sys.argv[2]
    result=re.sub(reg, rep, text)
elif len(sys.argv)==2:
    reg=sys.argv[1]
    result="\n".join(re.findall(reg, text))
else:
    result=re.sub(r"", r"", text) #Edit the regular expression here.
print(result)

Close Nano if it's open.

Open Nano.

Now, every time you press F7, it'll launch the execute command menu/prompt with the default text |reg "" "", with your cursor in between the first set of double quotes. Put your search text in the first set of quotes, and your replace text in the second set of quotes. Press enter when you wish to do the replacement. This allows you to do the regular expressions you can do in Python 3.x, which are quite nice (and allow new lines, groups, and other stuff). It'll search and replace the whole file, unless you have something selected (then it'll search and replace the selection, and for some reason it always puts a new line after the selection after a replace).

If you just do one set of quotes with a regular expression, it'll replace your document or selection with each of the matches (you can undo it with Alt+u).

If you don't have any quotes, but just do |reg, then it'll do the default regular expression that you have saved in the Python program named reg that you put in your path.

If you don't have Nano 6.4, and you're using Ubuntu, or such, you can compile it fairly easily. Add the source code repositories for your updater, if you haven't already. Then, do sudo apt build-dep nano. Then download the latest source code from nano-editor.org, and follow the instructions in the source code to compile.

For information about rebinding keys in order to execute external commands, see the Nanorc documentation, particularly this quote:

bind key "string" menu

Makes the given key produce the given string in the given menu (or in all menus where the key exists when all is used). The string can consist of text or commands or a mix of them. (To enter a command into the string, precede its keystroke with M−V.)

Brōtsyorfuzthrāx
  • 4,387
  • 4
  • 34
  • 56
0

You can filter the text more powerfully from nano by piping it through an executed command:

  • Optionally, select some text to filter (move cursor to start, Alt A, move cursor to end, Alt A again)
  • Execute: Ctrl T
  • New Buffer: Alt F (Optional, otherwise the selected text in the current buffer is replaced)
  • Enter your command with a pipe before it, e.g. '|sed G'
    (note, you can use Alt \ while typing to toggle adding/removing the pipe)

The selected text (or whole document if nothing selected) will be piped through the given command and replaced with the output.

(Or a New Buffer will be created with the output, if you chose that option.)

Note: Ctrl T only works from nano 5.0. For older versions you can do Ctrl R, Ctrl X instead.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35