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.

;; full emacs version number. A string.
emacs-version
;; sample value: "24.5.1"

;; A integer, emacs major version number.
emacs-major-version
;; sample value: 24

;; A integer, emacs minor version number.
emacs-minor-version
;; sample value: 5

Compare Version String

;; check emacs version
(if (version< emacs-version "24.4")
    (message "is before 24.4")
  (message "is 24.4 or after"))

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.

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.
Or Buy Xah Emacs Tutorial
Or buy JavaScript in Depth

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Init

Init Basics

packages

text editing

file

appearance

misc