Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete contents of an HTML tag in Emacs

Vim has an awesome feature that allows users to delete contents of tags, within quotation marks, etc. For example, in the following situation:

<h1>  Cursor is here -> █ <- :)  </h1>

one can type d i t (“delete in tag”) to delete the contents of the <h1> HTML tag.

There are also other shortcuts, for example:

  • d i ( to delete contents in parentheses ()
  • d i " to delete contents in double quotes "".
  • d i ' to delete contents in single quotes ''.

Does anything like this exist for Emacs?

I'm aware of zap-to-char and nXhtml's sgml-delete-tag, but they don't quite do what I want.

like image 366
a paid nerd Avatar asked Jan 24 '11 18:01

a paid nerd


1 Answers

How's this code work for you?

(defun sgml-delete-tagged-text ()
  "delete text between the tags that contain the current point"
  (interactive)
  (let ((b (point)))
    (sgml-skip-tag-backward 1)
    (when (not (eq b (point)))
      ;; moved somewhere, should be at front of a tag now
      (save-excursion 
        (forward-sexp 1)
        (setq b (point)))
      (sgml-skip-tag-forward 1)
      (backward-sexp 1)
      (delete-region b (point)))))
like image 199
Trey Jackson Avatar answered Oct 13 '22 00:10

Trey Jackson