I want to install nginx-mode
into emacs so that I can have highlighting and formatting of my nginx
configuration files. What is the easiest way to do this?
Asked
Active
Viewed 1,368 times
6

Drew
- 29,895
- 7
- 74
- 104

Simon Woodside
- 7,175
- 5
- 50
- 66
2 Answers
7
This requires emacs24
or newer.
emacs
now has a pretty good package management system. The default package repository ELPA
has a pretty restricted set of modes and packages, so instead we'll use MELPA
which is actively maintained and growing.
First, install MELPA
:
emacs /sudo::/etc/emacs/site-start.el
Paste in this code: (from https://melpa.org/#/getting-started)
(require 'package) ;; You might already have this line
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize) ;; You might already have this line
Save and exit.
Second, install nginx-mode
To get nginx pretty-printing and indentation, do this to install nginx-mode:
emacs
M-x package-list-packages RET (Type meta-key and S, then type package-list-packages and hit return)
C-s nginx RET (Type control-S to search, type nginx and hit return to find the nginx-mode package)
i (to mark it to install)
x (to execute installation of marked packages)
Use nginx-mode
Now you can switch into nginx-mode with M-x nginx-mode
. E.g.:
emacs /sudo::/etc/nginx/sites-available/default
M-x nginx-mode RET
You can make it recognize the sites-enabled files automatically by looking at these instructions.

Simon Woodside
- 7,175
- 5
- 50
- 66
-
1`sudo emacs` hardly!? – tripleee Nov 29 '16 at 09:53
-
@tripleee yes `sudo emacs /etc/emacs/site-start.el` is required since it's owned by root. As for the second `sudo emacs` I've corrected it. How do you install packages globally? – Simon Woodside Nov 30 '16 at 04:08
-
If you mean me personally, I tend to use my system's package manager, and create a package if one doesn't exist in the distro's repositories. – tripleee Nov 30 '16 at 04:46
-
Actually, you need to use `sudo emacs` when you install the MELPA module as well, in order to have it installed into root. You need that when you're editing the nginx config files. – Simon Woodside Dec 02 '16 at 04:42
-
For editing individual files as `root`, I use Tramp's `sudo` mode, specifically to avoid running the behemoth that is Emacs completely with root privileges unless absolutely necessary – tripleee Dec 02 '16 at 04:44
-
@tripleee I've updated the instructions to use Tramp's sudo mode. – Simon Woodside Dec 06 '16 at 06:05
-
According to the [documentation](https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html) Emacs looks for site-start.el in directories listed in the `load-path` variable. In my case it was `/usr/share/emacs/site-lisp/site-start.el` – Jan Šimek Apr 20 '19 at 08:52
1
If you can't use melpa:
You can download the file nginx-mode.el
from https://github.com/ajc/nginx-mode and copy it in ~/.emacs.d/
Then in you .emacs you can add
(add-to-list 'load-path "~/.emacs.d/")
(autoload 'nginx-mode "nginx-mode" nil t)
(add-to-list 'auto-mode-alist '("nginx.conf\\'" . nginx-mode)) ;; or M-x nginx-mode

djangoliv
- 1,698
- 14
- 26