3

I might have some text like the following:

<foo>something very important</foo>
1    2 ^                    3     4

If the cursor is inside the tag, for example on the m character, what is the simplest way to jump to the beginning of the tag (1)?

It seems like this action should be simple, but I can't figure it out. I know that I can select in or around the tag and then jump to the beginning of end of the visual selection (e.g. vito or vatoo) but it feels like I should be able jump the cursor around without going into visual mode first.

I've looked at the targets.vim plugin, but I don't think it offers a solution in this case, though I might be wrong as I find the documentation difficult to understand. Is there a way to use the f F t T commands on tags and objects rather than literal characters?

4

As suggested in an answer by @Rich to a related question, you can define a new operator to go to the beginning (or end) of the text object.

In your case:

function! GoStart(type) abort
  normal! `[
endfunction
nnoremap <silent> gs :set opfunc=GoStart<CR>g@

And then you can use gsat to move the cursor to the beginning of the tag, (1) in your diagram.

The command works with a count on the text object itself, so gs2at will work (but 2gsat will not.) See the aforementioned answer for another limitation, the undesirable side effect of making . repeat that motion (overwriting the previous operation stored for repeating with .). There's also a somewhat related ninja-feet plug-in which defines similar motions (also mentioned in other answers to that other question.) That plug-in by default only defines mappings to start insert mode at the beginning (or end) of the text object, not to just jump there, but it shouldn't be too hard to use the plug-in internal mappings to map keys to those actions either.

1
  • 1
    wow. I'll have to test this out, I think there a couple of things here that are new to me. thanks!
    – john
    Jun 9 at 18:42
0

@filbranden's answer is helpful and shows how to build the solution. It demonstrates how the result would need 4 key strokes to be able to get to any of positions 1 - 4. 4 key strokes seems like a minimum with this approach.

I'd prefer not to reinvent tools, and it appears to me that if t T f and F could wrap around lines then the problem would be solved in 3 key strokes - F<a F>i etc. It would also be a bit more general than just tags.

vim-sneak offers this functionality.

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.