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'?
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.
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.
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.
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.
dw
Will delete all the whitespace if you're on the first column.
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
).
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With