2

In 'nomagic' mode, ^ and $ have a special meaning and no others do. Why were these selected, rather than making people type \^ and \$ just as they do for \. and \*?

EDIT: I have been able to confirm that this behavior, and the magic option, goes back to ex version 1.1 (regex implementation, options). The structure also suggests an answer for ^: It is handled specially outside the main loop.

4

The nomagic setting was created to support the edit mode, which was "designed for more casual or beginning users", rather than as a general "nothing is a metacharacter" mode.

From the ex reference manual:

The ex default setting of magic gives quick access to a powerful set of regular expression metacharacters. The disadvantage of magic is that the user must remember that these metacharacters are magic and precede them with the character '\' to use them as "ordinary" characters. With nomagic, the default for edit, regular expressions are much simpler, there being only two metacharacters. The power of the other metacharacters is still available by preceding the (now) ordinary character with a '\'. Note that '\' is thus always a metacharacter.

The remainder of the discussion of regular expressions assumes that that the setting of this option is magic. To discern what is true with nomagic it suffices to remember that the only special characters in this case will be '^' at the beginning of a regular expression, '$' at the end of a regular expression, and '\'.

When started as edit, ex 1.1 sets noopen (not permitted to enter visual mode), nomagic, and notify=1 (ex default is 5, notify is equivalent to vim report).

The nomagic setting also affects the usage of & and ~ in substitution commands.

(Still no idea why, but we'd probably have to ask someone who was at Berkeley in the '70s for that.)

  • Thanks for writing this up! When the pdf manual I found didn't format text correctly, I gave up on quoting it directly without thinking to just search for another version – Matt Boehm Feb 27 '15 at 19:35
  • @MattBoehm Firefox's built-in PDF viewer has problems with copy and paste in my experience; have you tried opening it in Acrobat Reader? And I formatted this by hand from the source; I didn't see the PDF you found. – Random832 Feb 27 '15 at 19:36
  • Things were messed up enough that I think the PDF was generated from an OCR scan, but I'll try acrobat in the future. – Matt Boehm Feb 27 '15 at 19:38
3

Apparently magic/nomagic dates back to Ex (mentioned in the pdf user manual), but I'm not sure of the reasoning behind the original decision. If you're using Vim, there are 4 levels of 'magic' you can use. The two additional modes are explained in :help /magic:

Use of "\v" means that in the pattern after it all ASCII characters except
'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning.  "very magic"

Use of "\V" means that in the pattern after it only the backslash has a
special meaning.  "very nomagic"

This at least seems to have some logic to it. If you want no characters to have special meaning besides \ in Vim, you can start your pattern with \V

  • I don't think "very" mode existed in vi. And \V seem to have been created explicitly because nomagic/\M does not do this. – Random832 Feb 27 '15 at 18:48
  • Correct, I'll edit my query to mention that this is vim-specific – Matt Boehm Feb 27 '15 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.