# Tag Info

28

@Malabarba mentioned the use of wgrep package for editing grep/ack/ag results. I would like to write a detailed walk-through of how I use the ag package and wgrep-ag packages to achieve editing of 'ag'ged results using multiple-cursors package. These packages are available through Melpa. You also need to have ag aka the_silver_searcher installed on your ...

14

What I would like to do is to either quickly build a dired buffer from existing grep output [...], or transform grep output into a list of buffers for multi-occur [...]. You don't need to convert the grep results buffer into anything, there's already a mode specifically for editing grep output (and reflecting the changes on the files, of course). Install ...

10

Occur has a hook, occur-hook, that contains a list of functions to run after a match is found. So we can add a hook to jump to the *Occur* window there: (add-hook 'occur-hook '(lambda () (switch-to-buffer-other-window "*Occur*")))

9

I don't see the behavior that you describe, at all. For me, when I choose an occurrence in buffer *Occur* it visits the occurrence in the source buffer, putting that smack in the middle of the source-buffer window (vertically). But if you see what you describe, then just put some window-recentering code on occur-mode-find-occurrence-hook. Something like ...

7

There are a number of Emacs commands for which M-n at the input prompt (next-history-element) will insert a reasonable default if there is no next history element available. For occur that happens to be the symbol at point. So this might do what you want: M-s o M-n (You might have to hit M-n twice if you have a recent occur search provided as the first '...

7

If you only care about a particular order (e.g. Buy comes first) then you can use a regexp: Buy.*0.00000057, as mentioned by @zck. But if you need to check for both orders then a single regexp won't do the job.\ To match multiple things in any order, here are two approaches. Use occur, refining its output with multiple patterns. Use M-x occur Buy to get ...

6

Using helm-swoop Solution Use helm-swoop to find occurrences of words WORD1 and WORD2 in the buffer. M-x helm-swoop RET WORD1\|WORD2 RET Old answer Use helm-swoop to find occurrences of pairs of words WORD1 and WORD2 in the buffer, in the same line. M-x helm-swoop RET WORD1 WORD2 RET

5

If you know that BUY always comes before 0.00000057, you can give occur the regex BUY.*0.00000057. That will search for all lines that have BUY, any number of characters, then 0.00000057 in it. Alternate package If you use helm swoop, not only can you specify them in either order (swooping for BUY 0.00000057 is the same as 0.00000057 BUY), but you also ...

5

The easiest might be (occur "kittens[ ]and[ ]puppies" nil) If you use occur interactively, you can insert the actual newlines with C-q C-j. If you use the above lisp snippet, you can replace the newlines by \n: (occur "kittens[ \n]and[ \n]puppies" nil)

4

This functionality exists within projectile (projectile-replace). It will use ag/ack/git grep/grep if available (in that order) to find occurrences, and tags-query-replace to execute the query-replace.

4

By default occur searches are case-insensitive. To force them to be case sensitive, you need to set the variable case-fold-search to nil. Note that this is a buffer-local variable. If you set it for one buffer, it won't change it's value for other buffers. In your function, adding the form (case-fold-search nil) to your let statement should work: (defun ...

4

You can use write-file (C-x C-w), then specify a filename. C-x C-w runs the command write-file (found in global-map), which is an interactive compiled Lisp function in ‘files.el’. Note however this does not preserve the metadata of the buffer : if you save the buffer to a file, kill the buffer and re-open the file you just saved, you only get the text, ...

4

A common workflow is: start isearch with C-s, then search for the next word with C-w, then run occur with the current search string with M-s o.

4

occur can do this with prefix argument C-u M-x occur.

4

You have to use a literal C-j to match the newline, which you can etnter by prefixing it with C-q, i.e. M-x occur RET text-to-find.*C-qC-j.*result (occur also takes a prefix argument that tells it how many lines of context to show, which can be useful too).

3

The *Occur* buffer will be in a major mode unsurprisingly called occur-mode. This mode, or perhaps the code that prepares it, or a combination of the two, do a number of things, including these: the buffer is marked read-only it gets its own keymap, with most keys marked undefined the first line is given some text properties The best way to deal with the ...

3

If you want another way to do this, you can use Icicles. A non-negative prefix arg with command icicle-occur (C-c ') searches multiple buffers line by line (like occur, moccur, and grep). If you hit C-! at the prompt that asks which buffers then all buffers are searched. C-9 C-c ' C-! RET Here, C-9 says search multiple buffers. C-c ' is icicle-occur, ...

3

If you delete the entire line from the occur buffer, it will not touch the original buffer. So press e to run #'occur-edit-mode, then delete the entire line (C-k).

2

If the question is not limited to Helm, you can do this in Icicles using command icicle-occur (bound to C-c ') in either of these ways, depending on what you want: Type the regexp Hello.*Bye, if you want to look for Hello followed by Bye. Type Hello then S-SPC then Bye, if you want to look for both words in either order. C-c ' uses lines as search contexts....

2

Thanks to @Drew answer, I could manage to make helm-swoop filter at the same time the occurrences of two words (say 'Hello' and 'Bye'): M-x helm-swoop RET Hello\|Bye.

2

rst-toc can be used to navigate sections.

2

Looking at the source of occur-mode, I found this: (setq next-error-function 'occur-next-error)) This suggests occur-next-error is the function next-error calls in a "occured" buffer. And indeed, it seems that calling occur-next-error (with a negative arg to go backward) does work even after M-x compile. I have not tested this extensively, though. So it ...

2

You need to escape the backslash with another backslash. So: M-s o \\def See the manual node on "Syntax for Strings" for more information.

2

Not sure how helm-swoop worked, but you can do this: (defun call-helm-occur (query) "Passes QUERY to helm-occur using isearch-string'." (let ((isearch-string query)) (call-interactively 'helm-occur-from-isearch))) helm-occur-from-isearch pulls its input from the variable isearch-string, so use let binding to set this variable.

2

For replace-regexp in many files, select the files in Dired and press Q to run dired-do-find-regexp-and-replace. See also the FAQ entry. For the equivalent of occur in many files, you can use A (dired-do-find-regexp) in Dired. Another solution that doesn't use Dired is the grep functions and its variants, where you select files with shell wildcards. ...

2

One way this can be accomplished is by using dired. Here's an article from Xah Lee on how to use dired to find and replace across files: http://ergoemacs.org/emacs/find_replace_inter.html In short, open dired and place the cursor on the directory in which you wish to find and replace. Press Q to start up the query replace prompt. Enter your search ...

2

Use the command next-error that is bound to the key sequence C-x  (the second char is a backtick). As Omar pointed out in his comment, there is also a global keymap for motion commands bound to M-g. M-g ? gives the following listing: Global Bindings Starting With M-g: key binding --- ------- M-g TAB move-to-column M-g ...

2

You can scroll the window in occur-mode-find-occurrence-hook such that point is at the top of the window: (defun my-occur-recenter () "Scroll point to the top of the window." (recenter 0)) (add-hook 'occur-mode-find-occurrence-hook #'my-occur-recenter) Notes: The functions registered in occur-mode-find-occurrence-hook are called with point at the ...

1

The occur buffer is in occur mode. This mode doesn't allow you to edit text. After you save it, you need to change to a different mode. M-x text-mode should do what you need.

1

You might try using rgrep to find the regexp, then in the results buffer enable wgrep-mode (M-x wgrep-change-to-wgrep-mode, or C-c C-p). This will allow you to edit (replace) the regexp directly from within the results buffer, bypassing the need to open each file individually. When the replacement is complete, press C-c C-c to apply the changes to the ...

Only top voted, non community-wiki answers of a minimum length are eligible