A text editor provides an environment to write and edit plain text files such as source codes. Many would like to use IDEs to write and run source codes. That might be very useful for small programs, but as long as your projects being larger, writing and editing codes with IDEs become a very tedious task. Emacs is one of the best text editors that let us edit and write source codes faster and easier. It might be very challenging to start using any editors like Emacs, but it is definitely worth to spend some time to learn the first steps. This tutorial provides a list of commands, configurations and features that we need to begin.
To start working Emacs you might need to install it first. To install Emacs follow the instruction at GNU Emacs. Briefly for:
Use a text editor to create Emacs config file called
.emacs
in the home directory cd ~
then insert
the following to make our very preliminary configurations by:
;; Add MELPA
(package-initialize)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
;; Personal information
(setq user-full-name "Your Name"
user-mail-address "your@email.com")
;; Backup in one place
(setq backup-directory-alist '(("" . "~/.emacs.d/backup")))
;; Turn on bracket match highlight
(show-paren-mode 1)
;; Auto insert closing bracket
(electric-pair-mode 1)
Note that we can use (setq make-backup-files nil)
and
(electric-indent-mode -1)
to disable automatic backup and
new lines auto-indentation.
The following are some important commands in Emacs. Note that
C
and M
stand for Ctrl
and
Alt
respectively.
C-x C-c
: exitC-z
: suspendC-k
: kill the lineC-w
: cut the line/textM-w
: copy the line/textC-y
: uncut/paste the line/text (yank)C-space arrow keys
: select textC-shift arrow keys
: select paragraphsC-x u
: undoC-a
: move to the beginning of the lineC-e
: move to the end of the lineM-left
: one word leftM-right
: one word rightC-home/M-<
: move to the beginning of the bufferC-end/M->
: move to the end of the bufferM-g g
: go to lineC-s/C-r
: search/reverse searchM-%
: replaceC-g
: stop a commandC-x C-f
: make/open a file as a new bufferC-x b
: change the bufferC-x k
: kill the bufferC-x 1
: close other windowsC-x 0/q
: close/quit windowsC-x o
: switch to other windowsC-h ?
: help listC-h t
: tutorialC-h d
: search help for a patternC-h c
: show help for the commandM-x <command>
: run commandsM-x ispell
: spell check; enter the suggested
digit
or a
to accept and r
to
rewriteM-x package-install
: install packagesM-x package-list-packages
: list of packagesYou may find more details in:
An easy way to change the appearance of Emacs is changing faces.
M-x list-faces-display
shows current faces that can be
modified (use GUI Emacs to access them by clicking). To customize
certain faces, use M-x customize-face
and select the face
that you want to change (press tab to see all options).
Also, we are able to customize faces from Emacs config file. For
instance, adding the following to the .emacs
file will
change some faces in the Emacs terminal mode:
;; Customize faces
(custom-set-faces
'(font-lock-builtin-face ((t (:foreground "SkyBlue1"))))
'(font-lock-comment-delimiter-face ((t (:foreground "salmon"))))
'(font-lock-comment-face ((t (:foreground "chocolate1" :slant oblique))))
'(font-lock-constant-face ((t (:foreground "lime green"))))
'(font-lock-doc-face ((t (:foreground "light coral" :slant oblique))))
'(font-lock-function-name-face ((t (:foreground "green" :weight bold))))
'(font-lock-keyword-face ((t (:foreground "SteelBlue1" :weight bold))))
'(font-lock-preprocessor-face ((t (:foreground "cornflower blue" :slant italic))))
'(font-lock-regexp-grouping-backslash ((t (:weight bold))))
'(font-lock-regexp-grouping-construct ((t (:weight bold))))
'(font-lock-string-face ((t (:foreground "violet" :slant italic))))
'(font-lock-type-face ((t (:foreground "green" :weight bold))))
'(font-lock-variable-name-face ((t (:foreground "lime green"))))
'(font-lock-warning-face ((t (:foreground "dark orange" :weight bold))))
'(minibuffer-prompt ((t (:foreground "lime green" :weight bold)))))
To change the theme use M-x load-theme
, then press
tab
to see list of the available themes. To unload the
theme use, M-x disable-theme
. Also, we can set a theme
permanently, by adding (load-theme '<theme-name>)
to
the .emacs
init file. For example:
;; Load tango-dark theme
(load-theme 'tango-dark)
In Emacs we can install extra packages based on our needs. To install
a new package, press M-x
and type
package-refresh-contents
to fetch all packages and then use
M-x package-install
and enter the new package’s name. For
example we can install Markdown-mode, htmlize, ESS (R-mode),
autocomplete-mode and Jedi with:
M-x package-install markdown-mode
M-x package-install htmlize
M-x package-install ess
M-x package-install auto-complete
M-x package-install jedi
To see list of the available packages use
M-x package-list-packages
. And to delete packages, use
M-x package-delete
and enter the package’s name. Note that
you may use C-s / C-r
for searching and C-g
to
cancel any actions and q
to close buffers in Emacs.
Org website describes “Org mode is for keeping notes, maintaining TODO lists, planning projects, and authoring documents with a fast and effective plain-text system”. Org-mode in Emacs is very similar to Markdown. They let us combine text and codes and evaluate codes and generate PDF or HTML outputs. Following are great resources to learn Org:
Recent Emacs includes Org mode, to begin, use
emacs test.org
to create an Org file by Emacs.
Let’s learn how use org-mode to:
M-enter
M-up/down
tab/shift-tab
<e-tab
<s-tab
C-x C-s
, export C-c C-e
, and exit
C-x C-c
C-c C-e
and press h
and then
o
to see HTML outputTo create above list in Org use:
** Introduction
Let's learn how use *org-mode* to: - insert headlines (=M-enter=) - press =M-enter= to go to the next line
- move headlines up and down (=M-up/down=)
- fold/unfold (=tab/S-tab=)
- insert blocks
- example blocks (=<e-tab=)
- source code blocks (=<s-tab=)
- for example org/bash/python
- create tables
- create TODO items
- save (=C-x C-s=), export (=C-c C-e=), and exit (=C-x C-c=)
- use =C-c C-e= and press =h= and then =o= to see HTML output
Use M-up/down
to see how easy we can move headlines and
use C-c C-e h o
to see the outputs in the browser.
Note: unordered lists in Org start with - or + and ordered lists start with a number and a dot and descriptions use ::.
For example, this is an ordered list:
M-enter
M-enter
To create above list in Org use:
*For example*, this is an /orderd/ list: 1. First - press =M-enter=
2. Second - press =M-enter=
3. Third is a link to [[https://orgmode.org/][org-mode]] website
The following is table of key abbreviations:
Name | key | Abbr. |
---|---|---|
Control | Ctrl | C |
Meta | Alt | M |
Shift | Shift | S |
Return | Enter | RET |
| Name | key | Abbr. |
|---------+-------+-------|
| Control | Ctrl | C |
| Meta | Alt | M |
| Shift | Shift | S |
| Return | Enter | RET |
To create above table in org, we only need to insert
|Name|Key|Abbr.
and press tab
to make new row
and insert -tab
(i.e. dash tab
) to draw the
hline and press tab
. Also you can use (these might be
different in different OS):
M-up/down
to move rowsM-left/right
to move columnsM-S-up/down
to add/remove rowsM-S-left/right
to add/remove columnsWe can use <c>
, <l>
or
<r>
for center, left or right alignment.
To create a TODO list you only need:
** TODO learn org-mode
** DONE learn emacs DEADLINE: <2019-11-16 Sat>
To change a TODO task to DONE or remove it use
S-left/right
. Use C-c C-d
to add a
deadline.
We can use LaTeX to write equations in Org. For example:
#+BEGIN_SRC latex
\begin{equation}
x=\sqrt{b}
\end{equation}
If $a^2=b$ and $b=2$, then the solution must be either $$a=+\sqrt{2}$$ or $$a=-\sqrt{2}$$
#+END_SRC
Org processes LaTeX codes in the HTML/PDF outputs.