2

I know how to remove trailing white spaces at the end of lines. Does anyone know how to remove white spaces in the middle of a line? The white space I mean is before tab and after tab characters.

For instance (tabs are shown as ^I):

struct {
^Iint a;
^I int b; // Here we've got an extra whitespace after the tab. I want to ditch it.
^I}
7
  • 6
    Your title mentions tabs, but the body of your question doesn't. Do you mean multiple spaces between words? Or do you mean something that interacts with tabs? I guess I would benefit from a concrete example Commented Feb 16, 2015 at 6:44
  • 1
    Agreed, can you provide a concrete example please? Bit confused why you'd want this, which makes me think we/I don't understand the question. Commented Feb 16, 2015 at 13:02
  • Not sure what is confusing about this question. Seems pretty obvious what the OP wants. Commented Feb 16, 2015 at 22:11
  • 1
    What about spaces in the middle of the line? For example how do you want to deal with a line like ^Iint a; ^I ; int b; (^Is are tabs)? The spaces around the second tab should be removed?
    – toro2k
    Commented Feb 17, 2015 at 8:55
  • 1
    Downvoted because even with the edit, it's still pretty unclear what whitespace should or shouldn't be removed. Is the example the only type of space that is to be removed? If so, why does the first para say "before tab and after tab" characters, and why use the confusing term "white spaces" if the question is only referring to space characters?
    – Rich
    Commented Feb 17, 2015 at 14:14

3 Answers 3

4

This command will remove all whitespaces on both sides of one or more tabs in every line of the current buffer:

:%s/ *\([\t]\+\) */\1/g
4

Are you trying to remove the unnecessary spaces or are you just trying to format your code correctly. Because there are different solutions to the problem.

Do correct formatting:

You can get vim to correctly indent your code by using ={motion} command

The easiest way to use this is to use visual mode.
Go to the start of the section you want correctly indented. Hit Ctrl-V. Move the cursor to the end of the section you want correctly indented (can be the whole file). Hit = key and your code will be correctly formatted.

Replace space (where you have tabs to indent).

Since you are using tabs.
The following assumes you don't have expandtabs set

The following should preserve the normal indentation of your lines. Any lines with extra space characters at the end of the initial indentation will change slightly as those extra spaces will be removed.

If you do this a lot (as it is several steps) it may be worth wrapping this in some type of function. But my vim foo is not that strong yet. The following assumes you have your tab stop set at 4 you may need to alter a couple of the searches if you use a different size.

I would do it in three steps.

 :retab 1
 :% s/^I/ /g
 :retab 4

This converts all you tabs to spaces first (without messing with the indentation). (by first resizing your tabs replacing them with space then setting the tab size back to its original size).

Now you can convert all the leading space on a line into tabs.

 :% g/^    /s/    /^I/g

The above searches for all lines that begin with four spaces (use the number of spaces you have in your tab stop size). And replaces from left to right each group of four spaces with a tab character.

So now all your lines have leading tabs and any extra space is on the end of the initial tab indented line. So now you can just remove any trailing white space as we expect this is unneeded.

 :% s/^\(^I*\) */\1/

This searches for lines that begin with zero or more tabs followed by zero or more spaces. It then replaces the match with the initial number of tabs you found. Thus effectively removing space from the indentation.

3

To delete white spaces after tabs, use the following substitution command:

:%s/^I \+/^I/g

where ^I is a special character which appears when you press Tab.

To delete all whitespaces between non-whitespace characters, use the command:

:%s/\(\S\) \+\(\S\)/\1\2/g

To double-check if all middle spaces are gone, use: /^I \+ command to find them. If there are some, vim would highlight them for you.

If you'd like to highlight them, check: How to strip trailing whitespaces in a file?

To convert tabs to spaces, while maintaining text alignment: How to replace tabs with spaces?

See also:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.