2

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) {

    }
}
Drew
  • 75,699
  • 9
  • 109
  • 225
Prikshet Sharma
  • 237
  • 1
  • 3

2 Answers2

1
  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).

Drew
  • 75,699
  • 9
  • 109
  • 225
-1

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

éric
  • 420
  • 3
  • 3