1

so I'm jumping back into trying to learn programming.

I'm using my MBP using SSH to access the university's linux machines and programming in python and using emacs.

Each file is required to have a header:

# File: file.py
# Author: My Name
# Date: 30 September 2014
# Section: 1
# Email: [email protected]
# Description: sample header 

My question is:

Is there a way to make that a permanent template, so that whenever I create a new file, those comments are already there? everything except file date and description would be static. If there is a way, is there also a way to automatically fill in the stuff that changes file.py = whateverINamedTheProgram.py and date = todaysDate or something?

1
  • You may wish to take a look at the yasnippet library, which can be installed with M-x list-packages -- your headers can be added with a keyboard shortcut using yasnippet. You may also wish to have a form file directory (with all your forms), and the files containing your forms (e.g., header, etc.) can be inserted into your current /working file by using the function insert-file-contents. Commented Oct 1, 2014 at 0:17

2 Answers 2

5

header2.el is the way to go, however, here is an occasion to learn some emacs lisp and see how we can script our use of emacs. Maybe it's a bad idea now since you are learning programming, but maybe you'll find it clearer (than the non friendly header2 and yasnippet docs).

The advantage of this code is that you call it on demand and we can do what we want.

(defun my-insert-header ()
  "Asks for file name, date, description, ... and insert at point."
  (interactive)
  (setq cur-file (read-from-minibuffer "file name ? " 
                   (file-name-nondirectory (buffer-file-name))))
  (setq cur-date (org-read-date))
  (setq cur-author "Me")
  (setq cur-description (read-from-minibuffer "description ? "))
  (insert (format "# File: %s \n" cur-file))
  (insert (format "# date : %s \n" cur-date))
  (insert (format "# Description : %s \n" cur-description))
)

Put it in your dot file (.emacs.d/init.el or ~/.emacs).

You can just try it by putting it in the scratch buffer, going just after the last parentheses and hitting C-x C-e (eval-last-sexp). (see this tutorial: http://wikemacs.org/wiki/Emacs_Lisp_in_15_minutes)

Now explanations:

  • defun: define a function, like def in python.
  • (interactive): let the function be called with Alt-x. see http://ergoemacs.org/emacs/elisp_basics.html
  • setq: way to define a variable
  • read-from-minibuffer: does what it says :) the second argument is an optional default text to edit
  • org-read-date: a nice utility function from org-mode. You can change dates with Shift-left/right. It returns a string.
  • buffer-file-name: returns "/home/you/dir/try.py"
  • file-name-nondirectory: returns "try.py"
  • insert: insert text in current buffer
  • format "foo %s \n": string formatting, like in python

and that's it. I let you finish the function as you want !

Sign up to request clarification or add additional context in comments.

1 Comment

Just came across this answer and I've tried it on Emacs 26.1. It's great but I needed to do a correction, please find them below: ` (insert (format "# Date: %s \n" cur-date))`
0

Yes, you can use library header2.el to do what you ask. You can use it with different programming languages. Particular fields in the file header can be automatically updated when you save the file, if you want.

Your question is almost identical to this question, posed at almost the same time. (Coincidence?) See that question for more info about header2.el. Or see Emacs Wiki page Automatic File Headers, for more description and examples.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.