5

I'm new to editing html with VIM. I'm using matchit, ultisnips and surround to help me along the way.

One area which I can't seem to accomplish easily is editing attributes.

I want to go from: (cursor is |)

<input type="submit|" name="some_name" value="" id="some_name">

to:

<input type="submit" id="submit_button">

What is the fastest way to do this? Right now, I'm doing a lot of 'f' based searches.

2
  • 2
    To delete an entire attribute I use dwxda". dw to delete the attribute name, x to delete the =, then da" to delete everything inside the quotes, and the quotes themselves. Commented Oct 24, 2012 at 21:44
  • 2
    To delete an entire attribute I use dW (this includes the name, the = and the quoted value). Unless it's the last attribute before the tag's closing >. In that case I'd use 2df" or @Will's solution. Commented Dec 28, 2017 at 11:23

6 Answers 6

10

I usually just use dW, which deletes the name, equals character and the value in parenthesis (including the parenthesis).

In some cases if I want to delete the rest of attributes to the end of the HTML tag I'm using dt/ (delete everything until you find / which is the end of input tag <input ... />)

Sign up to request clarification or add additional context in comments.

Comments

6

I was also looking for a solution for this, and using w and W wasn't working great for me when dealing with AngularJS where you can have attributes like this:

<input ng-show="product.inUse" ng-model="product.customerPrice" />

What I've started doing is moving my cursor to the whitespace before the attribute, for example here:

<input| ng-show="product.inUse" ng-model="product.customerPrice" />

Then press d2f" (y instead of d for copying), which gives me:

<input| ng-model="product.customerPrice" />

It will look for a " character two times and delete everything including the last one.

Comments

5

Possible solutions

wd2W

This method requires counting so maybe you would rather do wdW and just hit . until it is correct.

wd/ id<cr>

Moves to the next word so the start of name=".." the delete with d til / id. This solution can also delete across lines. You can use /... with other operators as well, e.g. c, y, and =.

Of course you can do the method you are using currently with f and just repeat with .

Comments

2

For your example, I would use: wdw.....wwci"submit_button

  • w to move at the end of the current w
  • dw to erase a "word"
  • then . to repeat the last command and erase as many word as you want.
  • ww to move quickly between the "
  • ci" to change content inside "
  • submit_button
  • ESC

See :help text-objects for more explanation on ciw. (please not there is a tag text object so you can things like cit to change content of a tag quickly.

You could probably use something like wd4w but you have to be sure beforehand that you want to erase 4 words.

Have you heard about Zencoding.vim ?

This is plugin designed to quickly write html or css.

I am not sure there are features to edit, but to write new codes it beats everything.

1 Comment

Thanks; I'll try Zencoding for html generation.
0

I would use ci> and retype the form. Lot of times my snippets have tons of attributes I don't need like <form action=""> so ci> blows it up and lets me start from scratch when I need to. I find it faster.

Comments

0

<input type="submit" name="some_name" value="" id="some_name">

You can also use dt>, it will delete until the character >

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.