I am fairly new to emacs and I want to learn emacs Lisp. In particular, is it possible to write elisp functions in, say, the scratch buffer to do some complex text-processing on my program file example.cpp whose buffer I would have open in another frame.

Indeed if I could write some elisp code to do text-processing across all the files in the project folder that would be even better!

I am using Spacemacs

  • Elisp is perfect for performing text operations on the content of a buffers, you can define your functions in `*scratch*' or, even better, in a separate file. However, there is no concept of "project" in Emacs (even though some packages provides project-like support). Feb 9, 2016 at 6:10
  • @lawlist I do agree with your comment. But, I was also in the same situation as him until I discovered with-current-buffer. Once you know how to act on a desired buffer, you feel you passed the hardest cliff of leaning elisp and see the huge possibilities. Feb 9, 2016 at 6:18

1 Answer 1


Yes you can! (If you mean frame what Emacs think it is.)

To demonstrate, do the following steps:

Run Emacs

This will put you into the *scratch* buffer.

Create a new frame

M-x make-frame-command to create a new frame, which is also in *scratch*.

Open example.cpp

So that you can now see both *scratch* buffer in the first frame and example.cpp in the newly created frame, side by side.

Write lisp

Copy the following code and past it into the *scratch* buffer.

(with-current-buffer "example.cpp"
  (insert "Hello, Emacs!\n"))

Evaluate it

Put your cursor after the last ) and do M-x eval-last-sexp. If you see "Hello, Emacs!" in the other frame, you got the first step right. You can now customize the code to suite your needs.

  • Thanks for this suggestion. I also needed to turn the scratch buffer into "Emacs-Lisp" mode. I am using Spacmacs. While in scratch buffer press SPC SPC emacs-lisp-mode <RTN>. Oct 25, 2019 at 10:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.