Elisp: 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: 24
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

Elisp: Check If a {function, variable, feature} is Defined/Loaded

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Init

Init Basics

packages

text editing

file

appearance

misc