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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:58:51+00:00 2026-05-26T10:58:51+00:00

So oftentimes, while editing with Vim, I’ll get into a variety of situations where

  • 0

So oftentimes, while editing with Vim, I’ll get into a variety of situations where whitespace gives me hassle. For example, say I have a comment like this:

#This program was featured on the Today show, it is an algorithm for promoting world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause

If I want to, for example, trim the lines so they go to X characters, I end up putting a newline somewhere in the middle of the top line to get this (after hitting the newline and auto-indenting):

#This program was featured on the Today show, it is an algorithm for promoting
world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause

I then add a # to the beginning of the line, and that’s all well and good, but then I want that line to line up, too. To do so, I have to delete the newline, all the whitespace for the indent on the next line, and then the commenting # mark. It doesn’t take an awfully long amount of time to do that, but this and similar situations all add up over a day’s worth of coding.

Now the example above is pretty specific, but my question isn’t. What’s a good way in Vim to delete all whitespace INCLUDING NEWLINES up until the next non-whitespace character? If Vim already has movements that do that, that would be awesome, but if not, does anyone have a favorite Vim function they use to do the above that could be mapped to a key? At the very least, am I missing some Vim usage idiom that prevents me from even having to worry about this case?

EDIT: Formatting to width, while useful and applicable to the case above, isn’t the focus of this question. I’m concerned more with whitespace removal that doesn’t stop at the end of a line, but instead carries on to the first non-whitespace character of the next line.

  • 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-26T10:58:52+00:00Added an answer on May 26, 2026 at 10:58 am

    This command seems to work:

    :.s/\W*$\n\W*//g
    

    it uses a replace to remove whitespace up to end of line and the new line at the end.

    In this example:

    testting                 aad        $            
    asdjkasdjsdaksddjk$
    

    (to see meta characters in vim use the command :set list)

    if you place the cursor on the first line and use the first command it will delete everything from aad to $ (not including aad but including $ and a newline.)

    Also, note for what you are doing it is far more efficient to use an external program to format comments for you. In particular, par is a great small C program that edits text and wraps it to desired lengths.

    If you have par in your path, to do what you are trying to do is as easy as selecting the block of comment with Shift+v and running the command

    :!par 40pgr
    

    where 40 is the desired width in columns.

    If you are feeling hackish, write your own program in C/perl/C++/python that edits comments however you like, then put it in path and use the external filter command :! to process blocks of text through it.

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

Sidebar

Related Questions

So sometimes (oftentimes!) you want to target a specific .NET version (say 3.0), but
Let's say that I routinely have to work with files with an unknown, but
Oftentimes, when using git, I find myself in this situation: I have changes to
I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .
Oftentimes, you have a problem where property A can be either true or false,
oftentimes I have this kind of usage scenario: entity A is connected to B
i have a huge database of scraped forum posts that i am inserting into
Oftentimes a developer will be faced with a choice between two possible ways to
Have a n-tire web application and search often times out after 30 secs. How
I have SQL table that has a varchar(8) column that occasionally has binary data

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.