Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Vim: Best way to remove space between words

I'm an experienced developer on the Windows platform and I'm trying to teach myself how to use Vim. I am pretty good with regular expressions and understand the principles of how to use Vim. However, I have a specific problem, and although I have a solution, it feels as though there ought to be a better one.

I have a file which contains a line similar to the following:

CODE<tab><tab>Lorem ipsum dolor sit amet

There could be a variable number of <tab> or <space> characters between CODE and Lorem. Assuming the cursor is over the 'C' of CODE in normal mode, what I want to be able to do is find a key combination that will produce the following output, and leave the cursor between the 'E' of CODE and he 'L' of Lorem in insert mode.

CODELorem ipsum dolor sit amet

My curent solution is to use the following key sequence:

w d ? \ s \ + <return>

This works, but it feels illogical to go past the thing I want to delete before I can delete it. I feel like I should move to the end of CODE and delete forwards. I realise this could simply be a Vim idiom that I'm not aware of. I could also be totally missing a key piece of Vim knowledge.

What's the best way to achieve my goal?

like image 652
Damian Powell Avatar asked Feb 06 '11 23:02

Damian Powell


2 Answers

eldw will do it.

  • e - move forward to the end of the word
  • l - move 1 character to the right
  • dw - delete all the blanks

If your cursor is in the middle of some whitespace, diw will delete whitespace left and right of the cursor. (If it is somewhere in the middle of a word, it will delete the word.)

like image 100
Matt Curtis Avatar answered Nov 05 '22 14:11

Matt Curtis


Some alternatives:

:s/\s\+//
e xx
elxx
eldtL
f<tab>xx
eldw
like image 24
DigitalRoss Avatar answered Nov 05 '22 16:11

DigitalRoss