Emacs Lisp: Determine OS, Emacs Version, Machine Host Name

By Xah Lee. Date: . Last updated: .

The following are useful for setting up emacs init file on different machines.

Find Operating System Type

Here's how to find out what operating system emacs is running on.

(cond
 ((string-equal system-type "windows-nt")
  (progn
    (message "Microsoft Windows")))
 ((string-equal system-type "darwin") ;  macOS
  (progn
    (message "Mac OS X")))
 ((string-equal system-type "gnu/linux")
  (progn
    (message "Linux"))))

Find Emacs Version

The following variables returns emacs version number.

emacs-version
Value is a dotted string. Sample value: "24.5.1"
emacs-major-version
Value is a integer, emacs major version number. Sample value: 28.
(if (< emacs-major-version 28)
    (progn
      (message "not emacs 28"))
  (progn
    (message "yes emacs 28 or later")))
emacs-minor-version
Value is a integer, emacs minor version number. Sample value: 5

Compare Version String

version<
Compares emacs version string.
(if (version< emacs-version "27.1")
    (message "too old")
  (message "good"))
version<=
Compares emacs version string.
version=
Compares emacs version string.

Get Host Name, User Name, Init File Path

Here's some other useful elisp function or variable to determine machine name (host name), user name, emacs init path.

system-name
Returns the host name.
user-login-name
Returns user login name.
user-emacs-directory
Init file dir.
user-init-file
Init file path.

For detail, see:

Check If a Package is Loaded

Emacs Lisp: Check If a function/variable is Defined

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Init

Init Basics

Keys

Font

Text Editing

Completion

File

Restore Things

Backup/AutoSave

Line

Appearance

Misc

Advanced Init Tips

packages