In nxml-mode, I would like to insert an XML tag that has the same name as the nearest closing tag before point, without having to retype (or complete) the same tag name each time I do it.

1 Answer 1


This requires the expand-region package.

(defun repeat-element ()
  "start a new XML element with the same name as the previous XML element"
  (er/expand-region 1)
  (replace-regexp "</\\([^>]+\\)>" "\\&
<\\1" nil (region-beginning) (region-end) nil)
  (funcall indent-line-function))

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.