lukmac
lukmac

Reputation: 4821

Delete whitespaces before selected lines

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 ?

Upvotes: 3

Views: 229

Answers (3)

Thor
Thor

Reputation: 47089

If there is no indention defined for this file, you could use ={motion}. If the cursor is on the first line, do =G to indent to the end of the file or =} to indent to next empty line.

To do it for this and the following 4 lines use =4j.

Upvotes: 1

hochl
hochl

Reputation: 12920

Another quick method: You can also use the << command. Go to line 1 and enter 5<< to shift lines 1-5 to the left. Repeat with the . command until all whitespace has disappeared. This is useful for increasing indent as well by using >>.

Upvotes: 2

actionshrimp
actionshrimp

Reputation: 5229

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*//).

Upvotes: 7

Related Questions