14

Say I have the following code:

<p>
    Hello
</p>

And I want to make it

<p>Hello</p>

I would like to put the cursor in normal mode at the end of line 1, so on the ' > ' and have a command to delete all spaces until the next character. The closest I can think of is the motion

d/Hello

which deletes everything until Hello but the issue is that it deletes also the character under the cursor (the ' > ') so I end up with

<pHello
</p>

How would you do that?

1

3 Answers 3

19

One way when you won't need to repeat this action many times.

JxJx

Explanation:

J           # Join current line with next one but substitute end of line with a space.
x           # Remove the space.
Jx          # Repeat same process for last line.
1
  • I need to do it many times and try to repeat the action via ., yet that only repeats the x not the J. Can you please add an example if one needs to repeat this multiple times?
    – k0pernikus
    Commented Nov 23, 2016 at 10:46
6

There's a tag text-object in vim:

  • put cursor within tag, press vat to select entire tag
  • press :, it becomes :'<,'>
  • type j, it becomes :'<,'>j
  • press Enter to join lines

:help v_at

at          "a tag block", select [count] tag blocks, from the
            [count]'th unmatched "<aaa>" backwards to the matching
            "</aaa>", including the "<aaa>" and "</aaa>".
            See |tag-blocks| about the details.
            When used in Visual mode it is made characterwise.
2
  • 2
    vatJ would get you the same results.
    – Kambus
    Commented Jan 13, 2012 at 14:08
  • 3
    This doesn't actually answer the question because of the spaces added by :join. Commented Jan 13, 2012 at 14:28
2

When standing anywhere in the second line (the one that says Hello), press the following keys: ^d0vatgJ. Simply explained:

  1. ^ will go to the first non-whitespace character, H
  2. d0 will delete to the beginning of the line
  3. vat will select the entire tag
  4. gJ will join all the lines without inserting spaces

If you start on the H, you can skip the ^ part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.