I have some text like the following in a file:

  sample text
    some random text
            even more random text
text with no indent
 worst indention

I need to delete the empty space before each of the lines. For one line what I do is 0dw

I can repeat the same command using . but by manually navigating to the next lines.

But is there a way to apply '0dw' to those block of lines?

I suppose there is a way using macros, but I haven't used them before. But I am willing to learn them if ther is no other choice.


4 Answers 4

" Same thing (:le = :left = left-align given range):

Learn more here at http://vim.wikia.com/wiki/Remove_unwanted_spaces

If you want to do this for a particular range of lines:


BTW, best way to start learning vim is to execute vimtutor command, it will teach you how to use Vim in Vim editor.

  • 1
    :le was awesome and thanks a lot for the alternatives and the link :)
    – saiy2k
    Jan 20, 2012 at 8:18
  • 3
    +1 for vimtutor mention as well as a good answer. Jan 20, 2012 at 8:20
  • 1
    Select the relevant lines by using V and moving the cursor around to expand the line selection. Then press : to automatically enter '<,'>, and from there on you can use Nikhil's regexp.
    – UncleZeiv
    Jan 20, 2012 at 10:21
  • 1
    another way of doing a range of lines (since I always forget the line numbers) is to mark the 2 lines and operate on the marks. Example, go to line 19 and press 'ma', go to line 25 and press 'mb', then do :'a,'bs/^\s\+//.
    – phemmer
    Jan 21, 2012 at 20:47

An alternate way to do this is to use VIM's indentation tools (usually bound to < / >). First you need some way to indicate which lines will be affected. You can use the visual selection mode or any other means of specifying a range of lines to be affected, then use an arbitrarily high number or indentation levels to remove. 9< usually does the trick quite nicely to remove at least 9 levels of possible indentation (tabs/spaces) from the current line(s).


I have got approach to do this in the simple following steps:

1. press `v` to go to visual selection mode
2. select the lines you want to affect
3. :'<,'>normal 0dw

Explanation of the third step:

0 navigates to start of line.

dw deletes the word (empty spaces).

normal applies the command 0dw.

'<,'> repeats the normal for all selected lines in visual mode.


ctrl+v allows you to do something similar to what you want but not exactly. You can select a visual block and apply one command to the lot.

  • 2
    i selected those lines in block mode and this command worked: :'<,'>normal 0dw
    – saiy2k
    Jan 20, 2012 at 8:16
  • 1
    @saiy2k :normal is the important thing here, and I think it's the best answer here. Could you post it as an answer? Jan 20, 2012 at 23:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .