What is the Emacs command on Linux

Emacs Basics - an overview for beginners

Lecture at the Graz Linux Days 2003

Stefan Thalauer

When:
Friday, April 25, 2003 6:00 p.m.
Where:
FH Joanneum - Alte Poststraße 149, 8020 Graz, IT Laboratory 4

content

Emacs basics, editing files, search and replace operations, buffers and windows, Emacs as a working environment, emails and Usenet news (Gnus), creating texts (HTML / XML, LaTeX), writing macros, configuring Emacs, Emacs for programmers (C and C ++, Java).

Duration:
approx. 1 h
Level:
Beginners and advanced

To start the presentation, please F11 to press. (For Opera web browser users)

Further information on the Graz Linux Days can be found at http://www.linuxtage.at.

Overview

  1. Emacs basics
  2. Edit files
  3. Search and replace operations
  4. Buffer and Window
  5. Emacs as a working environment
  6. Wildebeest (news and mail)
  7. Create texts (HTML / XML, latex)
  8. Macros
  9. Configuration of the Emacs
  10. documentation

introduction

This introduction refers to the GNU Emacs (version 21.x)

"Emacs is the extensible, customizable, self-documenting real-time display editor."

A first version was written by Richard Stallman in 1975

Emacs stands for:

  • Editing MACroS
  • Extensible MACro System
  • Esc Meta Alt Control Shift
  • Eight Megabytes And Constantly Swapping
  • Emacs Makes AComputer Slow

Conventions

  • Emacs commands consist of a modifier followed by one or more characters.
Modifierbutton
CONTROLC.CTRL
METAM.ALT (ESC)
SHIFTS.Shift
(none>DELBACKSPACE.
  • All Emacs commands also have a full name (e.g .: keyboard-quit)
  • To execute commands by their full name:
    M-xcommand-nameRETURN
    M-x yow RETURN

Meaning of the modifiers

C-g
The CTRL Button and G pressing simultaneously.
ESC x
The ESC Press the key, release it and then x to press.
M-x
The OLD Button and x pressing simultaneously.
M-C-s
The CTRL Button, OLD Button and s pressing simultaneously.

ESC x is synonymous to M-x.

Emacs basics

Files and buffers

  • Editors do not directly edit the current file.
  • Content is loaded into a temporary buffer.
  • The file is only changed when it is saved!
  • Buffer have names.
  • Usually the same as the file.
  • There are buffers which are not assigned to any file.

Start

To start Emacs:

thalze @ glt03: ~> emacs

When starting, the file to be edited can be specified:

thalze @ glt03: ~> emacs

To start Emacs without X support:

thalze @ glt03: ~> emacs -nw

Emacs commands

  • All Emacs commands are LISP expressions.
  • Assignment between command and shortcut: Binding
  • Most frequently used commands: C-n (n = Character)
  • Less frequently used commands: M-n
  • Relatively frequently used commands: C-xkeys (keys = one or more characters)
  • Special commands: C-ckeys (keys = one or more characters)
  • Not often used commands: no binding (M-x command-name RETURN
  • You can define shortcuts yourself!

Modes

  • There are different modes
  • Behavior changes depending on the mode
  • One buffer can only one Have major fashion
  • However, a buffer can have several minor modes

Major modes

Fashionfunction
Fundamental fashionDefault mode; no special functionality
Text fashionTo write texts.
Message modeTo write mails and Usenet posts.
HTML modeTo write HTML documents.
XML modeTo write XML documents.
LaTeX modeTo write LaTeX documents.
C modeTo write C programs.
C ++ modeTo write C ++ programs.

Minor modes

Fashionfunction
Auto-fill modeAutomatic pagination
Overwrite modeOverwrite
Auto-save modeAutomatic saving at certain intervals.
Line number modeDisplay of the current line number
Transient mark modeHighlighting regions
Abbrev modeTo use abbreviations.
Flyspell modeAutomatic spell check

Open a file

  • To open a file: C-x C-f
  • If the wrong file was opened: C-x C-v
    to open alternative file.
  • Emacs supports: tab completion.
  • To insert a file: C-x C-i

Default Directory

  • The current directory is always used as the default directory when it is opened.

save files

  • To save files: C-x C-s
    in the mini buffer
  • To save the buffer under a different file name:
    C-x C-w

Quit Emacs

  • To quit Emacs: C-x C-c
  • If changes have not yet been saved:
    Emacs asks if you want to save:
    • y (yes) Emacs will save and exit.
    • n (no) Emacs does not save and asks if you really want to exit despite changes.

Summary

File handling commands

Keystrokescommandeffect
C-x C-ffind-fileLoad file in a buffer.
C-x C-vfind-alternative-fileLoad alternative file into buffer.
C-x C-iinsert-fileInsert file in buffer.
C-x C-ssave-bufferSave file.
C-x C-wwrite-fileSave the buffer under a different name.
C-x C-csave-buffers-kill-emacsQuit Emacs.

Edit files

  • navigation
  • Text delete, cut and copy
  • Format paragraphs
  • Tricks and Shortcuts

Cancel and Undo

  • Commands can be used with C-g canceled!
  • Changes can be made with C-_ (M-x undo) can be undone.
  • To load the last saved version of a file:
    M-x revert-buffer RETURN

navigation

There are many different ways to navigate files.

  • With the arrow keys
  • With commands:

Navigation is best in the tutorial (C-h t) to practice!

Navigation commands

Keystrokescommandeffect
C-fforward-charGo one sign (right).
C-bbackward-charGo back one character (left).
C-pprevious-lineGo to the previous line (up).
C-nnext-lineGo to the next line (down).
M-fforward-wordGo one word further.
M-bbackward-wordStep back a word.
C-abeginning-of-lineGo to the beginning of this line.
C-eend-of-lineGo to the end of this line.
M-eforward-sentenceGo back one sentence.
M-abackward sentenceGo one sentence further.

Further navigation

Keystrokescommandeffect
M-}forward-paragraphGo one paragraph further.
M- {backward paragraphGo back one paragraph.
C-vscroll upGo one screen down.
M-vscroll downGo back one screen.
M- <beginning-of-bufferGo to the beginning of the file.
M->end-of-bufferGo to the end of the file.
(none)goto-lineGo to line n.
C-lrecenterCurrent line in the middle.
M-ndigit-agrumentRepeat the next command n-times
C-u nuniversal argumentRepeat the next command ntimes (4 times if no n specified).

Clear

  • There are several methods to delete
  • The easiest: DEL (Backspace)
  • Deletion of words, sentences, paragraphs possible
  • Delete lines
  • Deleting regions

Re-insert what has been deleted

  • kill-ring saves what is deleted.
  • Deleted items can with C-y (yank) be reinserted.
  • It can be navigated through the kill ring (M-y).
    Only after the previous one yank possible.

Cut and copy

  • Regions can be defined.
  • Mark defines a boundary of a region.
  • A mark is made with C-SPACE set.
  • At the current position (point):
    • Region to be deleted (C-w).
    • Region to be copied (M-w).
    • Mark and Point are exchanged (C-x C-x).

Delete commands

Keystrokescommandeffect
CDdelete-charDelete characters under the cursor.
DEL (backspace)delete-backward-charDelete previous character.
M-dkill-wordDelete next word.
M-DELbackward kill wordDelete previous word.
C-kkill-lineDelete line from cursor
M-kkill-sentenceDelete next sentence.
C-x DELend-of-bufferDelete previous sentence ..
C-yyankPaste the last deleted one.
C-wkill regionDelete selected text.

Format paragraphs

  • Paragraphs and regions can be formatted.
Keystrokescommandeffect
M-qfill-paragraphReformat the paragraph.
(none)fill regionReformat the region.
C-x hmark-whole-bufferMark the entire file.

Overwrite

  • Overwrite: overwrite mode
  • To activate and deactivate:
    • INSERT (Insert)
    • M-x overwrite-mode RETURN

register

  • Regions and positions can be saved in registers.
  • The name of the register is a character.
Keystrokescommandeffect
C-x r scopy-to-registerCopies region to register.
C-x r iinsert-registerInserts the contents of the register.
C-x r SPACEpoint-to-registerSaves current position in register.
C-x r jjump-to-registerGo to the location saved in the register.

Tips and Tricks

Exchange of:

Keystrokescommandeffect
C-ttranspose-charsSwaps two characters.
M-ttranspose-wordsSwap two words.
C-x C-ttranspose-linesSwaps two lines.
(none)transpose-sentencesSwap two sentences.
(none)transpose-paragraphsSwap two paragraphs.

Capital letters

Keystrokescommandeffect
M-ccapitalize-wordFirst letter capitalized.
M-uupcase-wordCapitalized word.
M-ldowncase-wordLower case word.

Search and replace operations

Search

  • Simple search
  • Incremental search
  • Word search
  • Regular expression search
  • Incremental regular expression search

Replace

  • Simple search and replace
  • Query-replace
  • Regular expression replace

Incremental Search

  • Searches for each character entered
Keystrokescommandeffect
C-sisearch-forwardIncremental forward search
C-risearch-backwardIncremental reverse search.
RETURN(none)End search.
C-gkeyboard-quitCancel search.
DEL(none)Delete characters of the search word.
C-s C-w(none)Search with the word under the cursor.
C-s C-y(none)Search with text from cursor position to the end of the line.
C-s M-y(none)Search with text from the "kill ring".
C-s C-s(none)Repeat the last search.

More searches

Simple search

Keystrokeseffect
C-s RETURN
searchstring RETURN
Simple search forward
C-r RETURN
searchstring RETURN
Simple search backwards

Word Search

Keystrokescommandeffect
C-s RETURN C-wword-search-forwardWord Search Forward
C-r RETURN C-wword-search-backwardWord search backwards

Find Replace

Easy search and replace

  • Replace word
  • M-x replace-string RETURN
  • From the current cursor position, all words are replaced.
  • Replaced occurrences

Query-Replace

  • If not every occurrence of a search term should be replaced.
  • When it occurs, it is asked whether it should be replaced.

Query-replace interaction

Keystrokeseffect
M-%Start query replace
SPACE or yReplace and move on.
DEL or nDon't replace and move on.
.Replace and quit.
,Replace and go no further.
!Replace others without asking.
RETURN or qEnd query replace.
?Help.

Repeat operations

Keystrokescommandeffect
C-x ESC ESCrepeat-complex-commandRepeat complex operation
  • To go to the previous command: M-p
  • To go to the next command: M-n

Repeating complex operations does not only work with query replace!

Recursive editing

  • If something should also be changed with query-replace.
  • Query-replace can be interrupted.

Recursive editing during query replace

Keystrokeseffect
C-rStart recursive editing.
C-wDelete word and start recursive editing.
M-C-cEnd recursive editing and continue "query-replace".
C-]End recursive editing and "query-replace".

Is Search "Case-Sensitive"?

  • Case-sensitive (foo! = Foo)
  • Case-insensitve (foo == Foo) [ Default ]
  • Capital letters in search:
    --> Case-sensitive
(setq-default case-fold-search nil) ;; * Non-nil if searches and matches should ignore case. (setq-default case-replace nil) ;; * Non-nil means `query-replace 'should ;; preserve case in replacements.

Regular expressions

Sometimes normal search is not enough.

Then search with regular expressions.

characteraccordance
^Beginning of a line.
$End of line
.A single character (how?)
.*Zero or more characters (such as *, wildcard)
\<Beginning of a word
\>End of a word
[]Characters between the brackets. (e.g .: [a-z])
\Escape character (e.g .: \. For point)

Regular Expression Search

Keystrokescommandeffect
M-C-s RETURNre-search-forwardSearch forward on Regular Expression.
M-C-r RETURNre-search-backwardSearch backwards for regular expression.
M-C-sisearch-forward-regexpSearch incrementally backwards for regular expression.
M-C-risearch-backward-regexpSearch incrementally backwards for regular expression.
(none)query-replace-regexpFind replace a regular expression.

Spell check

  • Spell checker works with dictionaries.
  • Proper names are not recognized.
  • There is also the option of using your own dictionaries.
  • Cannot replace proofreading. (again vs. against)

Ispell

It is possible to check:

  • Buffer
  • region
  • Email or newspaper posting

Use Ispell

To check a buffer: M-x ispell-buffer RETURN

If all words are correct:

Keystrokescommandeffect
M- $ispell-wordCheck the word under the cursor.
(none)ispell regionCheck all words in the region.
(none)ispell-bufferCheck all words in the buffer.
C-u M- $ispell-continueContinue Ispell.
M-TABispell-complete-wordIn text mode: show possible completions of the current word.
(setq ispell-dictionary "english")

Flyspell

  • Automatically checks your spelling when you write.
  • Uses Ispell.
  • Is activated with: M-x flyspell-mode RETURN
  • Misspelled words are highlighted. (underlined)

To activate the Flyspell mode in general for a mode:
(e.g .: LateX Mode)

(add-hook 'LaTeX-mode-hook' flyspell-mode)

Word Abbrevation Mode

  • It is possible to define abbreviations.
  • The full meaning is inserted for an abbreviation.

Example:

  • Often you have to Graz Linux Days 2003 write.
  • The abbreviation is defined for this: glt03
  • It will then be used each time glt03Graz Linux Days 2003 inserted.

Use Abbrevation Mode

  1. .emacs (setq-default abbrev-mode t) (read-abbrev-file "~ / .abbrev_defs") (setq save-abbrevs t) (setq flyspell-abbrev-p nil)
  2. .emacs save and evaluate.
  3. Define Abbrevation (C-x a - or C-x a i l)
  4. M-x write-abbrev-file RETURN
  5. ~ / .abbrev_defs RETURN

Word Abbreviation Commands

Keystrokescommandeffect
(none)abbrev modeStart (exit) Word abbreviation mode.
C-x a - or
C-x a i g
inverse-add-global-abbrevAccording to Abbreviation, global definition.
C-x a gadd-global-abbrevAfter word global abbreviation.
C-x a i linverse-add-local-abbrevLocal definition according to abbreviation.
C-x a ladd-mode-abbrevLocal abbreviation after word.
(none)unexpand-abbrevUndo the last abbreviation.
(none)write-abbrev-fileSave Abbreviations.
(none)edit-abbrevsEdit Abbreviations.
(none)list-abbrevsShow Abbreviations.
(none)kill-all-abbrevsDelete abbreviations.

Dynamic Abbreviations

  • Complete the started word "automatically".
  • C- \ (dabbrev-expand)
  • A search is made for the "geographically" closest word that begins with the same letters.
  • Also across buffers!
  • Practical with long (self-explanatory) variable names.
  • Advantage through English keyboard layout.

Buffer and Window

  • Buffers are usually working copies of files.
  • There are buffers that are not assigned to any files:
    e.g .:

Window definition

A buffer is displayed in a window

This is the Emacs definition of a window! GUI windows (X, MS Windows) are called frames here!

Work with multiple buffers

To switch between buffers: C-x b followed by the buffer name.

C-x b followed byeffect
New buffer nameA new buffer is created.
Name of an existing bufferBuffer becomes active.

Clear buffer

With C-x k (kill buffer) a buffer can be deleted.

window

Keystrokescommandeffect
C-x 2split-window-verticallySplit the current window into two one on top of the other.
C-x 3split-window-horizontallySplit the current window into two adjacent ones.
C-x oother-windowGo to the other window.
C-x 0delete-windowDelete the current window.
C-x 1delete-other-windowClear all other windows.
C-x 4 ffind-file-other-windowOpen file in another window.
C-x 4 bswitch-to-buffer-other-windowSwitch to Buffer in Another Window.

List of all buffers

To get the list of all buffers: C-x C-b

MR key

.
displayed
*
changed
%
just read
D.
marked for delete
S.
marked for save

Commands

KeystrokeseffectHappens
C-nGo to the next buffer (down one line).Right away
SPACEGo to the next buffer.Right away
C-pGo to the previous buffer (up one line)Right away
dMark Buffer as deleted.After this x was pressed
sMark Buffer as saved.After this x was pressed
uUnmark.Right away
xDo commands (d, s, x).Right away
1Show buffers in a window.Right away
2Show buffer in the next window.Right away

bookmark

With bookmarks it is possible to remember positions in different files

Keystrokescommandeffect
C-x r mbookmark setSets a bookmark at the current position.
C-x r bbookmark-jumpJump to a bookmark.
(none)bookmark-renameRenames a bookmark.
(none)bookmark-deleteDeletes a bookmark.
(none)bookmark-saveSaves the bookmarks in the default file.
C-x r lbookmark-menu-listOpens the buffer.

Emacs as a working environment

  • Working with directories
  • Read man pages

Working with directories

  • Dired Mode to edit directories
  • To edit directories:
    • C-x d (dired)
    • C-x C-f applied to a directory.
  • Attention, deleting has a direct effect on the files!

Execute shell commands

Shell commands (e.g. grep) can be used on a file: !

Dired mode

View and edit files

Keystrokeseffect
vView file (read-only).
RETURNOpen file for editing

Delete, copy and rename files

KeystrokeseffectHappens
dMark file as deleted.After this x was pressed
D.Delete fileAfter dialogue
uUnmark.Right away
xCarry out orders (d).Right away
C.Copy file.After dialogue
R.Rename the file.After dialogue

Man

  • Man pages can also be read with Emacs
  • M-x man RETURNman-page entryRETURN
  • M-x woman

To change the window behavior:

(setq Man-notify-method 'pushy)

Wildebeest (news and mail)

To read and write emails and news:

M-x gnus

There may be an idea of ​​the wildebeest in the next Graz Linux Days :-) or by:

C-h i RETURN Gnus RETURN

Create texts

  • Plain text
  • HTML / SGML
    • HTML fashion
    • Html-helper mode (not part of Emacs)
    • Xml Mode with HTML DTD (not part of Emacs)
  • Latex
    • AUCTeX and RefTeX mode (not part of Emacs)
    • latex fashion

Edit rectangles

  • Regions always cover the entire screen
  • Emacs also offers: Rectangles.
  • These rectangles are rectangular areas
  • Special commands are offered

Rectangle commands

Keystrokescommandeffect
C-x r kkill-rectangleDeletes the rectangle and saves it.
C-x r ddelete-rectangleDeletes rectangle and does not save it.
C-x r yyank-rectangleInserts the last deleted rectangle.
C-x r c clear-rectangleOverwrites the rectangle with spaces and does not save it.
C-x r tstring-rectangleInserts characters in each line of the rectangle.
C-x r oopen-rectangleInserts an empty rectangle.

Macros

Keyboard macros are recorded commands.

Can be run repeatedly.

application areas

  • Format text
  • Perform complex search and replace operations
  • Reformat imported files

Define macros

C-x (
Start macro recording
C-x)
End macro recording
C-x e
Macro run
C-g
Abort macro recording. (Definition discard)

Example:

C-x (C-e C-f RETURN C-f C-x)

Tips for good macros

  • Use search operations to navigate
  • If surgery is no longer possible:

    Can be used to terminate.
  • Start and end of line only with C-a and C-e
  • Don't make wrong assumptions!
  • Use extra (not required) commands.

Macro commands

Keystrokescommandeffect
C-x (start-kbd-macroStarts macro recording
C-x)end-kbd-macroEnds macro recording
C-x ecall-last-kbd-macroExecutes the last macro
M-n C-x edigit-argument and call last-kbd-macroExecutes the last macro n times
(none)name-last-kbd-macroGives a name to the last macro
(none)insert-kbd-macroInserts Macro into a file
(none)load-fileLoads a file with function definitions
C-x C-kedit-kbd-macroEditing a macro

Configuration of the Emacs

  • Everything can be freely configured
  • The configuration file: .emacs
  • LISP
  • Download my configuration files to try out!

Keyboard layout

(global-set-key "\ C-xl" 'goto-line) (add-hook' c-mode-hook (lambda () (local-set-key "\ C-cc" 'compile))

variables

(setq auto-save-interval 800)

Documentation (Emacs Help)

Keystrokescommandeffect
C-h?help-for-helpavailable help options
C-h thelp-with-tutorialEmacs tutorial
C-h iinfoStarts the info system
C-h cdescribe-key-brieflyBrief description of the shortcut
C-h kdescribe-keyWhich command is on this keyboard shortcut
C-h wwhere-isWhich keyboard shortcut for this command
C-h mdescribe-modeInformation about the current fashion
CH Bdescribe-bindingsWhat are the keyboard shortcuts for this fashion?

literature

  • Debra Cameron, Bill Rosenblatt and Eric Raymond: Learning GNU Emacs: [UNIX text processing]. 2nd ed., Rev. and updated. O'Reilly, 1996. XXIV, 533 pp.
    ISBN 1-56592-152-6 TUB
  • Glickstein, Bob: Writing GNU emacs extensions: [editor customizations and creations with Lisp] 1st ed. - Cambridge [et al.] O'Reilly, 1997. XVIII, 215 pp.
    ISBN 1-56592-261-1 (A nutshell handbook) TUB
  • Debra Cameron: GNU Emacs Pocket Reference O'Reilly, 1999. 58S p.
    ISBN 1-56592-496-7

Online resources

Emacs

Online resources

Wildebeest

Online resources

Additional packages