Suppose I want to put my name and the date at the top of every new buffer that I create. How would I do that?

Furthermore, suppose I want to use the name of the new buffer in the text that I want to place automatically, where what text is placed depends on a parameter that I specify. Let's say if the parameter is "java" then the text placed will be

public class [Filename without file extension] {
    static void main (String[] args) {


or if it's "cpp" then,

public class [Filename without file extension] {
    int main(int argc, char** argv) {


2 Answers 2

  1. Define a function that inserts the boilerplate code you want, at the buffer position you want.

  2. Put that function on a relevant hook.

For #2, one of these might be a relevant hook:

  • window-configuration-change-hook - used when you change buffers
  • write-file-hooks - used when you write a file to disk
  • find-file-hook - used when you visit a file
  • A major mode hook

But there are lots of other hooks, some of which might be relevant for your use case.

This question is closely related, I think. It's about automatically inserting a file header.

See also the questions under tag [yasnippet] (put that in the Search field).


Have a look at the autoinsert package which will do much of what you want although maybe not all.

  • 1
    You might like to give an actual example.
    – Stefan
    Jan 9, 2020 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.