4

I seem to be using a hack approach to getting to the start of an html tag. For example:

enter image description here

To get to my cursor to be on the t after the tag, I end up doing yit -- this yanks the component (which I don't care about). Is there a way to just do something like "it" to get to that t without doing anything (also doing ft gets me stuck at the t before so that becomes a hassle). And then, what about going to the end of the text, that is the 't' right before the </td>?

3

Although vim provides text objects for tags, it doesn't have motions for tags. One options is to reuse it to create a pair of maps [t and ]t somewhat analogous to [b:

nnoremap [t vit<esc>`<
nnoremap ]t vit<esc>`>

These both work by temporarily visually selecting the inner tag and then moving to the start and end of the visual selection.

| improve this answer | |
2

You can use the optional plugin :h matchit , it's an extension for the built in :h % .

Add this to your vimrc and restart vim.

packadd! matchit

Now you can jump between <td> and <\td> by %, note you must not place your cursor at < or > for this to work, otherwise it jump between < and >

If you are in the body and you want to jump to the open or close tag, use [% or ]%.

If you want to go to start of the inner body, use [%%l, [% jump to < of <td>, % jump to >. use ]%%h for the other direction.

There is also Mass's vim-matchup , it provides highlight, more motions, and textobjects.

| improve this answer | |
  • The question is actually how to move to the start of the contents inside the tags... OP mentions using yit as having that side effect (leaving you at the beginning of the yanked area), but how to do it without having to yank it into a register? – filbranden Apr 29 at 3:30
  • 1
    @filbranden oops, updated. – dedowsdi Apr 29 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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