1

I'm trying to follow this but what I want to do is add two lines to the very top of unopened files. So I want

# -*- mode: org -*-
# -*- coding: utf-8 -*-

added to many *.org files in many other directories. Can't seem to find the Emacs regex to say what I want done, such as, what is "beginning of file" in Emacs regex?

147pm
  • 2,137
  • 18
  • 28
  • I do not believe there is a regex for the beginning of the buffer. Instead, you may wish to ensure the buffer is widened with the `(widen)` command and then `(goto-char (point-min))` and in terms of restoring the lay of the land (in case the buffer was already open) see the macros/functions `save-restriction` and `save-excursion`. And, you will probably be using things like `(while (re-search-forward ...))` -- see the doc-string for setting the bounds (if so desired). [I would probably use `wgrep` for this task, and `multiple-cursors`.] – lawlist Apr 03 '18 at 15:56
  • It sounds like you're saying there isn't a regex way to say "go to the top of the file and add these lines." But of course there is an elisp way. – 147pm Apr 03 '18 at 16:27

1 Answers1

0

You have to open the files to add text to them.

Can you know which files you want to add this text to, i.e., which files do not already have such text?

If so, you can use make-header, from header2.el, to add that text as a (temporary) file header. (See also Automatic File Headers.)

If not, then you can do the same thing, but conditionally: check for such text first, to prevent inserting it again.

When you first visit a file in Emacs, point (cursor position) is automatically at the start of the file. And anyway, make-header always inserts the header at the buffer beginning, regardless of whether the file is already visited in a buffer and the point might not be at the buffer beginning when it is called.

Drew
  • 29,895
  • 7
  • 74
  • 104