Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
present:
reveal-md . -w --theme solarized --highlight-theme monokai

serve:
bundle exec jekyll serve
148 changes: 148 additions & 0 deletions _posts/2022-09-14-emacs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
layout: post
title: "Emacs Basics"
date: 2022-09-14 09:13:00 +0100
categories: tools
---

## Basics

Emacs is a text editor with command line interface. It allows to edit a text
using only keyboard. You can write text on it as usual and perform "shortcuts"
to perform some actions with the editor and the text.

These "shurtcuts" are called commands.

```
C-x C-c Close the editor.
```
To perform this command you have to press "x" key when CTRL key is hold
and then press "c" key when CTRL key is hold.
In commands first "C-" means CTRL key.
"M-" means Meta key which is usually labeled "alt" or "option".

> NOTE: not all terminals support Meta key.
But it can be emulated by pressing ESC key and then a target key sequentially.
For example for command `M-f` you can press "f" while holding "alt" or you can
type ESC and then press "f".

## Movements

Here is a summary of simple cursor-moving operations,
including the word and sentence moving commands:

```
C-f Move forward a character
C-b Move backward a character

M-f Move forward a word
M-b Move backward a word

C-n Move to next line
C-p Move to previous line

C-a Move to beginning of line
C-e Move to end of line

M-a Move back to beginning of sentence
M-e Move forward to end of sentence

M-< Move to beginning of whole text
M-> Move to end of whole text

C-l Position line with cursor at the center of screen.
If it is at the center, position at the top.
If at the top, position at the bottom.
If at bottom then position at the center and so on.
```

Most of commands be prefixed with `C-u <NUMBER>` for a repeat count.
But `C-v` and `M-v` take it as amount of lines to scroll. E.g. `C-u 10 C-v`
will scroll down 4 lines trying to leave cursor in its place.

```
C-u <NUMBER> <CMD> Perform <CMD> command <NUMBER> times.
E. g. C-u 5 C-n will move to next line 5 times.

M-<NUMBER> <CMD> Same as above. Type <NUMBER> when holding Meta.
```

You can abort current command by `C-g`. If you change your mind after typing
a part of a command you can abort it by `C-g`. Or you can use it to abort long
running command.

So if you type `C-u 100 C-g C-f` it would move cursor forward by 1 symbol
because you cancelled it with `C-g`.

If you type an unsafe command (`C-x C-l` for example) you see a window
to confirm it. This happened because emacs has "disabled" commands for a noob's
safety. Press `n` to cancel it and `<SPC>` (space) to confirm.

## Windows

`C-x 1` kill all windows other than 1.

## Editing

Just type text :)

You can give repat count to a symbol to insert it many times:
Next `<DEL> = backspace`

```
M-3 i produces "iii"
M-3 <DEL> removes 3 symbols
```

Delete text as usual with DEL or Backspace keys. Or...
```
<DEL> delete the character before the cursor
C-d delete the character under (after) the cursor

M-<DEL> kill the word before the cursor
M-d kill the word after the cursor

C-k kill to end of line
M-k kill to the end of sentence
```

> The sentence commands assume that you follow the American typist’s
convention of putting two spaces at the end of a sentence.

```text
C-<SPC> Set mark/Start text selection/Visual mode in Vim
C-w Kill (cut) selected text
C-y Yank (paste) killed text
M-y Yank previous kill and replace paste buffer with text killed earlier
```

In the previous paragraph there were "kill" and "delete" terms. "Kill" is the
same as "delete" but it copies the text.

To undo latest changes type `C-/` or `C-_` or `C-x u`.

## Files

Current opened file name can be found at the bottom of the screen.
To open a file type `C-x C-f` and type a file name. To write changes type
`C-x C-s`.

First time you type `C-x C-s` focus will jump to the bottom screen
mini-buffer and prompt you a file name to write.

When editing and saving file emacs copies unedited version with the same
name suffixed by "~" not to lose it.

## Buffers

You can open one file, then another by typing `C-x C-f <FILENAME>` and so on.
All these files remain open in Emacs and stored in "buffers".

To switch between buffers type `C-x b <BUFFERNAME>`.

Emacs does not save files when you switch current buffer, but you don't have
to switch to the right buffer to save it - use `C-x s` (save unsaved buffers).

## Commands