Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 ยท Version Control and Tools

Shells, Compilation, grep and Project Search in Emacs

Run commands from Emacs: shell-command, shell, eshell and vterm, the compile and next-error workflow, grep and occur search, and project.el for project-wide work.

You rarely need to leave Emacs to run a command. It can run one-off commands, host full shells, run builds and jump to the errors they report, and search whole projects with results you can navigate. These tools share one idea: command output goes into a buffer, and certain lines (errors, matches) become links that open the right file at the right line. This lesson covers each tool and the keys that tie them together.

Commands are real. The runnable blocks are JavaScript stand-ins that model the output parsing Emacs does for compile and occur.

One-off shell commands

  • M-! (shell-command) runs a command and shows its output in the echo area or in *Shell Command Output*. C-u M-! inserts the output at point.
  • M-& (async-shell-command) runs it without blocking Emacs.
  • M-| (shell-command-on-region) pipes the region through a command, for example sort -u or jq .; with C-u the output replaces the region.

These make Emacs a front end for all your Unix tools: select a table, press M-|, type column -t, done.

Shells inside Emacs

Command What it is Good for
M-x shell Runs your shell in a regular buffer (comint) Simple, uses all Emacs editing; limited for full-screen programs
M-x eshell A shell written in Elisp Portable (also on Windows), mixes Lisp and commands
M-x term / ansi-term A terminal emulator Basic full-screen programs
M-x vterm A fast terminal using libvterm (package) Programs like htop or vim; needs compiling
M-x eat A terminal emulator written in Elisp (package) A vterm alternative

In M-x shell, the output is a buffer: search with C-s, copy any text, and use C-c C-p and C-c C-n to jump between prompts. M-p and M-n cycle input history. In Eshell, commands are Lisp-friendly: ls is implemented in Elisp, (+ 1 2) evaluates Lisp, and find-file foo opens a file. Redirect to a buffer with > #<buffer scratch>.

The compile workflow

M-x compile runs a command (default make -k) in the *compilation* buffer, from the current directory. Edit the command for your project: npm test, cargo build, pytest -x. Emacs remembers the command in compile-command and offers it next time, and M-x recompile (bound to g in the compilation buffer) repeats it.

The key feature is error parsing: Emacs recognizes file:line:column: message patterns from many compilers and test runners, and turns them into links.

  • M-g n (next-error) (or C-x followed by the backquote key) jumps to the next error in its source file; M-g p the previous one.
  • RET on an error line in the compilation buffer visits it.
  • C-c C-k kills a running compilation.
ELISP
(setq compilation-scroll-output 'first-error)  ; scroll until the first error
(setq compilation-ask-about-save nil)           ; save files without asking
(global-set-key (kbd "<f5>") #'recompile)

The same next-error machinery serves grep, occur, Flymake and Xref results, so M-g n always means "go to the next thing in the results buffer".

JS Stand-in: turn compiler output into a next-error list

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Searching text

  • M-x grep runs grep (or the command you configure) and lists matches as links. M-x rgrep searches recursively by file pattern and skips version-control folders; M-x lgrep is non-recursive.
  • M-x project-find-regexp (C-x p g) searches the current project through Xref, using ripgrep or git grep when available. With Consult, consult-ripgrep shows live results.
  • M-x occur (also M-s o) lists every line in the current buffer matching a regexp, in a separate buffer; press RET to jump. Edit the results with e to change the original buffer (occur-edit-mode), then C-c C-c.
  • M-x multi-occur searches several buffers. isearch (C-s) followed by M-s o converts the current search into an occur list.
  • M-x query-replace-regexp (C-M-%) replaces interactively with y, n, ! for all.
JS Stand-in: occur with context

Press Run (or Ctrl + Enter) to see the console output here.

No output.

project.el

Emacs has a built-in project concept, project.el: a project is a directory tree with a version-control root (or a marker file). The prefix C-x p gives you project-aware commands:

Keys Command
C-x p f Find a file in the project
C-x p g Search the project with a regexp
C-x p b Switch to a project buffer
C-x p d Open the project root in Dired
C-x p s Open a shell in the project root
C-x p c Run compile in the project root
C-x p p Switch to another known project
C-x p k Kill the project's buffers

Because compile, shell and grep use default-directory, running them from the project root is what makes them useful in large repositories. A complementary package, Projectile, does similar things with more features; stay with project.el unless you need them.

Long-running commands in M-x shell that print huge amounts of output can slow Emacs. Pipe to a file or use async-shell-command with a limited command, and consider so-long-mode, which Emacs enables automatically when a buffer has very long lines.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command runs a build and lets you jump to the errors it reports?

    compile runs the command and parses the output into jumpable errors; M-g n visits the next one.
  2. 2. What does M-g n do after a grep or compile?

    next-error works with compilation, grep, occur, Flymake and xref results.
  3. 3. Which command lists all matching lines of the CURRENT buffer in a separate buffer?

    occur searches the current buffer and lists matches you can jump from.
  4. 4. Which prefix gives access to built-in project commands?

    project.el commands live under C-x p (find file, search, shell, compile...).

Summary

  • M-!, M-& and M-| run commands without leaving Emacs; shell, eshell, term and vterm host full shells.
  • M-x compile plus M-g n / M-g p is the build-and-fix loop; recompile repeats it.
  • grep, rgrep, occur, project-find-regexp and consult-ripgrep cover searching.
  • C-x p gives project-aware find, search, shell, compile and buffer commands.
  • All results buffers share next-error.

Next: Keyboard Macros and Registers