Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:03:58+00:00 2026-05-30T09:03:58+00:00

I am very new to regular expressions, and I need some help. First I

  • 0

I am very new to regular expressions, and I need some help. First I will explain my motivation and some diagrams, as it is very hard to explain otherwise.

I recently installed Vim Indent guides which displays those vertical bars/rulers like so (picture from github account):

Vertical Bars

The way it does this is by pattern matching spaces at the beginning of a line and adding them to either the IndentGuidesEven or the IndentGuidesOdd. The problem with this is that it can’t pattern match empty lines, and you get less than ideal hilighting like so:
Poor highlighting

The easiest solution is to remove all empty/blank lines, but code without spaces can be hard to read. What I had in mind is to transform the code in several stages, and ultimately adding spaces, as shown below.

Animation

What I am doing is:

  • removing spaces from all lines with only spaces %s/\s\+$//e
  • truncating all multiple empty lines %s/\n\{3,}/\r\r/e
  • adding spaces to empty lines %s/^\ \(\ *\)\([^\ ]\)\(.*\)\n^\ *$\n^\ /\ \1\2\3\r\ \1\r\ /gc

what that last statement does is look at three lines where the first and third non empty and the second one is empty. But this leads to problems if the first line is, for example indented in 8 times and the third one is only indented in 3 times. Is there anyway, upon finding the first pattern (in this case 8 indents) to use it in the same search pattern to make sure lines 1 and 3 start with the same number of spaces? I’m sure I could do this with an iterative function, and start from, for example 30 indents, and work my way back, but that might be a little inefficient.

I am aware that lines with only spaces are bad. However, removing spaces is trivial and I have key mapped to do this automatically. If need be I can quickly remove them. Also, I know the problem is more complex than this, and there are more cases to consider, however, I will deal with those cases as they present themselves.

Any advice as to how I could go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:03:59+00:00Added an answer on May 30, 2026 at 9:03 am

    In order to implement the third step according to your last comment, one can
    use the following command.

    :%s/^$/\=repeat(' ',min([indent(line('.')-1),indent(line('.')+1)]))
    

    This global substitution relies on the substitute with an expression feature
    (see :help sub-replace-\=) to replace empty lines with a string containing
    a space character repeated min([indent(line('.')-1),indent(line('.')+1)])
    times. The number of spaces is calculated as a minimum of two values joined
    in a temporary list. These values are indentation levels of the lines
    immediately preceding and succeeding the current one (line('.') evaluates to
    its number); the levels of indent are determined using the indent()
    function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long time user, first time asker; very new to regular expressions. I'm a designer
I have a problem using preg_match. (I'm very new to regular expressions) if(preg_match('^/http:\/\/myWebsite\.com\/', $_SERVER['HTTP_REFERER']))
I am very new programmer to Java regular expressions. I do not want to
I am very new to both Python and regular expressions, so bear with me.
I'm very new to javascript and have been playing around with regular expressions. I'm
I'm very new to Regular Expressions, but I thought it'd be the best way
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Very new to XSL (and XML for that matter), but I need to step
I'm very new to C#. My boss asked me to wrote some code using
I am very new to C++ programming and you will see why. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.