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
Navigating to the locations in files
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
- https://www.emacswiki.org/emacs/OccurMode
- 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
- C-s (incremental search forward - from the point)
- C-r (incremental search backward - reverse of C-s)
- M-e (edit current search)
- C-s C-s (repeat last successful search)
- C-M-s (regexp search)
- M-x occur
- type your regex for search
- it will launch an
occur
buffer - find all occurances in the current buffer
- hitting return on an occurance in the occur buffer will take you to that line in the main buffer.
- you can navigate through the results using C-n, C-p, M-n, M-p
- How to feed the results of
isearch
tooccur
buffer? This should send the results ofisearch
into theoccur
buffer.- Option 1
- Launch isearch in a buffer (use M-x isearch)
- When isearch shows the results that you are looking for, launch
occur
from withinisearch
by doingM-s o
- Option 2
- Use the function
isearch-occur
- Use the function
- Option 1
Case sensitivity
How to do case insensitive search?
You can toggle case sensitivity in isearch using this function: isearch-toggle-case-fold