emacs - search and replace

Search in current buffer

emacs isearch (incremental).

Ctrl+s

While in isearch:

Command Description
Ctrl+s Jump to next occurrence
Ctrl+r Jump to previous occurrence.
Ctrl+g/Enter Exit and place cursor at original position.

Find the word under the cursor

Put the cursor at the beginning of the word you want to search for:

isearch-forward-symbol-at-point

Ctrl-s Ctrl-w

But, this is not strictly symbol search - but “word part at the right of point” search.

Instead, use Meta s . (period)

https://www.gnu.org/software/emacs/manual/html_node/emacs/Symbol-Search.html

This starts symbol search with the symbol at point preloaded. Symbol search is a special mode of isearch that you can start with M-s _. If you’re already in isearch (for example, after pressing C-s or C-r) you can also type M-s _ to enter symbol search.

Its good to read the entire documentation of isearch since it’s very powerful and versatile.

Use smartscan package

smartscan is a good alternative for default isearch.

Using fzf.el

Command Description
M-x fzf-find-in-buffer

Search in the current repository

Using fzf.el

Command Description
M-x fzf

Using Projectile

Command Description
C-c p s r projectile-ripgrep
C-c p s g projectile-grep
C-c p s s projectile-ag
C-c p s x projectile-find-references This seems to be searching only in org files in static website repositories. So, it is helpful.

Using Consult and Ripgrep

Command Description
M-x consult-ripgrep this is case insensitive

How to export these results into a buffer

M-x embark-act embark-export

We can navigate to the original files from this list. Navigating from embark-export list seems a little easier than navigating from projectile-rg buffer.

Replace words in a buffer

How to not replace all occurances of a word? Do not use replace-string. Use query-replace and query-replace-regex instead. And use the ? operator. It is very helpful.

emacs - Rename something in an entire project

Occur mode

  1. https://www.emacswiki.org/emacs/OccurMode
  2. https://www.masteringemacs.org/article/searching-buffers-occur-mode

Occur mode is the right tool for the job when you want to show all lines matching a certain regular expression. And if that’s not enough, you can even edit the matching lines and commit the changes to the original buffer.

Standard Emacs command ‘occur’ lists all lines of the current buffer that match a regexp that you give it. The matching lines are listed in buffer ‘*Occur*’, and you can click them there to navigate to the corresponding lines in the original buffer. That is, buffer ‘*Occur*’ acts as a hypertext index to your buffer.

Notes

  1. C-s (incremental search forward - from the point)
  2. C-r (incremental search backward - reverse of C-s)
  3. M-e (edit current search)
  4. C-s C-s (repeat last successful search)
  5. C-M-s (regexp search)
  6. M-x occur
    1. type your regex for search
    2. it will launch an occur buffer
    3. find all occurances in the current buffer
    4. hitting return on an occurance in the occur buffer will take you to that line in the main buffer.
    5. you can navigate through the results using C-n, C-p, M-n, M-p
  7. How to feed the results of isearch to occur buffer? This should send the results of isearch into the occur buffer.
    1. Option 1
      1. Launch isearch in a buffer (use M-x isearch)
      2. When isearch shows the results that you are looking for, launch occur from within isearch by doing M-s o
    2. Option 2
      1. Use the function isearch-occur

Case sensitivity

How to do case insensitive search?

You can toggle case sensitivity in isearch using this function: isearch-toggle-case-fold