how to set the font and font size in emacs editor

Everybody has a different preference for fonts, font size and the look and feel of the editor that they are using. Most modern editors allow you to customize every single aspect of the look and feel and emacs is no exception.

As with almost everything in emacs, there are several different ways to set the font and the font size of the buffer. Not one method is better than the other as far as I can tell, so whatever works for you will be dependent on your specific requirement.

In this post, we will only deal with changing the font face of the text and not any other aspect of the general look and feel.

set the font of the buffer

If you want to change the font of a single buffer, then you could enable the buffer-face-mode on the current buffer which will enable a minor mode for buffer-specific font face. You can then change the font of the buffer using the buffer-face-set command.

M-x buffer-face-mode

M-x buffer-face-set

Run the buffer-face-mode again to disable the mode and the buffer should change back to the default font face.

In order to change the size of the font, the commands are text-scale-increase, text-scale-decrease and text-scale-adjust. These will increase or decrease the size of the font by one point. The text-scale-adjust command without a parameter will reset the size to the default size of the font.

set the font of all the buffers

In order to set the font of all buffers in the emacs instance, you can use the command set-frame-font. This will set the default font to the specified font and change it in the current frame.

M-x set-frame-font

Another useful command is menu-set-font which will pop up a dialog where you can select the font and then set that as the default font across the buffers in the frame.

M-x menu-set-font

set in the configuration file

All the above options work if you want to set the font for the current session, but if you like to persist it across sessions then you can set it in the configuration file (usually the emacs.d/init.el). You can use the set-face-attribute to do the same.

The example below checks if the font is installed, then it will set the font to the first available font. This works on both Linux and Windows, as I use the Consolas font in windows and the Noto Sans Mono in Linux.

(cond
((find-font (font-spec :name "Noto Sans Mono"))
(set-face-attribute 'default nil :font "Noto Sans Mono-9.5"))
((find-font (font-spec :name "DejaVu Sans Mono"))
(set-face-attribute 'default nil :font "DejaVu Sans Mono-9.5"))
((find-font (font-spec :name "Consolas"))
(progn
(set-face-attribute 'default nil :font "Consolas-10"))))

set the font of the mode-line

You can also customize the font for the mode line to be different to that of the buffer. You can use the set-face-attribute as with the previous example. You will change the mode-line instead of the default in this case.

(set-face-attribute 'mode-line nil :font "Noto Sans Mono-8")

The most useful command to remember when it comes to customizing the font is describe-face and customize-face. The describe-face shows you the information about the font and the current fonts being used and the customize-face gives you an interactive way set the font for various parts of emacs.