- Richard Feynman -
Emacs - basics
Emacs - tons of things can be said about Emacs. We will try to keep it short.
Emacs is actually a family of text editors and we will focus on GNU Emacs. GNU Emacs is the Emacs is available in cygwin, GNU/Linux and MacOS (using Homebrew or MacPorts. GNU Emacs is part of the GNU project, a project start in 1983 with the goal of giving computer users freedom.
On this page will focus on how to use our (the authors) editor of choice.
Contents
- 1 Videos
- 2 Emacs concepts
- 3 Uderstanding the key bindings on this page
- 4 Starting, putting to sleep, and leaving
- 5 General commands
- 6 Text management
- 7 File management
- 8 Creating, deleting and moving between windows
- 9 git / VCS integration
- 10 TODO
Videos
Emacs - Basics (Full playlist) | Emacs - Basics - 1 - buffers | Emacs - Basics - 2 - Start pause exit | Emacs - Basics - 3 - Searching | Emacs - Basics - 4 - mark cut paste | Emacs - Basics - 5 - Moving in the buffer | Emacs - Basics - 6 - File management | Emacs - Basics - 6 - Window management
Emacs concepts
buffers
In emacs you edit texts in buffers. If you open a file the text is read into the buffer and you then edit that buffer (rather than the file).
buffer commands
-
C-x b buffername <Enter>
(switch-to-buffer) - switches to buffer buffername. Text completion works nicely her.
-
C-x C-b
- lists existing buffers (list-buffers)
-
C-x k buffer <Enter>
- kills current buffer (kill-buffer)
Read more about buffers in the GNU Emacs manual: Buffers
modes
Every buffer has a mode, typically a file with Java source code has the mode java mode and a C file C mode. The modes helps you with indentation, highlightning and more.
autosave
Emacs autosaves your buffers in sepcial. Such autosave files are saved with ~
at the end, e g Student.java~
.
Uderstanding the key bindings on this page
With emacs, and most other editors, you can bind keys to a command. We need to define how we write these keybindings.
special keys
-
C
is an abbreviation for theCtrl
key
-
M
is an abbreviation for theM
. This refers to the meta key which does not exist on all keyboard and thus a new key (or many) are bound to meta instead. You can (most likely) useEsc
orAlt
key instead
-
<RET>
is an abbreviation for theEnter
(or Return) key
-
<SPC>
is an abbreviation for theSpace
key
key combination
-
c-x
- means press control and x (both keys pressed) and then release
-
M-x
- means press Meta and x (both keys pressed) and then release
-
M-x s
- means press Meta and x (both keys pressed) and then release, followed by pressings
Starting, putting to sleep, and leaving
Running Emacs in a terminal
We run Emacs in a terminal window. This way we can toggle between bash (also running in the terminal) and emacs in the same window. So fire up a terminal runnig bash and type:
emacs
... in case you wonder, you can of course run bash from emacs. Press meta-x
followed by enter
and then type shell
and type enter
.
Emacs under X on GNU/Linux (and other)
If you're running emacs in GNU/Linux you may have installed a graphical version of Emacs. We're assuming you don't want this, so in order to get Emacs in your terminal you to use the option -nw
is short for --no-window-system
which on GNU/Linux makes Emacs not communicate with X, ignoring $DISPLAY. This means in short that Emacs will run in your terminal.
Alias emacs to emacs -nw
Add the following line:
alias emacs='emacs -nw'
in the file:
-
.bashrc
in your home folder - on GNU/Linux and cygwin -
.bash_profile
in your home folder - on MacOS
This sets up an alias in bash to run emacs -nw
when you type emacs
.
Starting emacs and open a file
You can start Emacs with a file as argument. If the file exists, Emacs opens it. Otherwise Emacs creates a buffer for you with which you can save the content under the file name.
Let's say you want to open an existing file called se/juneday/StupidExample.java
, then you start Emacs like this:
emacs se/juneday/StupidExample.java
Pausing emacs
C-z <RET>
- pause emacs
You can put Emacs to sleep if you want to use bash for a while. This is useful if you're writing code when you typically switch between editing and compiling. To pause emacs press C-z <RET>
.
fg
- resume emacs
To get back (wake up) to emacs type (in bash) fg
. fg
is an abbreviation for foreground. This is a simple example and may not work if you have many "background jobs" in bash but if you switch between bash and emacs it should work fine.
If you're in bash and want to see what processes have been put to sleep, you can type jobs
. You'll then probably get a printout like this:
$ jobs
[1]+ Stopped
Exit Emacs
You exit Emacs by typing: C-x C-c
.
General commands
-
C-_
- undo the latest change (press many times and you'll undo more changes)
-
C-g
- quit current command.
Text management
The keys Delete
and Backspace
work as you'd expect (delete a character). You can however make these keys delete words instead of a character by using the M
in combination, e g M-<DEL>
deletes a word.
search for string
C-s
- search for a string
You use it by typing C-s
followed by the string you want to search for.
Extra features:
- pressing
C-s
one more time finds the next occurance. The search wraps around the file (begins from the beginning if the end is reached)
- place your marker at the beginning of one word, press
C-s
and thenC-w
. Emacs now searches for the word.
Actual Emacs command: isearch-forward
If you'd like to read more about searching Incremental search in GNU Emacs manual.
C-r
- searches for a string backwards
You use it by typing C-r
followed by the string you want to search (backwards) for.
Extra features:
- pressing
C-r
one more time. The search wraps around the file (begins from the end if the beginning is reached)
- pressing
C-s
a forward search is performed.
Actual Emacs command: isearch-backward
If you'd like to read more about searching Incremental search in GNU Emacs manual.
mark/cut/paste text
mark region
You mark a region by:
- putting the marker in the position where you want the section to begin
- press
C-<SPC>
(alt.C-@
) to set the mark (and activate it). - move your marker to the end of the region you want to mark
Now you're region is marked.
Actual Emacs command: set-mark-command
If you'd like to read more about marking region Mark in GNU Emacs manual.
C-w
- cut (kill) region
If you have a region marked (see above) you can cut it by pressing either:
-
delete
(or backspace) - to simple cut the text and throw it away "for ever"
-
C-w
to cut the region and store it (in the so calledkill-ring
) for later retrieval. Actual Emacs command:kill-region
M-w
- copy (kill/save) region
If you have a region marked (see above) you can copy it by pressing M-w
to copy the region and store it for later retrieval. Actual Emacs command: kill-region-save
C-k
- cut (kill) line
To cut a line and store it for later retrieval you can type C-k
. The text is stored in the so called kill-ring
so you can reuse it later.
Extra features:
- if you're not at the beginning of the line Emacs cuts the remaining part of the line (from the marker) but leaves the newline
- press
C-k
one more time and emacs cuts the newline, and then (pressingC-k
again) the next line ....
- if you want to cut the entire line but the marker is not in column 0, you can being by pressing
C-a
which takes you to the first column
Actual Emacs command: kill-line
C-y
- paste text (yank)
If you have some text in the kill-ring
(typically by cuting a line or region) you can paste it (yank) using C-y
.
Extra features:
- if you press
C-y
many times the same text is copied equally many times
Actual Emacs command: yank
If you want to read more about yanking (pasting), check out Yankning in the Eamcs manual.
M-y
- paste text (yank)
If you want some other text you cut (killed) earlier than the last you can use M-y
which takes you through the history of previous kills.
Actual Emacs command: yank-pop
M-%
- query replace
To replace a string with another one you can use M-% string <RET> newstring <RET>
. Let's give an example, say you want to replace "foo" with "bar":
-
M-%
-
foo
- <Enter>
-
bar
- <Enter>
Then you answer yes or no for each occurance, or !
if you want to replace all occurances.
If you want to read more about query replace, check out Query replace in the Eamcs manual. Check out the command C-M-% regexp <RET> newstring <RET>
which can substitute text using regular expression instead of plain strings.
Actual Emacs command: query-replace
M-/
- text completion
To expand the word (where your marker is) to a word found in the buffer you can use M-/
. Let's say you have the following piece of code:
public Person(String name, String email) {
this.na
}
If you place the marker after this.na
and press M-/
(Meta-Shift-7
on a Swedish keyboard) you will the letters na
completed to name
.
Actual Emacs command: dabbrev-expand
Move around
Here are a couple of commands (and key bindings) to quickly move around in a buffer. If you want to read further, checko out Moving point in the Emacs manual. Moving around with the arrow keys and Pgup/PgDown work as you expect them to.
-
M-<
- move to the top of the buffer
-
M->
- move to the bottom of the buffer
-
c-f
- move forward one character - f as in forward (forward-char)
-
c-b
- move backward one character - b as in backward (backward-char)
-
M-f
- move forward one word - f as in forward (forward-word)
-
M-b
- move backward one word - b as in backward (backward-word)
-
c-p
- move up one line - p as in previous (previous-line)
-
c-n
- move down one line - n as in next (next-line)
-
c-a
- move to the beginning of the line (move-beginning-of-line).
-
c-e
- move to the end of the line (move-beginning-of-line).
-
M-g g
- go to line xx. You will be prompted for the line number to move the marker to (goto-line).
File management
C-x C-f
- open file
To open a file (either existing or prepare for a new file) press C-x C-f<code> filename <code><Enter>
.
Actual Emacs command: find-file
C-x C-s
- save buffer
To save a buffer a file (either existing a new file) press C-x C-s
.
Actual Emacs command: save-buffer
Other save commands can be found here: Save commands (GNU Emacs manual).
C-x C-w
- write buffer to file
To save a buffer to a file (give a new name) C-x C-w<code> new-filename <code><Enter>
.
Actual Emacs command: write-buffer
Other save commands can be found here: Save commands (GNU Emacs manual).
Creating, deleting and moving between windows
-
C-x 0
- delete the selected window (delete-window)
-
C-x 1
- use only one window, keep the current buffer in the only window (delete-other-windows)
-
C-x 2
- split the current window in two by creating a new windows below (split-window-below)
-
C-x 3
- split the current window in two by creating a new windows to the right (split-window-right)
-
C-x o
- move (the marker) to the next window (other-window)
If you want to read more about windows, check out: Change window, Other window, split window.
git / VCS integration
We're recommending our students to use git so we're going to use git terminology here, but the commands work with "all" VCS toolswe've used.
Add and/or commit a file
C-x v v
- perform next VCS action (vc-next-action)
If you're file has not been registered (git add
) in git Emacs will register it for you using this command. Emacs does not however commit (after all the command really is vc-next-action so this makes sense) so you will have to do C-x v v
again to commit.
Emacs prompts you for a commit message in a separate window. Enter text text and press C-c c
Pull
C-x v +
- push(vc-update)
To pull a repository you type C-x v
.
Push
C-x v P
- push(vc-next-action)
To push a file you type C-x v P
(on a swedish keyboard C-x v Shift-p
).
TODO
must
Perhaps
- transposing
- emacs-server / emacs-client (really???)
- macro
- record
- play
- diff
-
.emacsrc