Is there a way to configure emacs, so that when I hit M-backspace it would actually remove the whole variable? If I have a variable like my_dummy_variable I need to hit M-backspace three times in order to remove the name?

  • 4
    Maybe the answers to this question can help.
    – Nsukami _
    Feb 27 '16 at 18:16
  • this is really related to emacs not C and not C++, suggest remove c tag, Feb 28 '16 at 1:02

You can use the built-in backward-kill-sexp.

(global-set-key (kbd "M-DEL") 'backward-kill-sexp)

This has the unwanted effect of adding whatever was deleted to the kill ring.

To fix this you can try:

(defun backward-delete-sexp (arg)
     (interactive "P")
     (delete-region (point)
            (progn (backward-sexp arg) (point))))
  • actually, you probably want <M-backspace>, not <M-DEL>
    – erjoalgo
    Feb 28 '16 at 21:45
  • (let ((kill-ring)) (backward-kill-sexp arg)) may be a cleaner solution.
    – PythonNut
    Feb 28 '16 at 22:43

I think you're looking for superword-mode. From its documentation:

Superword mode is a buffer-local minor mode. Enabling it remaps word-based editing commands to superword-based commands that treat symbols as words, e.g. "this_is_a_symbol".

The superword oriented commands activated in this minor mode recognize symbols as superwords to move between superwords and to edit them as words.

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.