This guide provides a comprehensive configuration file for setting up a personalized and efficient Emacs experience tailored for Windows 10 users. The configuration file covers performance optimization, package management, appearance customization, environment settings configuration, as well as the setup of Org mode and ChatGPT API integration.
Open a Command Prompt or PowerShell window with administrative privileges. Enter the following command to install Chocolatey:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Enter the following command to install the latest version of Emacs:
choco install emacs
Verify that the installation was successful by entering the following command:
emacs --version
Right-click on the Computer icon on the desktop or in the Start menu, and choose Properties. Click on Advanced system settings in the left sidebar. In the System Properties window, go to the Advanced tab and click on the Environment Variables button. In the User variables section, click on the New button. Set the Variable name as HOME and the Variable value as %USERPROFILE%. Click OK to save the changes. Click on the New button again in the User variables section. Set the Variable name as EMACS_SERVER_FILE and the Variable value as %USERPROFILE%.emacs.d\server\server. Click OK to save the changes. Click OK to close the Environment Variables window and then click OK again to close the System Properties window.
Open the init.org file in Emacs. Customize the init.org file according to your preferences. Use the **org-babel-tangle** command to generate the init.el file. This command will extract the Emacs Lisp code blocks from the Org file and put them into the init.el file in the same directory. Restart Emacs to apply the changes.
This section configures the automatic tangling of the init.org file upon saving, ensuring that the init.el file is always up-to-date with the latest changes.When the init.org file is saved, this function will automatically tangle it, creating or updating the init.el file accordingly.
(defun tangle-init-file ()
(when (equal (buffer-file-name) (expand-file-name "~/.emacs.d/init.org"))
(org-babel-tangle)))
(add-hook 'after-save-hook 'tangle-init-file)
Set the garbage collection threshold to its highest value during startup to speed up the process.
(setq gc-cons-threshold most-positive-fixnum)
Restore the garbage collection threshold to a more reasonable value after Emacs has started to optimize runtime performance.
(add-hook 'emacs-startup-hook (lambda () (setq gc-cons-threshold 16777216)))
Configure package.el by setting the package repositories, initializing the package system, and ensuring that Org Mode and use-package are installed.
(require 'package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("gnu" . "https://elpa.gnu.org/packages/")))
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Install Org Mode from GNU ELPA
(unless (package-installed-p 'org)
(package-install 'org))
;; Install use-package if not already installed
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(require 'use-package)
(setq use-package-always-ensure t)
Disable the tool bar (icons), scroll bar, and menu bar.
(tool-bar-mode -1)
(scroll-bar-mode -1)
(menu-bar-mode -1)
Do not show the startup screen.
(setq inhibit-startup-screen t)
Remove the default message in the scratch buffer.
(setq initial-scratch-message nil)
Set the default font to “JetBrains Mono” with size 15.
(defun my/set-frame-font ()
(set-frame-font "JetBrains Mono-15" nil t))
(add-hook 'after-make-frame-functions
(lambda (frame)
(select-frame frame)
(my/set-frame-font)))
(my/set-frame-font)
Configure and load the ‘modus-operandi’ theme, with slanted and bold constructs enabled.
(use-package modus-themes
:ensure t
:init
(setq modus-themes-slanted-constructs t
modus-themes-bold-constructs t)
:config
(load-theme 'modus-operandi :no-confirm)
;; OR (modus-vivendi)
)
Change the initial working directory to the user’s home directory.
(cd "~")
Define a function that toggles the fullscreen state of the Emacs window.
(defun toggle-fullscreen ()
(interactive)
(if (eq (frame-parameter nil 'fullscreen) 'fullboth)
(set-frame-parameter nil 'fullscreen nil)
(set-frame-parameter nil 'fullscreen 'fullboth)))
Set the F11 key to call the `toggle-fullscreen` function.
(global-set-key [f11] 'toggle-fullscreen)
Configure Emacs to start in fullscreen mode and apply fullscreen settings to future frames.
(add-to-list 'default-frame-alist '(fullscreen . fullboth))
Ensure that new frames created by the Emacs daemon start in fullscreen mode.
(defun set-fullscreen-for-new-frame (frame)
(set-frame-parameter frame 'fullscreen 'fullboth))
(add-hook 'after-make-frame-functions #'set-fullscreen-for-new-frame)
Load Org Mode and configure basic settings, such as hiding leading stars, setting agenda files, and defining TODO keywords.
(use-package org
:config
(setq org-hide-leading-stars t
org-agenda-files '("~/org")
org-todo-keywords '((sequence "TODO" "IN-PROGRESS" "WAITING" "DONE"))))
;; Require org-tempo and add custom Org Tempo template
(require 'org-tempo)
(defun org-tempo-src-emacs-lisp-tangle-yes ()
"Insert an emacs-lisp source block with :tangle yes option."
(interactive)
(let ((content (org-tempo--expand-structure-template '("se" . "src emacs-lisp :tangle yes"))))
(insert content)
(search-backward "#+END_SRC")))
(with-eval-after-load 'org-tempo
(add-to-list 'org-structure-template-alist '("se" . "src emacs-lisp :tangle yes")))
Create a file named “secret.el” in your Emacs configuration directory (~/.emacs.d/). Add the following line to “secret.el”, replacing “your_api_key_here” with your actual OpenAI API key:
(setq my-openai-api-token "your_api_key_here")
Add the provided org-ai configuration code to your Emacs configuration file.
Enables GPT-4 integration with Org mode for AI-assisted text generation. Automatically installs AI snippets for Yasnippet (optional).
(use-package org-ai
:ensure
:commands (org-ai-mode)
:init
;; Load secret.el
(load-file "~/.emacs.d/secret.el")
:custom
(org-ai-openai-api-token my-openai-api-token)
:config
;; Set the default chat model to GPT-4 (replace "gpt-4" with the actual GPT-4 model name)
(setq org-ai-default-chat-model "gpt-4")
;; if you are using yasnippet and want `ai` snippets
(org-ai-install-yasnippets))