24

How do I replace current word under cursor in Emacs?

I know that I can use query-replace or replace-string but every time I do so I have to type entire string to be replaced, this is just annoying.

Vi has equivalent command cword and I can use a shortcut to pull the word under cursor for replacement:

nmap <leader>z :%s#\<<C-r>=expand("<cword>")<CR>\>#

Anybody know how to do it with Emacs?

2

Use copy-paste: M-d C-_ M-x replace-string RET C-y (kill-word, undo, replace-string, yank). If you've already started the replacement, you can switch back to the original buffer to do the copy-paste with the usual window or buffer switching commands: M-x replace-string RET … C-x o M-d C-_ C-x o C-y. You can use a prefix argument on M-d (e.g. ESC 4 2 M-d) to replace multiple words.

If you're planning to replace one or more words at the cursor, you can instead start from incremental search (C-s), use C-w to start searching the word under the cursor, then press M-% to switch to replace-string.

25

Incremental search has this feature, but the replace functions don't. Fortunately, incremental search does have a way to switch to replace mode once you've selected a search term. So:

  • Press C-s to switch to incremental search mode
  • Press C-w to yank the current word into the search buffer You can keep pressing it to append multiple words, and you can also use C-M-y to yank individual characters and C-y to yank whole lines
  • Press M-% to switch to replace mode using the search buffer you've already constructed

As you probably know from using M-% normally, this is a query replace mode where it prompts you for what to do with each match. If you just want to replace them all, hit ! on the first match

8
;; query-replace current word
(defun qrc (replace-str)
   (interactive "sDo query-replace current word with: ")
   (forward-word)
   (let ((end (point)))
      (backward-word)
      (kill-ring-save (point) end)
      (query-replace (current-kill 0) replace-str) ))
  • 1
    This solution works nicely and is much more useful than being told how to do it manually. I'm way too lazy to be bothered with copy-and-paste. – Paul Coccoli Jun 29 '15 at 17:09
2

I'm absolutely new to emacs but I can suggest the following:

M-b       to move backward over a word
M-d       to kill up to the end of the word

Now you can write a new word. Works almost like cw in vim

1

If you go into search-and-replace with M-S-% just pressing M-n fills in word under cursor, pressing it again wraps it in "symbol delimiter".

1

I was struggling and wanting the same thing since move from Vim to Emacs.

After some research and experimenting, I came up the function below which allow you to do the same thing with Evil package installed or using spacemacs.

It also works with region active (or visual selection in vim term) and that can be quite useful to replace word like abc-def-hij:

; replace current word or selection using vim style for evil mode
(defun evil-replace-word-selection()
(interactive)
(if (use-region-p)
    (let (
            (selection (buffer-substring-no-properties (region-beginning) (region-end))))
        (if (= (length selection) 0)
        (message "empty string")
        (evil-ex (concat "'<,'>s/" selection "/"))
        ))
    (evil-ex (concat "%s/" (thing-at-point 'word) "/"))))

then in your init.el, define the same key binding:

(global-set-key (kbd "\C-co") 'evil-replace-word-selection)

keybinding is slightly different from vim one which is "space z" but I haven't figure out how to map that in emacs when evil mode is there. keep complaining not a prefix etc and I'm relatively new to emacs. But still not too bad and myself is happy with this solution at the moment.

0

This has been answered already and I can't comment because I don't have enough reputation yet. But what you're looking for is ciw in vim.

In emacs the closest I've come to is this combination:

M-b M-d

This goes back to the beginning of the word the cursor is on and then deletes the word.

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.