Emacs - hpaluch/hpaluch.github.io GitHub Wiki
After many years using Vim I decided that I should also try Emacs to see its pros and cons.
Shortcut modifiers:
-
C
-Control
-
M
- Meta - today known asAlt
-
S
-Shift
-
M-x
+ write command name + ENTER, for exampleinfo
will open Info help viewer. -
M-!
run shell. Warning: on my keyboard I have to use RightAlt
-Shift
-1
key (becauseLeft Alt
+Shift
switches keyboard layouts on my machine)
Even Emacs has so called "Modes" where different hot-keys. Current mode name is shown as last expression in standard brackets, for example:
-
(Info Narrow)
- Info viewer mode -
(C/*l Abbrev)
- when editing*.c
file, commands can be found on https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Commands -
(Fundamental)
- plain text -
(nXML Valid Fill)
- when editing*.xml
file
These are "Major modes" specific to some file format.
Note many modes are described on: https://www.gnu.org/software/emacs/manual/
-
d
return to Top Directory node (list of all Info files) - TAB - next menu item
- S-TAB - previous menu item
- ENTER - go to node
-
L
(capitalL
-Shift
+l
) - list of visited nodes -
<
- go to 1st node in current manual -
m
+ menu item name - quick search and go -
n
- Next node in navigation -
p
- go to node markedPrev:
on top bar (NOT in history!) -
u
- Up node in navigation -
l
- go to previous node in history
-
C-x C-f
- will be asked for filename - existing will be open, not-existing created. You can use famous TAB for filename expansions -
C-x C-s
- Save -
C-x C-w
- Save as -
C-x C-c
- save and Exit (will not ask if files are saved).
Buffers are containers holding content. Buffer name is surrounded with
asterisks (*
).
Important keys:
-
C-x LEFT-ARROW
orC-x RIGHT-ARROW
- select previous/next buffer -
C-x C-b
- list of buffers. you can use arrows and press ENTER when on buffer name to select that buffer -
C-x k
kill active buffer - you will be asked to confirm name of buffer to kill
Copy & Paste:
- go to start position of region and press
C
-SPACE
- go to end of region and
- press
M
-w
to just copy it to kill buffer - go to target position (where you want to copy text)
and press
C
-y
to Paste (yank).
Cut & Paste
- just replace
M
-w
in step 3. withC
-w
(Control instead of Alt) - you can also "kill text to the end of line with
C
-k
instead ofC
-w
Undo:
-
C
-/
Windows are basically split regions of screen.
Keys:
-
C-x o
(small letter "ou") - Selects another Window, from https://ftp.gnu.org/old-gnu/Manuals/emacs-20.7/html_node/emacs_162.html -
C-x 0
(number zero) - closes current Window from https://stackoverflow.com/a/17961482 -
C-x 1
(number one) - close all Windows but keep active Window only.
Note: some functions will happily open another window without asking.
etags
command is use to create database of keywords "tags" and their position in files
(headers or source modules)...
Main idea based on: https://condor.depaul.edu/glancast/443class/docs/etags.html
Let's have simple C program hello.c
:
#include<stdlib.h>
#include<stdio.h>
int main(int argc, char **argv)
{
printf("Hello, world!\n");
return EXIT_SUCCESS;
}
And Makefile
:
CFLAGS := -Wall
hello: hello.c
To find system functions (for example printf
) I did:
# -a is for "append to existing tags"
find /usr/include/ -name '*.h' -exec etags -a {} \;
Now you open hello.c
simply with:
emacs hello.c
You can close Tutorial Window with C-x 1
(buffer with
hello.c
should be active if not - change active Window using C-x o
).
Special navigation keys (see C
menu on X11 compatible Emacs):
-
M-e
move to next command -
M-a
move to previous command - above commands are a bit primitive (thinking that
;
insidefor()
is also command separator)
To find where is printf
:
- move cursor to
printf
- press
M-.
(Alt + Dot) - you may be asked, what Tag file should be used - press ENTER for detail
TAGS
- if there is question "File TAGS is large" - confirm
y
Now there are two options:
- More than one location found - you will see
XREF
Window. In such case you can- move cursor to any line and press ENTER to open that file
- you will directly jump to target file with only occurence
In both case you can then use:
- if there is more than 1 Window use
C-x 0
(zero) to close active Window (Window with file containing tag or XREF buffer) - if there is only one Window you have to press
C-x k
to kill current buffer and return to main source Window.
Other try:
- similarly you can move cursor to
EXIT_SUCCESS
and again pressM-.
to see location of its definition. - issue: when I tried
puts(3)
it was not found....
Compiling program:
- press
M-x
and typecompile
and ENTER - remove
-k
from arguments (-k
means - do not abort build on error) and again press ENTER - you should see build results.
Simple way to run program:
- pres
M-x
and typeshell
ENTER - press
M-!
- you need to actually press Alt-Shift+1 (in my case exactly in this order) - type name of program
./hello
to run and ENTER
I'm used to just press C-s
for Save (from GUI applications), but it is forward search in Emacs!
- so if you accidentally press just
C-s
you can specify forward search (for exampleprintf
) - press enter to move cursor to it and Copy it. Or pressC-g
to abort search. - I have to remember to press
C-x C-s
to really save file.
Text Emacs (no GUI)
- to get menu use Alt-backtick (top-left key)
- to open Emacs info manual:
C-h r
- when stuck in prompt try
C-g
as Escape - when editing text use
C-/
as Undo
Interesting (Org is "outline" mode for big text documents, may also
include TODO list, tags and other funnies) activated with .org
extension of file:
-
Emacs | How To Create Beautiful HTML Documents With Org Mode
: https://www.youtube.com/watch?v=LS0JMOdQw-s - Spelling:
- https://www.gnu.org/software/emacs/manual/html_node/emacs/Spelling.html
- note "word underlining" is called "flyspell", however you will not see spelling errors until you move cursor over specific location
- HTML Mode in Org: manual: https://orgmode.org/manual/HTML-Export.html
- Org manual: https://orgmode.org/manual/Link-Format.html
- HTML Major mode: https://www.gnu.org/software/emacs/manual/html_node/emacs/HTML-Mode.html
- I like entering single left quote with:
C-c C-n BACKTICK
(BACKTICK is simple left quote character) - closing current tag:
C-c /
- I like entering single left quote with:
- changing case: https://www.gnu.org/software/emacs/manual/html_node/emacs/Case.html
- NOTE - there are two different terms:
- "all upper case" will do "ALL UPPER CASE" (upper case all selected letters)
- "capitalize" will do "Capitalize" (upper case only 1st letter of each word)
- NOTE - there are two different terms:
Most important inline formatting commands from https://orgmode.org/manual/Emphasis-and-Monospace.html
You can make words ‘*bold*’, ‘/italic/’, ‘_underlined_’,
‘=verbatim=’ and ‘~code~’, and, if you must, ‘+strike-through+’.
HTML Metadata are defined as "properties" from https://orgmode.org/manual/Property-Syntax.html. Modified Example from video tutorial:
#+TITLE: Beautiful HTML tutorial
#+AUTHOR: Human
* Basic formatting
Testing /emphasis/, *bold* and _underline_.
* Exporting
Press ~C-c C-e~ (for Export) and then `h` (to HTML) and `o` (output
and open File)
* Structured blocks
Testing C block:
#+begin_src c
#include<stdlib.h>
#include <stdio.h>
printf("Hello, world!");
#+end_src
#+begin_quote
It is HTML quote
#+end_quote
You may find also helpful Emacs Reference cards on: