Context
Editing an arbitrary XML file in nxml-mode
.
Need
Let's say I have a structure like this:
<a>
<b>...</b>
</a>
Let's say the cursor is at the <a>
.
I'm looking for one shortcut that brings the cursor directly from <a>
to </a>
, and back if called again.
Search before you ask
This question requests one-way jump and gets an answer: C-M-n
to jump forward, C-u -1 C-M-n
to jump backwards -- Emacs, nxhtml how to highlight or jump to the closing html tag? - Stack Overflow.
But this is inconvenient because to jump back one has to:
- position the cursor exactly at end of closing tag, and
- use a long and convoluted key shortcut
C-u -1 C-M-n
.
There's room for more convenience: one shortcut to do both directions.
How to do that?
If this does not already exists, I'd like to it. I'm a seasoned developer in a number of languages and environments but not proficient enough in emacs-lisp to make my own solution. Still, it might be easy enough to do for an emacs-lisp wizard.
Technically, one function with this behavior would do the trick:
- when cursor is on an opening tag, jump forwards (call
(nxml-forward-element 1)
) - when cursor is on a closing tag, jump backwards (call
(nxml-forward-element -1)
) - bonus points if, when cursor is anywhere between
<
and>
(not only just before<
asnxml-forward-element
requires), the function still works - bonus points if, when cursor is anywhere inside text or whatever, then jump backwards to opening tag.
</foo><bar>
jump down to</bar>
. If inside</foo>
, jump up to<foo>
. Same key shortcut.