Emacs - hpaluch/hpaluch.github.io GitHub Wiki

Emacs notes

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 as Alt
  • S - Shift

Commands

  • M-x + write command name + ENTER, for example info will open Info help viewer.
  • M-! run shell. Warning: on my keyboard I have to use Right Alt-Shift-1 key (because Left Alt+Shift switches keyboard layouts on my machine)

Emacs modes

Even Emacs has so called "Modes" where different hot-keys. Current mode name is shown as last expression in standard brackets, for example:

These are "Major modes" specific to some file format.

Note many modes are described on: https://www.gnu.org/software/emacs/manual/

Info mode commands

  • 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 (capital L - 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 marked Prev: on top bar (NOT in history!)
  • u - Up node in navigation
  • l - go to previous node in history

Files

  • 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

Buffers are containers holding content. Buffer name is surrounded with asterisks (*).

Important keys:

  • C-x LEFT-ARROW or C-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:

  1. go to start position of region and press C-SPACE
  2. go to end of region and
  3. press M-w to just copy it to kill buffer
  4. 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. with C-w (Control instead of Alt)
  • you can also "kill text to the end of line with C-k instead of C-w

Undo:

  • C-/

Windows

Windows are basically split regions of screen.

Keys:

Note: some functions will happily open another window without asking.

Working with etags

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 ; inside for() 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:

  1. 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
  2. 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 press M-. to see location of its definition.
  • issue: when I tried puts(3) it was not found....

Compiling program:

  • press M-x and type compile 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 type shell 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

Gotchas!

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 example printf) - press enter to move cursor to it and Copy it. Or press C-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

Rescue

  • when stuck in prompt try C-g as Escape
  • when editing text use C-/ as Undo

Just studying

Interesting (Org is "outline" mode for big text documents, may also include TODO list, tags and other funnies) activated with .org extension of file:

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

More resources

You may find also helpful Emacs Reference cards on:

⚠️ **GitHub.com Fallback** ⚠️