0

I just want to learn how vim regexp works and to be able to compose my own regexp for example, I have found this on google which searches for matching .sh files

#^\s\+.*sh$#

How does this work?

5
  • 2
    For starters there would be chapter 27 of the user manual with about 560 lines. You can reach it with :h usr_27. The reference manual on regex is pattern.txt which is about 1400 lines with all the details (:h pattern.txt). There is also a webpage vimregex.com named Vim Regular Expressions 101. – Hotschke Oct 31 '18 at 16:42
  • 1
    @Hotschke That answer wouldn't have helped me 20 yrs ago when I started using VIm, or today. The documentation is not written with the general user in mind it's aimed at people who already have an understanding – Steve Oct 31 '18 at 22:29
  • Well, that makes my comment (not an answer) sound stupid. Anyhow: the original title of this question was "Where can I find a comprehensive vim regex tutorial?". I was trying to point out the essentials which statox did not mention. I can agree that understanding a specific regex as a beginner, a general introduction would probably not cover features used in the one you want to understand. IMHO pattern.txt is nothing you will totally learn in your life and can be overwhelming for a beginner or everyone. Going through chapter 27 covers a selection of features, i.e. it will not suit everyone. – Hotschke Nov 1 '18 at 7:47
  • @Steve: since you have 20 years of experience. Do you know a beginner friendly vim regex tutorial? BTW a comprehensive tutorial is not realistic. There is a good reason why statox changed the title. How do you learn it? I am pretty sure alternatives or sharing experiences is appreciated. I can say for myself pattern.txt is the vim help page I have opened the most in the past and I do not expect that this will change. It simply takes time to learn it. As Euclid (Elements) said: There is no royal road to geometry / vim / vim regex. – Hotschke Nov 1 '18 at 8:02
  • @Hotschke Unfortunately no. This is a good guide learnvimscriptthehardway.stevelosh.com. I learned regex on Sun Unix and Emacs before I saw the light, documentation even worse, but there are reasonable perl style guides. Unfortunately the syntax is different. This was a good help vimregex.com. I just write my own help files so the impenetrable is understandable by me – Steve Nov 1 '18 at 22:05
5

All you need to know about Vim regex can be found directly in the doc with :h regexp. You can read all the file until :h perl-patterns, that's a long read but it is worth it.

Now if you read the doc (:h pattern-atoms) you can find the meaning of the different atom used :

#^\s\+.*sh$#
 ^                  The beginning of the line
  \s\+              At least one (\+) whitespace (\s)
      .*            Zero or more (*) character (.), any character
        sh          Literraly the characters 's' then 'h'
          $         The end of the line

Note that the # character at the beginning and end of the regex were probably used as delimiters in a substitution command like this:

%s#^\s\+.*sh$#foo.sh

This will rename every .sh file name to foo.sh, see :h :s (Note that people often use ; as delimiters but you can also use # or almost any other character)

2
  • @statox Nice answer. As a followup, cause I struggle with it, is how do you do a non greedy regexp for anything, alpha, numeric, whitespace or special – Steve Oct 31 '18 at 22:31
  • @Steve I'm not sure I completely understand what you mean. Are you talking of something like .\{-} as mentioned at :h non-greedy? – statox Nov 3 '18 at 11:06

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.