Hanfei Sun
Hanfei Sun

Reputation: 47061

How to replace a word instead of a string in emacs like this?

For example, I have the codes below:

(defun toggle-light ()
    "Toggle setting tab widths between 4 and 8"
    (setq a
      (if (boundp 'a) a nil))
    (interactive)
    (if a
    (progn
      (load-theme 'solarized-dark t)
      (setq a nil))
      (progn
    (load-theme 'solarized-light t)
    (setq a t) )))

And now I want to refactor this blocks by replacing the variable name a with is-lighted, but without changing other character a in other words (for example, in interactive or tab).

Is there a built-in function in emacs that can finsih that job?

Upvotes: 2

Views: 115

Answers (2)

Mirzhan Irkegulov
Mirzhan Irkegulov

Reputation: 18055

While phils's answer is correct and idiomatic, you should also learn about \b word boundary regular expression. Thus, equivalent regexp replace would be M-%\ba\b. See Backslash in Regular Expressions @ Emacs Manual and Regular Expression @ EmacsWiki.

Upvotes: 1

phils
phils

Reputation: 73256

C-uM-% a RET is-lighted RET

The prefix argument ("delimited") to the standard query-replace functions wraps the pattern with word-boundary markers.

Upvotes: 5

Related Questions