Delete whitespaces before selected lines

vim

Solution

I'd use visual line mode (Shift+V) to select the lines I wanted, then run the substitute command on them (hitting `:` should automatically include the visual mark `'<,'>` at the start for you):

`:'<,'>s/^\s*`

This is useful when you're working and haven't figured out the line numbers. In this case as you know it's lines 2 to 6, you can do:

`:2,6s/^\s*`

A helpful option for figuring out the line numbers quickly is `set number`.

The substitute command is greedingly grabbing all whitespace (`\s*`) from the start of each line (`^\s*`), and replacing it with nothing (equivalent to `/^\s*//`).

Problem

I have text like this: ``` This is a normal line. 6 spaces are before me. //line 1 4 spaces are before me. //line 2 3 spaces are before me. //line 3 6 spaces are before me. //line 4 4 spaces are before me. //line 5 Another normal line. 2 spaces are before me. But that is ok. //line 7 Line goes on. ``` How do I use vim to select and delete all the spaces before line 1 to line 5 ?

Original source