Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Algorithmic blackout:

  1. problem: find the files that match
  2. problem: stack/buffer the matches
  3. move the cursor to the next file of the stack and thereby to the location of the match
  4. repeat step 2

Things I have used earlier to tackle the issue.

Vi/m: (the below works in an ubuntu server, perhaps configured by sysadmin but cannot get it work locally)

:grep -r "[^|]*|[^|]*|[^|]*|.*" * 
<edit something>
<edit something>
<damn forgot it>
<forward and so on>

emacs: M-x grep Enter and then to have grep -r "[^|]*|[^|]*|[^|]*|.*" * but now in mystery. How can you do the fast hierarchical edits?

shellesque: then I once created a wild one-liner with some grep-xargs-something-things-vi but I am starting to smell reinventing-the-wheel.

Sorry about the vim/emacs juxtaposition but I don't care which tool you use, use the most convenient (xargs/emacs/vi/m just examples).

share|improve this question
up vote 1 down vote accepted

M-xgrep in Emacs, then I can use the usual keys for following the links representing the found matches, and also the usual general-purpose Emacs keys for switching between buffers back and forth (or for whatever I want).

One can also learn the specialized keys for jumping to the next match.

The "specialized" key to jump immediately to the next found match is quite easy to remember: it's M-g n (go to next) (or C-x `) for next-error. next-error is a command that is more general-purpose than just for grep; from the help (per C-h k M-g n):

[It] normally uses the most recently started compilation, grep, or occur buffer.

(Indeed, first I learned it for LaTeX "compilation".)

More of the general "go to" commands bound to keys in my Emacs (as per M-g C-h):

Global Bindings Starting With M-g:
key             binding
---             -------

M-g ESC      Prefix Command
M-g g        goto-line
M-g n        next-error
M-g p        previous-error

M-g M-g      goto-line
M-g M-n      next-error
M-g M-p      previous-error
share|improve this answer
@imz: just updated that so how can you traverse the files you find with the command? I want the cursor directly to the match point with each point to edit fast. How? – user2362 Mar 16 '11 at 2:16
@hhh: How to traverse: the primitive way is to move cursor to the interesting match in the buffer with the list of the matches, then press Enter--this will "follow the link" and bring you to the "source" of the match.(See the description of the grep-mode for more keys besides Enter that are useful in the buffer with matches: e.g., C-h f grep-mode.) Or the 2nd "smart" way without the hassle of jumping from buffer to buffer that is now described in my updated answer in detail: M-g n and so on. – imz -- Ivan Zakharyaschev Mar 16 '11 at 2:44
Re grep invocation in Emacs. If you are in eshell inside Emacs, you can issue a usual grep ... shell command, and this will invoke the Emacs buffer with grep matches, which in their turn you can use in your Emacs (traverse) the way described in the answer above. – imz -- Ivan Zakharyaschev Mar 16 '11 at 3:37
@imz: which emacs you are using? My emacs GNU Emacs 21.4.1 with M-g C-h returns commands related to things such as bold, italics and such things, nothing to do with browsing. How did you press? I am a bit lost with M-g n also, not sure what it really means (not sure whether different emacs or mispressing). – user2362 Mar 19 '11 at 12:40
@hhh I'm using GNU Emacs 22.2. Well, if I remember correctly, some of the M-g stuff was introduced recently. But C-x ` (a "synonym" for M-g n) must have been there for ages. I don't know what kind of "things such as bold, italics" you could see and why. What's unclear about pressing M-g n? (Perhaps, it hasn't been added yet in your version of Emacs; try C-x ` then.) – imz -- Ivan Zakharyaschev Mar 19 '11 at 16:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.