Vim regexes

One thing I very much miss in Gentoo is controlling what is compiled into my Vim. You need to enable perldo and rubydo support at compiletime. Gentoo had USE flags to do it. In Ubuntu I get perldo but no rubydo by default, which is annoying.

The reason I need perldo/rubydo is because Vim's regexes are so inconsistent. * is special when not escaped, but + is special when escaped. You can use \{x,y} (escape only opening bracket), but you have to use \( \) (escape both parens), and with [] it's special when you don't escape either. I simply can't remember these, especially when coding at full speed, and speed is one of the reasons to use Vim in the first place.

Then Vim has magic and nomagic. And you can use \v to set "very magic". Very magic is almost what I want, but you can't set it in your .vimrc and even if you could, the Vim manual tells you to leave the setting of magic alone if you know what's good for you.

PCREs are so much more consistent and easier to remember (excepting Perlisms like "dot matches newline" inconsistencies). The special characters are always special, and you escape them all to make them non-special. But perldo and rubydo in Vim can't do everything Vim regexes can do; they can't properly span lines, is the major thing. They don't highlight text like Vim does with its builtin regexes if you have hls set.

I read somewhere that Vim regexes are set up to let you match C code easily, and that's why for example {} are non-special by default. I don't remember where I read it or if it's true. Doesn't help a lot when writing non-C code though.

7 Responses to “Vim regexes”

  1. Quoth chi:

    I know exactly what you mean. I am currently trying to write my own syntax highlighting script for a certain wiki syntax and have the pattern help pages opened all the time because I often cant remember what needs to be escaped within vim regexes an what not. “help: perl-patterns” gives a - lets say “little short” explanation about their differences, but I guess it`s better not to get used to vim regexes - could lead to confusion in other places.

  2. Quoth numerodix:

    Hey Brian, I'm new to vim… how do I unset stuff? :set list ayeeee how do I unset this?

  3. Quoth Brian:

    :set nolist. Generally there's a `:set command` and `:set nocommand` pair.

  4. Quoth numerodix:

    Thanks. Another one for you. How do I allow the humble cursor to move beyond the last character of a line in command mode? He really would love to, but he's not allowed there. :(

  5. Quoth Brian:

    I don't think you can. Generally to enter text at the end of a line I hit A (capital A, note). Not sure what other reason there would be to move the cursor beyond the text like that.

  6. Quoth Armin Ronacher:

    apt-get install vim-ruby

  7. Quoth Brian:

    vim-ruby does not seem to enable rubydo. Dunno.

Leave a Reply

You can use these tags in comments (Note: HTML is automatically escaped inside <pre> tags, nowhere else, so if you post source code, put it in <pre>):

<pre lang="some_programming_language"> 
<em>
<strong>
<a href="url">

NOTE: Comments are automatically spam-filtered. If your comment fails to appear, it was likely munched by the filter. Try not to link-spam or post anything that looks like it was typed by a robot.