Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: delete the first 2 spaces for multiple lines

Tags:

vim

What's the easiest way to delete the first 2 spaces for each line using VIM? Basically it's repeating "2x" for each line.

Clarification: here the assumption is the first 2 characters are spaces. So the question is about doing indentation for multiple lines together.

like image 976
twimo Avatar asked Aug 05 '11 19:08

twimo


People also ask

How do you backspace multiple lines in Vim?

Enter visual mode ( ctrl + v) jj to select the lines. shift + i to go into insert. Delete 2 spaces ( with backspace or delete)

How do I remove leading spaces in Vim?

Assuming you still have lines with leading spaces, type . to repeat the shift command. Type . repeatedly until all the spaces are gone. If you want to remove the leading spaces from all lines to the end of the buffer, use <G and then .

How do I delete all lines starting with Vi?

To remove all lines containing a particular string in the vi or Vim text editors, you can use the g command to globally search for the specified string and then, by putting a "d" at the end of the command line, specify that you want all lines containing the specified string deleted.


7 Answers

  1. Enter visual block mode with Ctrl-V (or Ctrl-Q if you use Ctrl-V for paste);
  2. Select the area to delete with the arrows;
  3. Then press d to delete the selected area.
  4. Press Esc
like image 155
Tom Kerr Avatar answered Oct 08 '22 18:10

Tom Kerr


Some more options. You can decided which is the "easiest way".

Remove the first 2 characters of every line:

:%normal 2x

Remove first 2 characters of every line, only if they're spaces:

:%s/^  /

Note that the last slash is optional, and is only here so that you can see the two spaces. Without the slash, it's only 7 characters, including the :.

Move indentation to left for every line:

:%normal <<
like image 26
bhinesley Avatar answered Oct 08 '22 16:10

bhinesley


You could also use a search and replace (in the ex editor, accessed via the : character):

Remove first two characters no matter what:

%s/^.\{2}//

Remove first two white space characters (must be at the beginning and both must be whitespace... any line not matching that criteria will be skipped):

%s/^\s\{2}//
like image 44
Jason Down Avatar answered Oct 08 '22 17:10

Jason Down


Assuming a shiftwidth=2, then using shift with a range of %

:%<
like image 45
Peter Rincker Avatar answered Oct 08 '22 18:10

Peter Rincker


Two spaces, or two characters? (2x does the latter.)

:[range]s/^  //

deletes two blanks at the beginning of each line; use % (equivalent to 1,$) as [range] do to this for the entire file.

:[range]s/^..//

deletes the first two characters of each line, whatever they are. (Note that it deletes two characters, not necessarily two columns; a tab character counts as one character).

If what you're really doing is changing indentation, you can use the < command to decrease it, or the > command to increase it. Set shiftwidth to control how far it shifts, e.g.

:set shiftwidth=2
like image 41
Keith Thompson Avatar answered Oct 08 '22 16:10

Keith Thompson


I'd try one of two approaches:

  1. Do column editing on the block to delete using Ctrl+V (often mapped to Ctrl+Q).
  2. Record a macro on the first row using q1 (or any other number/letter you want to denote the recording register), then replay that macro multiple times using @1 (to use my previous example. Even better, use a preceding number to tell it how many times to run - 10@1 to run that macro 10 times, for example. It does, however, depends on what you recorded - make sure to rewind the cursor 0 or drop one line j, if that's relevant.
like image 37
sa125 Avatar answered Oct 08 '22 16:10

sa125


I'd also add: learn how to configure indentation for vim. Then a simple gg=G will do the trick.

like image 41
Luc Hermitte Avatar answered Oct 08 '22 16:10

Luc Hermitte