Introduction
This article is about the ways by which Emacs can be invoked / executed / loaded in windows operating system.
After downloading Emacs from the official website https://www.gnu.org, unzip the contents to a suitable location. Add the bin directory of the unzipped folder to the windows environment variable — path
. The following .exe(executable) files are available in the bin directory, that can be used to start Emacs in windows.
1. emacs.exe
2. runemacs.exe
3. emacsclient.exe
4. emacsclientw.exe
Each one of these executable files have a particular behaviour, applicable to certain situations.
Initial Configuration
Behaviour and appearance of Emacs can be configured / modified using an initial configuration file named init.el. By default, this file is located in c:\Users\username\.emacs.d\init.el
.For user convenience, a folder c:\Home
can be created (Assuming windows is installed in C drive). Add the path of the same to the Environment variable Home
in windows. While being loaded, Emacs search for initial configuration file in c:\Home\.emacs.d\init.el
. If not available, the file is automatically created by Emacs
In this article, the ctrl and alt keys in keyboard are referred as C and M. So C-c refers to pressing ctrl and c keys together. Such kind of key bindings are used in Emacs to perform special actions.
Starting Emacs in GUI mode (Normal Startup)
The command runemacs
will invoke Emacs in GUI mode. Different ways of invoking runemacs
are listed here.
1. A shortcut key to runemacs.exe can be placed in the desktop and double clicked using mouse
2. Open the run
utility of windows using the key binding Windows+r and type the command runemacs
3. Open the cmd / powershell and type runemacs
at the prompt.
Starting Emacs inside the terminal (No GUI)
Open powershell / cmd terminal and type the following command
emacs -nw <filename>
where the option -nw
states no window. This makes Emacs to run in the current terminal. The terminal-mode specific configurations of Emacs can be added to the initialization file .init.el as follows
(unless (display-graphic-p)
;; Remove any key bindings and theme setup here
(require ‘org)
(define-key org-mode-map (kbd “M-n”) ‘org-meta-return)
)
In the above example, when Emacs is made to run within terminal, the key binding “Alt+n” is assigned to the variable “org-meta-return”
Starting Emacs In Client Server Mode
Starting Emacs Server
Every time when Emacs is started using the command runemacs
, it gets initialized as per configurations in init.el
. The loading time of Emacs will be irritatingly high if it needs to initialize several packages during start up. A solution for this issue is to start Emacs as a server and later on client instances of Emacs are invoked as and when required. This makes Emacs to initialize only one time, during the start of server instance and makes starting of client instantaneous. Thus Emacs client instances will load much faster when compared with running Emacs as a stand alone process. One approach to start Emacs server instance is to run GUI version of Emacs by issuing the command runemacs
at the prompt. Once Emacs finished loading its buffers, invoke the key binding Alt+x followed by the command start-server
. This will start the server process in the background.
Another approach is to put the statement (start-server)
in the ~/.emacs.d/init.el file. Thus when Emacs is invoked, the server process gets started automatically. The next step is to call Emacs client application and it automatically binds with the server process. Emacs client application can be invoked using emacsclient.exe or emacsclientw.exe.
Using emacsclient.exe
In case of invoking the command emacsclient <filename>
from cmd / powershell, the file identified by the filename will be loaded in a buffer and Emacs delivers a text-mode display of the buffer. While using this command, filename is compulsory or else issue the argument -t
as emacsclient -t
to invoke Emacs client with scratch buffer(dummy buffer that cannot be saved). Once the command is issued from the terminal, the terminal will wait for the Emacs client process to get completed. In the mean time, no other application can be invoked from the current terminal.
Using emacsclientw.exe
The command emacsclientw <filename>
will open the file in a buffer and provides graphical display mode for the buffer to be viewed. The terminal is freed from the necessity to wait for the client process to get completed. Also emacsclientw -t
will evoke Emacs with a scratch buffer.
Starting Emacs in daemon mode
It is also possible to run Emacs in daemon mode. The Emacs server process will start in the background and it will wait for a suitable Emacs client to latch on to it. To run Emacs in daemon mode, issue the command runemacs -daemon
. An alternate option is to use the command emacs -daemon
.In this case, the terminal will wait for the return of the server process. Also emacsclientw
cannot be used. You can guess the reason. Only runmeacs.exe supports graphical display.
To Terminate Emacs
From the Emacs buffer issue the key binding C-x C-c
to kill the current Emacs process. It will also kill any Emacs server unless Emacs is started in daemon mode. If Emacs is started in daemon mode, then kill Emacs by issuing M-x kill-emacs
. To kill the client alone, without affecting the server, issue the keys C-x #
. To kill the frame, issue C-x 0 5
.
References
https://cs.wellesley.edu/~cs240/s19/docs/common/emacs/
https://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-Startup.html
http://wikemacs.org/wiki/Emacs_server