Home COMSC-171 <- Prev Next ->

emacs

emacs (Editor Macros) is a full-featured, programmable text editor written in a version of Lisp. The user interface is designed to work on character terminals, though a GUI is also supported.

emacs provides "A wide range of functionality beyond text editing, including a project planner, mail and news reader, debugger interface, calendar, IRC client, and more." GNU

Many lightweight clones exist, including uemacs (Micro Emacs) and mg

jargon

buffer
memory area containing text being edited
point
cursor location in a buffer
mark
saved location in a buffer
region
characters between the mark and the point (can be operated on by many commands)
kill ring
multi-level structure which stores deleted, cut, or copied text longer than 1 character

screen

window
shows a portion of the text in a buffer
mode line at the bottom shows status information
minibuffer
last line, shows messages and provides space for typing commands

commands

Commands are entered using Ctrl and Alt in different ways:

Ctrl keys
Alt keys
docs call these Meta keys, may be something other than Alt
alternatively hit and release Esc, then hit the following key
Ctrl combinations, commonly beginning with Ctrl+x
hit and release the Ctrl key, then hit the following key(s)
Alt+x command-name
for commands not mapped to keystrokes

configuration

cat > .emacs # create configuration file (optional) (define-key global-map "\C-H" 'backward-delete-char) # override help (setq make-backup-files nil) # turn off auto backup (setq column-number-mode t) # show column numbers (setq scroll-conservatively 1) # scroll by line instead of by half screen (setq inhibit-startup-screen t) # no startup screen (electric-indent-mode 0) # disable new indentation feature

startup

cp /etc/passwd pass1 # make a working copy you can write cp /etc/group group1 # make a working copy of another file cp /etc/fstab fstab1 # make a working copy of another file emacs pass1 # start emacs and load pass1

cursor movement

Ctrl+n # next line Ctrl+f # forward (right) Ctrl+p # previous line Ctrl+b # back (left) Ctrl+e # end of line Ctrl+a # beginning of line Alt+m # first nonblank character on line (move) Ctrl+v # next screen Alt+f # forward 1 word Alt+b # back 1 word Alt+v # previous screen Alt+> # end of buffer Alt+< # beginning of buffer

incremental search

Alt+< # move to top of buffer Ctrl+s # search forward 1 # find next 1 0 # find next 10 Ctrl+s # find next 10 (repeat search) 0 # find next 100 Enter # terminate search, leave cursor where it is Ctrl+r # reverse search 1 # find previous 1 Ctrl+r # find previous 1 (repeat search) 0 # (zero) find previous 10 Bksp # remove last character from search, return to last 1 found 1 # (one) find previous 11 Ctrl+g # abort search, or any uncompleted command (may need to hit twice)

delete

Ctrl+a Ctrl+d # delete character at cursor Ctrl+e Bksp # delete character left Ctrl+n Ctrl+a Ctrl+k # delete to end of line Ctrl+k # delete blank line Alt+d # delete word Alt+3 Ctrl+f # forward 3 spaces Ctrl+k # delete to end of line Ctrl+k # nonblank line not deleted, but line below joined

command repetition

Alt+2 Ctrl+n # cursor down 2 lines

cut, copy, paste

Ctrl+a Ctrl+k Alt+5 Ctrl+n # down 5 lines Ctrl+k Alt+5 Ctrl+n Ctrl+y # yank (paste) last delete Ctrl+y # yank same delete again Alt+y # yank previous delete Alt+20 Ctrl+f # right 20 spaces Ctrl+Space # (or Ctrl+@ or Alt+x set-mark) set mark Alt+5 Ctrl+n # down 5 lines Ctrl+w # delete (cut) from mark to character left of cursor Alt+< Ctrl+y # yank last delete Alt+x set-mark Alt+5 Ctrl+n Alt+w # copy from mark to character left of cursor Ctrl+y # yank last copy Ctrl+a Alt+x set-mark Alt+5 Ctrl+n Alt+20 Ctrl+f Ctrl+x Ctrl+x # exchange mark and point Ctrl+x Ctrl+x Ctrl+x r k # kill rectangle defined by mark and character left of cursor Ctrl+x r y # yank rectangle, replacing what you just killed Alt+5 Ctrl+n Alt+20 Ctrl+f Ctrl+x r y # yank rectangle again

recenter

Alt+10 Ctrl+n Ctrl+l # recenter (and redraw) screen

input

# type the following lines and hit Enter after each: line one line two Insert # (or Alt+x overwrite-mode) overwrite mode foo # type these characters Insert # (or Alt+x overwrite-mode) toggle back to insert mode bar # type these characters

undo

Ctrl+x u # undo Ctrl+x u # undo again (multi-level)

replace

Alt+< Alt+x replace-string : Enter # search string # Enter # replacement string Alt+< # move to top of buffer Alt+x replace-regexp h$ Enter # search regex H Enter # replacement string

macros

Alt+< Ctrl+x ( # start macro definition (double space) Ctrl+e # end line Enter # new line Ctrl+n # next line Ctrl+x ) # end macro definition Ctrl+e # execute macro e # repeat macro

buffers

Ctrl+x Ctrl+f group1 # find file (opens new buffer) Alt+5 Ctrl+k # delete five lines Ctrl+x b pass1 # switch buffer Ctrl+y # paste Ctrl+x Ctrl+f fstab1 Ctrl+k # kill buffer

windows

Ctrl+x 2 # split screen top/bottom Ctrl+x b pass1 # switch buffer Ctrl+v # next screen Ctrl+x o # (lowercase O) other window Alt+v # previous screen Ctrl+x 0 # (zero) delete window Ctrl+x 3 # split screen side-to-side Ctrl+x b group1 # switch buffer Ctrl+x o # other window Ctrl+x 0 # (zero) delete window (1 deletes all other windows)

read-only

Ctrl+x Ctrl+q # toggle on foo # type these characters, all you see is an error message Ctrl+x Ctrl+q # toggle off foo # type these characters, OK now

files

Ctrl+x i group1 # insert file contents Ctrl+x Ctrl+s # save Ctrl+x Ctrl+w pass2 # write (save as) Ctrl+x Ctrl+c # save and exit rm fstab1 group1 pass1 pass2 # clean up

tutorial

Ctrl+h t # run within emacs