A common solution to this type of problem is keyboard macros (not to be confused with (Emacs) LISP macros). Basically, Emacs allows you to record a sequence of keystrokes and "play them back" later. This can be a very handy tool in situations where writing custom LISP code seems unnecessary.
For example, you can create the following keyboard macro (enter the key combination on the left side, the right side shows explanations for each key press):
Cx ( ; start recording a keyboard macro Cx h ; mark whole buffer Cw ; kill region (require 'file-name) ; insert a require statement into the buffer Cx Cs ; save buffer Cx Cf ; find file file-name.el <RET> ; specify the name of the file M-< ; move to the beginning of the buffer Cu Cy ; insert the previously killed text, leaving point where it is (provide 'file-name) <RET> <RET> ; insert a provide statement into the buffer Cx ) ; stop recording the keyboard macro
Now you can replay this macro in some other buffer by typing Cx e or save it for later use. You can also bind a macro to a shortcut in the same way as to a function.
However, there is one drawback to this approach: you want to be able to specify a file name, and not just use the string "file name" every time. This is a bit complicated - by default, the macro on the keyboard does not provide general features for user request (except for the minimum Cx q , as described here ).
Emacs Wiki , however, this requires several workarounds, instead of asking the user in the minibuffer, sometimes it may be enough to run the macro, killing the current line and keeping its text in the register.
Cx ( Ce C-<SPC> Ca ; mark current line Cx rs T ; copy line to register T Ck Ck ; kill current line ... ; actual macro Cx )
Now, when you want to use your macro, you must first write the desired file name to an otherwise empty line, and then make Cx e in that line. Whenever a file name value is required in a macro, you can get it from the T register:
Cx ri T ; insert file-name into buffer
For example, for the provide statement in the above macro, you can write: (provide 'Cx ri T) . Please note that this technique (insertion) also works in the minibuffer, and, of course, you can save several lines in different registers.
It may seem complicated, but in practice it is quite simple.