31

I am aware of nano's search and replace functionality, but is it capable of using regular expressions for matching and substitution (particularly substitutions that use a part of the match)? If so, can you provide some examples of the syntax used (both for matching and replacing)?

I cut my teeth on Perl-style regular expressions, but I've found that text editors will sometimes come up with their own syntax.

Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
  • You can start it with the `-R` flag according to [the nano documentation](http://www.nano-editor.org/dist/v1.2/nano.html#SEC3). – gpojd Oct 12 '11 at 19:56
  • @gpojd The documentation you link is quite dated. For the version(s) of nano that ship with all modern OSs (as far as I can tell), the -R flag is for "restricted", not "regex" as in the older versions. – Daniel Standage Oct 12 '11 at 19:59

5 Answers5

29

My version of nano has an option to swtich to regex search with the meta character + R. In cygwin on Windows, the meta-key is alt, so I hit ctrl+\ to get into search-and-replace mode, and then alt+r to swtich to regex search.

user151841
  • 17,377
  • 29
  • 109
  • 171
  • 2
    +1 for the alt+r. This is half the real answer. @SPArifSahariWibowo has the bigger half. – Bob Stein Jun 19 '13 at 14:56
  • For those who use Mac: http://osxdaily.com/2013/02/01/use-option-as-meta-key-in-mac-os-x-terminal/ – emilyk Feb 10 '15 at 23:32
  • this combined with S P Arif Sahari Wibowo's answer is the correct answer IMO – JoSSte Oct 19 '18 at 03:53
  • For those with German keyboard: It's Strg-AltGr-\ because you need AltGr to get the backslash. – JPT Oct 26 '19 at 12:04
  • On Ubuntu, I did ^w (to search) and Alt+R made it regexp. – Shayan Aug 16 '21 at 10:55
  • I just had to use "Alt+R" TWICE the first time to make sure I was in regexp search mode. You only need to hit that once the next times. It should say `[Regexp]` if you are in the right mode. – Akaisteph7 Sep 01 '22 at 21:45
23

You need to add, or un-comment, the following entry in your global nanorc file (on my machine, it was /etc/nanorc):

set regexp

Then fire up a new terminal and press CTRL + / and do your replacements which should now be regex-aware.

EDIT


Search for conf->(\S+):

enter image description here


Replace with \1_conf

enter image description here


Press a to replace all occurrences:

enter image description here


End result:

enter image description here

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks Bart. I have done that, but I'm still getting a "not found" response from nano when I know it should be matching the regex. How would I translate the following Perl regex into something nano would understand? `s/conf->(\S+)/$1_conf/` – Daniel Standage Oct 12 '11 at 20:45
  • @Daniel, added some screen-shots. – Bart Kiers Oct 12 '11 at 20:54
  • Thanks, this worked great on my Ubuntu machine. Unfortunately, it did not work on my Mac. The version that ships with Snow Leopard is 2.0.x, whereas Ubuntu is using the latest stable (2.2.x). I even tried compiling from the latest stable source code, but still the regexp is not working on my Mac. Oh well... – Daniel Standage Oct 12 '11 at 21:34
  • 8
    Thanks a lot! Instead of editing the config file, you can also press Meta+R (Alt+R) while in search to toggle regex search on/off. – Arno Teigseth Aug 27 '15 at 14:35
  • Mac keyboard "Meta+R" (Option-R) is the Registered Trademark ®. In Terminal change this in Preferences, Profiles tab, Keyboard subtab (on right), checkbox at the bottom "Use Option as Meta key". – Able Mac Jul 25 '19 at 17:06
  • 1
    For those without super user access or those who don't want to mess up config for other users edit $HOME/.nanorc instead – Lee Nov 28 '19 at 08:53
15

The regular expression format / notation for nano use "Extended Regular Expression", i.e. POSIX Extended Regular Expression, which is used by egrep and sed -r, this include metacharacters ., [ and ], ^, $, (, ), \1 to \9, *, { and }, ?, +, |, and character classes like [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].

For more complete documentation you can see manual page, man 7 regex in Linux or man 7 re_format in OS X. This page may give you same information as well: https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended

Unfortunately in nano there seems to be no way to match anything that span across multiple lines.

  • 4
    +1 for the multiline clue. And `$1` doesn't work in the second string but `\1` does! (For parenthetical subexpressions.) Neither `\n` nor `[:newline:]` seems to work in the second string. – Bob Stein Jun 19 '13 at 14:52
  • Nano has it's own start/end commands to capture groups which span multiple lines. A C-style block comment could be defined as: color brightblack start="/\\*" end="\\*/" – awwsmm Apr 23 '18 at 08:50
  • Regular character classes did not seem to work for me. So `[:digit:]` works while `\d` did not. Thanks! – Akaisteph7 Sep 01 '22 at 21:46
1

This is a bit old, just updating the search index.

Nano 5.5 uses the ASCII column from this same table.

Thanks to @S P Arif Sahari Wibowo ,

I found the answer here anyway (same wiki link): https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended

0

I was recently faced with the problem of inserting text at the beginning of everyline that started with a numerical digit. For that the only way to distinguish this from text i didn't want to change was the previous new line.

Playing around with the information provided in this answer I was able to do it and decided to add it to the answer in case somebody else faces the same situation.

To search for the beginning of the line followed by a number and then insert "Text String" at the beginning of each line that starts with a number:

\ then "(^[0-9])" press carry return, then: "Text String 1" press carry return and the select yes, if it does what you want next press a for all. Omit the " quotation marks.

Julio Spinelli
  • 587
  • 3
  • 16