11

I merged a python file using vimdiff, and the result has something like this:

def code(): line 1 line 2 line 3 line 4 ... line n 

In this state, the indentation is inconsistent. How could I remove all the spaces in front of the lines? I tried in Visual mode, but it's selecting the whole line. Does vim have a multiline edit? Notepad++ allows this with Alt-Shift.

4 Answers 4

12

Ctrl-V to select blocks.

Or, to remove all white-space at the start of all lines in the file:

:%s/^\s\+ 
Sign up to request clarification or add additional context in comments.

3 Comments

If you use Ctrl-V then j to select the column to which you want to align your code, hit < to shift the code that is behind the column.
@Benoit : Great trick! I often increase/decrease indentation in the middle of lines : until now I was yanking/pasting spaces !
What would be the solution if you didn't want to replace white spaces on ALL lines, but on a selection of lines which are not adjacent (e.g. lines 1, 3 and 7)?
8

There are at least two solutions to your problem :

  1. There is a blockwise select mode than you enter by using Ctrlv. You can select the spaces at the beginning of your lines and hit d.

  2. Alternatively, you can select linewise the line you want to modify by entering visual mode with SHiftv (then use down arrow or j to select). And then use < to remove one indentation level. Use . to repeat the last operation if you want to remove multiple indentation.

Comments

4

ShiftV will put you into visual line mode, and then you can press : to enter a command to execute on only the selected lines.

Comments

4

With VISUAL mode you only have to select all the lines you want to re-indent and hit =.

I've never used Notepad++ but I think that what you call multiline edit is achievable with what Vim calls VISUAL BLOCK : instead of V hit <C-v>. You are then able to select rectangular portions of text. Once you have your column, hit I or A to enter EDIT mode then <Esc> to apply your edit to all the lines. Since you want to remove a single space you have to visually select the column of spaces and hit x or d.

You can also do it with a simple search/replace, I guess. Or use :norm.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.