简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM