0

I'd like to add a couple lines of text (copyright) to the top of all text files in a directory. Can I do this in emacs without copy/pasting for each file?

Max Ghenis
  • 10,717
  • 13
  • 59
  • 109

2 Answers2

3

This is copied from Chris Conway's answer to a different question: Using Emacs to recursively find and replace in text files not already open

  1. M-x find-name-dired: you will be prompted for a root directory and a filename pattern.
  2. Press t to "toggle mark" for all files found.
  3. Press Q for "Query-Replace in Files...": you will be prompted for query/substitution regexps.
  4. Proceed as with query-replace-regexp: SPACE to replace and move to next match, n to skip a match, etc.

You can use it the same way

Community
  • 1
  • 1
Thomas
  • 131
  • 2
1

Yes, with

find . -type f -exec emacs -batch '{}' --eval '(insert-string "foo\nbar\nbaz\n")' -f save-buffer \;

or something to that effect. The emacs bit is

emacs -batch filename --eval '(insert-string "foo\nbar\nbaz\n")' -f save-buffer

replace "foo\nbar\nbaz" with your message. However, using emacs for this is really a lot of overkill. You could just put your copyright header into a file and use cat header file > tempfile; mv tempfile file.

Wintermute
  • 39,646
  • 4
  • 64
  • 71