Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
+1 nice question, I end up doing a lot of the same thing. And welcome to the site. –  Sardathrion Jan 20 '12 at 8:01
add comment

4 Answers 4

up vote 8 down vote accepted
:%s/^\s\+
" Same thing (:le = :left = left-align given range):
:%le

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

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

:19,25s/^\s\+//

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

share|improve this answer
1  
:le was awesome and thanks a lot for the alternatives and the link :) –  saiy2k Jan 20 '12 at 8:18
3  
+1 for vimtutor mention as well as a good answer. –  Sardathrion Jan 20 '12 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 '12 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\+//. –  Patrick Jan 21 '12 at 20:47
add comment

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.

share|improve this answer
2  
i selected those lines in block mode and this command worked: :'<,'>normal 0dw –  saiy2k Jan 20 '12 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? –  Gilles Jan 20 '12 at 23:24
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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