3 Ways to Replace string in Emacs

Replace is a common editing task and in Emacs you have countless ways to do it for different kind of replace. This post introduce you three useful tips to replace strings.

1. query-replace

The first one is the query-replace. But I rarely use it because it not better than the regular editors. It's an ask and answer process, each time the command buffer prompt you to input something and go to next step. For more complex replacing, it can't help much.

2. multiple cursors

Here the multiple cursors comes in. It can handle special cases like replace all occurrences of a variable name used in a function. See how to install and use multiple-cursors Multiple Cursor editing in Emacs.

You can select the variable and then execute M-x mc/mark-all-like-this and then input the replacement string, all the occurrences will change at the same time.

Or you can select the function body and execute M-x mc/mark-all-in-region, so it won't affect the same name in other functions.

3. evil mode

If you like the VIM way to replace string, you can use the evil mode.

The syntax is the same as VIM, but it will highlight each replacement so you can see what will be replaced before actually replacement.