Remove all arbitary spaces before a line in Vim

VimIndentation

Vim Problem Overview


I'v written a plugin where it comes to parsing a XML tag. The content inside the tag is indented and when i copy the parsed string into the file it's gettting like:

    Example line
        This is part of the parsed line
        Thats goes one
    End of line

What I want is to remove all spaces in front of these lines, the final text should be

Example line
This is part of the parsed line
Thats goes one
End of line

I've tried to use = but it doesn't work the way I want. How can I do that with minimal key strokes ?

Vim Solutions


Solution 1 - Vim

To format a line to the left I use :left. Use this format an entire file:

:%le

Solution 2 - Vim

A simple search/replace s/^\s*// should do the trick, but it's probably not the minimal version.

Solution 3 - Vim

Personally I would visually select the lines with V, then use 99< to push the text as far left as it could go.

Solution 4 - Vim

Just type d followed by w followed by j at the beginning of each line.

Solution 5 - Vim

How about this:

:%s/^ *//

Or are you looking for a vim-script solution?

Solution 6 - Vim

To remove initial spaces and tabs at specified line numbers (E.g. from lines 5 to 10),

:5,10s/^\s*//

Solution 7 - Vim

Yet another way to achieve this is using the the normal command :h :normal-range

:%norm d^

This goes to column 0 in each line (%) and deletes (d) to the first non-white character(^).

This is slightly more to type as the accepted answer, but allows for easy extension if you have a more complex scenario in mind, such as additional un-commenting or so:

:%norm d^I# 

Resulting in:

#Example line
#This is part of the parsed line
#Thats goes one
#End of line

Solution 8 - Vim

The search/replace suggested by Lukáš Lalinský or the %le approach in the wikia page is probably the way I'd do it, but as another alternative you could also do:

:%< 99

As a quick way to shift the whole file (%) 99 times to the left.

Solution 9 - Vim

Remove all consecutive spaces: :%s/ */ /g

It was useful to me to go from:

$screen-xs-min:              480px;
$screen-sm-min:              768px;
$screen-md-min:                992px;
$screen-lg-min:                  1200px;

To:

$screen-xs-min: 480px;       
$screen-sm-min: 768px;       
$screen-md-min: 992px;           
$screen-lg-min: 1200px;                                                                                                 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFatih ArslanView Question on Stackoverflow
Solution 1 - VimPeter RinckerView Answer on Stackoverflow
Solution 2 - VimLukáš LalinskýView Answer on Stackoverflow
Solution 3 - VimRandy MorrisView Answer on Stackoverflow
Solution 4 - VimSimonView Answer on Stackoverflow
Solution 5 - VimchrisView Answer on Stackoverflow
Solution 6 - VimMayurKubavatView Answer on Stackoverflow
Solution 7 - VimSebastian MüllerView Answer on Stackoverflow
Solution 8 - VimDrAlView Answer on Stackoverflow
Solution 9 - VimDorianView Answer on Stackoverflow