Emacs

Emacs is a powerful, extensible and customizable text editor. GNU Emacs is probably the most popular variant of Emacs as it is included in the repositories of many popular GNU/Linux distributions. Emacs runs on several operating systems including GNU/Linux, OpenBSD, FreeBSD, NetBSD, MacOS, MS Windows and Solaris.

Install Emacs on OpenBSD

$ doas pkg_add emacs

Emacs comes with a comprehensive, built-in help system which can be accessed by pressing C-h t; In other words, pressing Control h, followed by t. This starts an interactive tutorial that you can follow, save and continue at a later time.

Emacs cheat sheet

Emacs key bindings are unique, but easy to understand and remember when you become familiar with its terminology. In Emacs C-x means "click on Control and x at the same time". M-x means: "type Alt then x"; M refers to Meta. In most cases the Alt key functions as the Meta key. However on some keyboards there is a real Meta key. In this cheat sheet we use the popular naming of the keys as Alt and Control.

Starting emacs

$ emacs [Enter]

    to start emacs. 

$ emacs filename [Enter]

    to start emacs and load a file 

$ emacs -nw filename [Enter]

    to start emacs with no new window (load file) 

Quitting

[Ctrl]x [Ctrl]c

    quit emacs (Emacs will prompt if any of the files have not yet been saved.) 

[Ctrl]g

    aborts any command in progress 

Help

[Ctrl]h t

    to see the tutorial 

[Ctrl]h a topic [Enter]

    to see help about topic 

[Ctrl]x u

    undo the last command

Cursor positioning and navigation within a file

[Ctrl]f or [>-]

    forward (right) one character. 

[Ctrl]b or [>-]

    back (left) one character 

[Ctrl]p or [^]

    up one screen line 

[Ctrl]n or [v]

    down one screen line 

[Esc]b

    left one word 

[Esc]f

    right one word 

[Ctrl]a

    to beginning of line 

[Ctrl]e

    to end of line 

[Esc]<

    start of document 

[Esc]>

    end of document 

[Esc]v

    page up 

[Ctrl]v

    page down 	

[Ctrl]l

    cursor in middle of screen 

[Ctrl]u 20 [Ctrl]n

    advance 20 lines 

[Ctrl]x w

    display the line number where the cursor is located 

[Esc]x goto-line [Enter] 999 [Enter]

    go to line number 999

Search and Replace

[Ctrl]s dance [Enter]

    search for dance; cursor moves as you type. Press [Enter] once at the correct location 

[Ctrl]r dance [Enter]

    search backwards for dance; cursor moves as you type. 

[Ctrl]s [Enter] [Enter]

    search for the next occurrence 

[Esc]% oldstring [Enter] newstring [Enter]

    Search for oldstring and replace it with newstring. The y key confirms each replacement, n skips it, q to exit 

Regions

[Ctrl]SPC

    set mark at cursor 

[Ctrl]w

    kill region 

[Esc]w

    copy region to kill ring 

[Ctrl]y

    yank back last thing killed 

Loading and Saving

[Ctrl]x [Ctrl]f filename

    create new filename for editing (clears workspace) 

[Ctrl]x [Ctrl]f filename

    load in filename for editing 

[Ctrl]x [Ctrl]w filename

    write (save) as filename 

[Ctrl]x [Ctrl]s

    resave under the current filename (based on above or name given when starting emacs)

Cutting and Pasting

[Ctrl]SPC

     Mark one end of region

[Ctrl]w

     Cut (after you Mark and move to other end)

[Alt]w

     Copy (after you Mark and move to other end)

[Ctrl]y

     Yank (paste) most recently killed (cut or copied); will also use text currently selected in another application.

[Alt]y

     Next most recently killed (do [Ctrl]y first, repeat [Alt]y to cycle thru all)‍