Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: how to delete spaces till the first character?

Tags:

vim

I have a line like this:

      foobar

as you can see there are many spaces between the first column and 'foobar'. If I have the cursor in the first column, is there any key binding to remove the spaces till 'foobar'?

like image 930
ziiweb Avatar asked May 05 '12 12:05

ziiweb


People also ask

How to delete first 2 spaces for each line using 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. Show activity on this post. Then press d to delete the selected area.

How do I run Vim on all lines of text?

Another option is to use the :norm-command of vim. : enters command mode, norm specifies a command that will be executed on all lines, ^ goes to the beginning of the line, and x deletes the first character. Thanks for contributing an answer to Unix & Linux Stack Exchange! Please be sure to answer the question.

Is there a shortcut key to delete words in VI?

I understand that vi has shortcut keys to delete characters, words and lines with various options. For example, I might type du" expecting the editor to "delete until the next " character is found" The closest I know is d9w where 9 is the number of words to delete. Does anyone know if this is possible? Show activity on this post.

How do I change the Order of the selected lines in Vim?

Method that will work only in vim: Go to the first line that you want to manipulate. Type Vor Ctrl+Vto go into visual selection mode (mark the beginning of the range). Move to the end of the range. Type 9<.   This will shift the selected lines left by nine shiftwidths.


3 Answers

dw

Will delete all the whitespace if you're on the first column.

like image 193
Mat Avatar answered Oct 09 '22 00:10

Mat


dtf

Delete Till F

There is no other easy command. You could use a regex, but that'd be more painful to write.

My bad, @Mat's answer is even better (dw).

like image 20
Florian Margaine Avatar answered Oct 09 '22 00:10

Florian Margaine


The simple answer is

dw

...which performs [d]elete [w]ord. If you want to remove all whitespace in the whole document before all occurences of "foobar":

:%s/^\s*foobar/foobar/
like image 4
Jeff Daly Avatar answered Oct 08 '22 23:10

Jeff Daly