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 6046209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:12:44+00:00 2026-05-23T07:12:44+00:00

This question is nearly identical to this question except that I have to go

  • 0

This question is nearly identical to this question except that I have to go to three spaces (company coding guidelines) rather than four and the accepted solution will only double the matched pattern. Here was my first attempt:

:%s/^\(\s\s\)\+/\1 /gc 

But this does not work because four spaces get replaced by three. So I think that what I need is some way to get the count of how many times the pattern matched “+” and use that number to create the other side of the substitution but I feel this functionality is probably not available in Vim’s regex (Let me know if you think it might be possible).

I also tried doing the substitution manually by replacing the largest indents first and then the next smaller indent until I got it all converted but this was hard to keep track of the spaces:

:%s/^              \(\S\)/                     \1/gc

I could send it through Perl as it seems like Perl might have the ability to do it with its Extended Patterns. But I could not get it to work with my version of Perl. Here was my attempt with trying to count a’s:

:%!perl -pe 'm<(?{ $cnt = 0 })(a(?{ local $cnt = $cnt + 1; }))*aaaa(?{ $res = $cnt })>x; print $res'

My last resort will be to write a Perl script to do the conversion but I was hoping for a more general solution in Vim so that I could reuse the idea to solve other issues in the future.

  • 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-23T07:12:45+00:00Added an answer on May 23, 2026 at 7:12 am

    Let vim do it for you?

    :set sw=3<CR>
    gg=G
    

    The first command sets the shiftwidth option, which is how much you indent by. The second line says: go to the top of the file (gg), and reindent (=) until the end of the file (G).

    Of course, this depends on vim having a good formatter for the language you’re using. Something might get messed up if not.

    Regexp way… Safer, but less understandable:

    :%s#^\(\s\s\)\+#\=repeat(' ',strlen(submatch(0))*3/2)#g
    

    (I had to do some experimentation.)

    Two points:

    • If the replacement starts with \=, it is evaluated as an expression.
    • You can use many things instead of /, so / is available for division.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 files that are applicable to this question, myaccount.php and test5.php .
Direct Question : With three (or more) nearly identical copies of a class object,
This question was very helpful, however I have a list control in my report,
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Since this question is rather popular, I thought it useful to give it an
Super-short version: I solved this problem when I was nearly finished writing the question.
I recently posted this question about codes for a gift-card-like voucher that users can
This question is nearly identicle to : Php search string (with wildcards) , but
This question/answer is nearly exactly what I want to do. However, I wish to
I'm nearly complete with this, so I have one final bug I need your

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.