A. Wilson
A. Wilson

Reputation: 8840

Remove Various Whitespaces While Editing in Vim

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.

Upvotes: 0

Views: 271

Answers (3)

A. Wilson
A. Wilson

Reputation: 8840

This is a while later, but I found that there is a command that does what I need to in 90% of circumstances:

J --  join line below to the current one

Upvotes: 1

ldog
ldog

Reputation: 12151

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.

Upvotes: 0

jamessan
jamessan

Reputation: 42667

You really just want to reformat that comment to fit the current 'textwidth'. If the comment is a paragraph (i.e., separated by a line of whitespace above and below), then you can just use gqip (gq is the reformat command, ip is the "inner-paragraph" text object) to reformat it. If it's not a standalone paragraph, you can visually select those lines and then use gq.

This likely also relies on having 'formatoptions' set correctly to make sure the comment characters are handled properly, but in many cases the ftplugin has already done that.

Upvotes: 1

Related Questions