4 editing files and emacs editing files the emacs editor
TRANSCRIPT
4 Editing files and Emacs
Editing filesThe Emacs editor
EditorsEditors are used to create files of text
and to modify their contentsYou need an editor for sending email,
writing programs and many other tasksThere are many different editors - vi, ed
and emacs are common UNIX examplesThere are different styles of editor - e.g.
line editors and text editorsThey are not the same as word
processors which are concerned with printed documents (fonts, sizes, etc.)
The Emacs Editor
A powerful editor, available on most UNIX systems
It’s a text editorComplex and extensible - we will
only explore a fraction - see tutorial and on-line help facilities for more
Emacs conventions
Frequent use of the control and meta keys control key usually marked control or ctrl the meta key is usually marked alt
The notation C-x means hold down the control key and simultaneously press the x key (was ^X in previous lectures)
M-y means hold down the meta (alt) key and simultaneously press the y key
If the alt-y combination is taken by the host operating system (for example Windows), you can press the escape (esc) key, release it, and then press the y key
Mini-buffer
Status line
Edit window
Starting and quittingFrom the UNIX prompt type emacs or
emacs filenameThe emacs screen is divided into three
parts edit window - large part where the contents
of the file are displayed status line - name of file, how far through
you are and whether it needs saving minibuffer - where commands are entered
C-x C-c to quit (of course). You may be prompted to save files
Moving around the fileYour current position in the file is
shown by the cursorMost basic movement is one
character or line at a time use the arrow keys or
C-b backward (left) one characterC-f forward (right) one characterC-p previous line (up one
character)C-n next line (down one character)
Scrolling happens as necessary
Can also move in steps of a word, sentence, paragraphs and screen
M-f forwards one wordM-b backwards one wordC-a start of current lineC-e end of current lineM-a start of current sentenceM-e end of current sentenceC-v scroll down one screenM-v scroll up one screenM-< move to start of fileM-> move to end of file
C-l refreshes the screen and centers the cursor.
Inserting and deleting text
To insert text, position the cursor and type
Type RETURN to enter a new line, otherwise the line will wrap around
DEL deletes the character before the cursor and C-d the character after the cursor. Hold down for repeated operation
M-del and M-d deletes whole words
C-k deletes from cursor to end of line. A further C-k deletes the new line character
Mark and then delete a whole area: C-SPACE at start and move cursor to end. Make sure you have “Active Region Highlighting”
ticked to see the marked region in color! C-w to delete the marked out region M-w copies the area without deleting it.
Commands that remove more than one character at a time save the text. It can be yanked back with C-y Another C-y to yank back another copy M-y to yank back next previously deleted text
Can use C-w and C-y to move text around
File handling
Need to be able to load and save files
C-x C-f finds and loads a filePrompts for the pathname in the
minibufferType the name and press RETURNUse TAB for filename completionFile is either loaded or created
Changes made on screen are not immediately made to the file, they are made to a copy called the buffer
Have to explicitly write them back by saving, C-x s (all files) C-x C-s (current file) (answer y and n to prompts)
Can have more than one file open at a time. C-x C-f also switches between buffers if we give the name of an open buffer.
C-x b swaps to the previous buffer Save frequently - work will be lost if emacs or
the system crashes Previous version may be backed up in filename~
Undoing mistakes
C-x u, C-_, and C-/ undo the previous command
Repeating the undo command takes you further back through the editing history
SearchingNeed to search for text in a large fileIncremental search
type C-s to search forward. Type C-r to search backwards Or enter command M-x isearch-forward
or M-x isearch-backwards in the minibuffer (use TAB for command completion)
you can delete the last character in the search string
C-g to quit at any time (this works for any command!)
Replacing strings
Change all occurrences of one string of text with another type M-x and enter query-replace Or type M-% enter search and target strings type y or n at each prompt. ! to replace all use replace-string to avoid the prompting
M-x then spell-buffer for the spell checker
Windows
Can divide the edit window into smaller sub-windows, each with a different document
C-x 2 split horizontallyC-x 3 split verticallyC-x 1 make current window the
full windowC-x o move cursor to the next
window
Modes
Modes configure emacs for different kinds of editing, controlling wrap-around and indenting M-x then mode name fundamental-mode text-mode (gives automatic text wrapping) c++-mode
May guess mode from the filename suffix
Use TAB for autocompletion
Help and the tutorial
C-h for top level help menuC-h C-h to find out what each
menu option meansC-h t for the on-line tutorialA cheatsheet with most of the key
combinations in this lecture is in the module folder
Coursework 1
Deadline: 10th April 4PM.Covers the first three lectures (and
the revision lecture)It mentions one simple command
we haven’t seen. You’ll have to find out how it works.
Next Lecture
UNIX processes How to list them How to prioritise them How to schedule them Parent and Child processes
Signals What are they for? Types of signal