25

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
  • 7,189
  • 14
  • 61
  • 101
  • 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

4 Answers4

25

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
  • 15,348
  • 28
  • 93
  • 156
17

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
  • 153,868
  • 34
  • 276
  • 272
  • 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
  • 5
    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
14

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
0

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