Hanfei Sun
Hanfei Sun

Reputation: 47071

Insert a few lines as the head of a huge file by emacs?

For example, I have a huge file that I need to insert two lines before it. Is there a easy way to do it in emacs?

Upvotes: 1

Views: 252

Answers (2)

Thomas
Thomas

Reputation: 17422

The only way to do that in Emacs is to visit the file, insert the two lines at the top of the associated buffer, and write the buffer to disk. But really, Emacs is the wrong tool for this job. You should do it on the command line instead.

One easy way to achieve that is simply using cat:

cat - /path/to/input-file >/path/to/output-file <<EOF

After that command, you can type your two lines right in the shell, and as the third line write EOF. This will then insert those two lines at the top of the file, and write the result to output-file. For instance:

cat - /path/to/huge-file >/path/to/output-file <<EOF
> This is the first line
> This is the second line
> EOF

If you have to do this for many files, it makes sense to write your two lines to a separate file, so you don't have to type them every single time. You would then simply do:

cat /path/to/two-line-file /path/to/huge-file >/path/to/output-file

Upvotes: 6

Drew
Drew

Reputation: 30708

See also library header2.el. It lets you insert a file header (which you can define) automatically.

Upvotes: 0

Related Questions