2

Plugins such as snipMate, emmet and many others allow me to quickly insert opening and closing html tags. For example by typing span and hitting the correct key (TAB for snipMate, <C-y>, for emmet) I can magically get <span>_</span> with my cursor positioned where I have placed the underscore. Now I type some content that should go inside the span. That leads to my question. How to efficiently leave the span and continue entering text?

For a block-level tag the best I have found is <Esc>o because I almost always want to start on a new line with the next block-level tag. But for a span or other inline tag, I'd like to skip the closing tag and continue typing. For example I might type span<C-y>, and get <span>_</span> followed by some text leaving me with <span>some text_</span> where the cursor is shown by the underscore again. I'm in input mode. How to efficiently get to after the </span> and still be in input mode?

I could use <Esc>vat<Esc>a which seems like a ton of keys for a simple action though it does allow climbing the hierarchy in a nice way.

I could <Esc>f>a which still seems heavy for something I use many times.

Is there some more efficient way that I am missing?

1
  • Even a macro will be three key presses because you need Esc first. If you really need it often, I suggest using a key binding.
    – Philippos
    Commented Mar 17, 2017 at 7:40

1 Answer 1

1

This answer was actually provided by the author himself. In his own words:

It seems like a common enough problem that someone should have addressed it.

But apparently, not yet. It is necessary to map a solution, and here are a some of the possibilities:

inoremap <C-f> <esc>f>a
  • inoremap - create a map that works in insert mode
  • <C-f> - bind the map to the keys CTRL+f
  • <esc> - leave insert mode
  • f> - look for the next occurrence of > and move the cursor there
  • a - append text after the cursor

Note: this map overwrites the original function of in insert mode. Take a look at :h i_CTRL-f just to be sure.

Another way would be:

inoremap jk <Esc>vat<Esc>a
  • inoremap - create a map that works in insert mode
  • <Esc> - leave insert mode
  • vat - visually select around the
  • <Esc> - leave visual mode
  • a - append text after the cursor

And yet another way could be:

inoremap <leader>zx <esc>%a
  • inoremap - create a map that works in insert mode
  • <leader>zx - bind the map to the keys +zx
  • <Esc> - leave insert mode
  • % - Find the next item in this line after or under the cursor and jump to its match.
  • a - append text after the cursor

Original (and incorrect) answer

You could use <esc>A (uppercase "A"), it will leave you in insert mode at the end of the line. Check :help A.

If you are dealing with HTML, it might be helpful to be aware of % which will (according to :help %):

Find the next item in this line after or under the cursor and jump to its match. |inclusive| motion.

Or basically jump to the opposite (closing or opening) tag.

10
  • As Gary knows a bunch of commands, he surely thought of A, but this doesn't help, if there is already text behind the closing tag.
    – Philippos
    Commented Mar 17, 2017 at 7:37
  • Yes, <esc>A is good if the place to continue is the end of the line. And yes, <C-O>% (with matchit enabled) will move to the end of the containing html tag. It is almost perfect except it leaves me in insert mode inside the closing tag rather than after it.
    – GaryBishop
    Commented Mar 17, 2017 at 10:38
  • @Philippos, I thought the same, but since he wrote "_ some more efficient way that I am missing?_" I thought it wouldn't hurt to mention. @GaryBishop, in this case what about doing what @Philippos said and bind something with both % and A? It could be an insert mode map with <ctrl>something?
    – lsrdg
    Commented Mar 17, 2017 at 12:04
  • @GaryBishop, I'm sure you could come up with this alone, but here's what I have just tried: considering the <span>some text_</span> situation, I tried your last suggestion and I am tempted to actually use it: inoremap <C-f> <esc>f>a. Though I'll read :h i_CTRL-f with more attention before deciding on the binding. At least, with capslock remapped to ctrl this seems pretty efficient. Anyway, thanks for bringing the topic since I have never thought about it before. (:
    – lsrdg
    Commented Mar 17, 2017 at 12:33
  • 1
    I found a useful discussion on reddit vim.
    – GaryBishop
    Commented Mar 17, 2017 at 19:18

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.