uyetch
uyetch

Reputation: 2190

Changing Emacs default font size and new buffer text

I want to configure Emacs so that it has:

  1. Increased default font size. Currently I use Shift+Click to change it every time I open a file, but I want my configurations saved to the emacs config file.

  2. I wanted to have the default text that appears while I open a new buffer to be changed. I assume it would be like some template that opens by default. Here is the code which I would like to see as the default text when I start emacs, so that I can directly work on it.

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <pthread.h>
    
    int main(int argc, char *argv[])
    {
       return 0;
    }
    

Upvotes: 1

Views: 889

Answers (3)

jarodeells
jarodeells

Reputation: 416

Use a hook to specify the desired text whenever you create a new file:

(defun cpp-file-not-found ()`
  (if (or (equal (file-name-extension (buffer-file-name)) "cpp")
      (equal (file-name-extension (buffer-file-name)) "cxx")
      (equal (file-name-extension (buffer-file-name)) "cc")
      (equal (file-name-extension (buffer-file-name)) "C")
      (equal (file-name-extension (buffer-file-name)) "c")
      )
      (progn
        (goto-char 1)
        (insert "#include <stdio.h>\n")
        (insert "int main (int argc, char *argv[]) {\n")
        (insert "  return 0;\n")
        (insert "}\n")
        )
    )
  )
(add-hook 'find-file-not-found-functions 'cpp-file-not-found)

Upvotes: 0

R&#246;rd
R&#246;rd

Reputation: 6681

To change the default font (size) not just for the first, but each Emacs frame (i.e. window in more common terms), use this in your .emacs:

(setq default-frame-alist
      '((font . "Terminus 10")))

(Of course, replace Terminus 10 with your desired font and size.)

Upvotes: 0

Trey Jackson
Trey Jackson

Reputation: 74480

There are better ways to insert a template, but this should do the trick for you. Obviously customize the font to what you want.

Put the following code in your .emacs, which can be found in your home directory, or by doing C-x C-f ~/.emacs RET (and a bunch of other locations are possible, see these SO questions).

(setq inhibit-startup-message t)
(switch-to-buffer "temp")
(set-frame-font "courier-16")
(insert "#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <pthread.h>

int main(int argc, char *argv[])
{
   return 0;
}")

Upvotes: 1

Related Questions