I want to replace a character inside a word, so I wrote:

(setq regexp "[ab]")
(and (re-search-backward regexp nil t 1)
     (replace-match "c"))

This replaces a or b to c as I wish, but I want to set a bound of it. There is a BOUND option with re-search-backward. How can I make the match only a word that is under the current cursor?

Thanks for @jackson I can write the function, but still it doesn't work properly.

(setq regexp ".*[ab].*")
(defun test-it ()
      (narrow-to-region (progn (backward-word) (point))
                        (progn (forward-word) (point)))
      (and (re-search-backward regexp)
           (replace-match "c")))))

Thins function replaces ab to ac and ad to cd as I expected, but it doesn't replace ac to cc. Why?

  • Out of curiosity, are you implement some sort of auto-correction here? – Kaushal Modi Nov 12 '15 at 19:21
  • I want make a pinyin (Chinese tone mark) input mode. like ma -> mǎ. So I want to replace aāáà to ǎ by a function. I can write it separatey by using ("a" "ǎ") ("ā" "ǎ") ("á" "ǎ") ("à" "ǎ"). But I thought it is a bit redundant. – ironsand Nov 12 '15 at 19:35
  • Yay, finally a chinese introduction lesson I took years ago helps me tell that this has something to do with mother and horse :) I don't understand or type in Chinese, but have you looked at set-input-method and toggle-input-method? – Kaushal Modi Nov 12 '15 at 20:01
  • Also, is this for an occcasional phonetics correction? If not, you can probably modify the solution here for fixing double caps to always "autocorrect" based on custom rules like ma* to , ma- to , ma` to , and so on. – Kaushal Modi Nov 12 '15 at 20:05
  • As far as I know these two method are intended someone who can type Chinese, right? I want to make a minor mode for the phonetics notation. – ironsand Nov 12 '15 at 20:08

Not quite sure what you're asking. If you're using re-search-backward because you want to include the beginning of the word that encompasses the point, perhaps this does what you want:

(setq regexp ".*[ab].*")
(defun test-it ()
      (narrow-to-region (progn (backward-word 1) (point))
                        (progn (forward-word 1) (point)))
      (backward-word 1)
      (if (looking-at regexp)
          (replace-match "frog")))))

The code is a bit clumsy, but will replace the regexp with the given string if it matches.

  • Thanks for your help! I changed your code a little because I want to search the string backward from the (point). But my code have still a problem, it would be very nice if you can take a glance at it. – ironsand Nov 12 '15 at 20:17
  • 1
    I changed the regexp to be .*[ab].* - you probably don't want the .* - I did because I was checking to see if the entire word would be replaced. You're just replacing a single character. – Trey Jackson Nov 12 '15 at 20:45

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.