0

I use emacs to mostly code in c++. Is there any way to add a particular piece of code snippet to every new file opoened in Emacs.

For eg: Suppose i want to create a new file in emacs with name abc.cpp and emacs by default initiates the file with following lines

using namespace std;

int main(){
    return 0;
}
jlahd
  • 6,257
  • 1
  • 15
  • 21
Sarkar
  • 33
  • 1
  • 6

1 Answers1

0

Like this:

(defun insert-my-c++-headers ()
  (when (= 0 (buffer-size))
    (insert "using namespace std;\n\nint main(){\n    return 0;\n}\n")))

(add-hook 'c++-mode-hook 'insert-my-c++-headers)
jlahd
  • 6,257
  • 1
  • 15
  • 21
  • If you want to have the functionality every time you run emacs, add it to the `.emacs` file in your home directory. – jlahd Jul 31 '14 at 20:29