.emacs is an elisp source file that belongs to GNU Emacs, which is located somewhere in the home directory. The file contains information about the Emacs setup, and is automatically changed when changes are made to the options in Emacs. The file .emacs can also be modified manually if that if preferred (Emacs may have to be restarted after modification of the file for the changes to take effect).

For reaching the file from Emacs, one can simply press C-x C-f and then enter "~/.emacs".

Here is an example of an .emacs file intended for use by C/C++ programmers:

(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(c-basic-offset 4)     ;;This variable will make the indentation be 4 spaces wide when programming C/C++
 '(tab-width 8))         ;;This will set the tab width to 8 spaces
 '(indent-tabs-mode nil) ;;This will disable Emacs from replacing multiple spaces in the indentation with tabs

Or if mode hooks and file type associations are prefered, in order to apply certain settings for certain modes and open certain file types in certain modes, the following file can be used:

(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(blink-cursor-mode nil)
 '(column-number-mode t)
 '(inhibit-startup-screen t)
 '(scroll-bar-mode (quote left))
 )

;;;;;;;;;;;;;;;;
;; MODE HOOKS ;;
;;;;;;;;;;;;;;;;

(defun c_cpp_mode_hook () ;;Defining a function which prepares for C/C++ programming when called
  (setq
   c-basic-offset 4
   tab-width 8
   indent-tabs-mode nil
   )
  (custom-set-variables
   '(show-paren-mode t)) ;;For some reason setq doesn't work with this variable for me
  (c-set-offset 'substatement-open 0) ;;Sets indent style of substatement start brackets to match Allman ("bsd") style
  )

(add-hook 'c-mode-hook   'c_cpp_mode_hook) ;;Tells Emacs to run c_cpp_mode_hook when opening a C file
(add-hook 'c++-mode-hook 'c_cpp_mode_hook) ;;Tells Emacs to run c_cpp_mode_hook when opening a C++ file

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILE TYPE ASSOCIATIONS ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;Enter c-mode when opening files with the extensions .vs, .fs
(setq auto-mode-alist (cons '("\\.vs$" . c-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.fs$" . c-mode) auto-mode-alist))
Community content is available under CC-BY-SA unless otherwise noted.