- 4 Editing files and Emacs Editing files The Emacs editor.
4 Editing files and Emacs Editing files The Emacs editor.
4 Editing files and EmacsEditing filesThe Emacs editorEditorsEditors 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 EditorA powerful editor, available on most UNIX systemsIts a text editorComplex and extensible - we will only explore a fraction - see tutorial and on-line help facilities for moreEmacs conventionsFrequent use of the control and meta keyscontrol key usually marked control or ctrlthe meta key is usually marked altThe 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 keyIf 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 keyMini-bufferStatus lineEdit windowStarting and quittingFrom the UNIX prompt type emacs or emacs filenameThe emacs screen is divided into three partsedit window - large part where the contents of the file are displayedstatus line - name of file, how far through you are and whether it needs savingminibuffer - where commands are enteredC-x C-c to quit (of course). You may be prompted to save filesMoving around the fileYour current position in the file is shown by the cursorMost basic movement is one character or line at a timeuse the arrow keys orC-bbackward (left) one characterC-fforward (right) one characterC-pprevious line (up one character)C-nnext line (down one character)Scrolling happens as necessaryCan also move in steps of a word, sentence, paragraphs and screenM-fforwards one wordM-bbackwards one wordC-astart of current lineC-eend of current lineM-astart of current sentenceM-eend of current sentenceC-v scroll down one screenM-vscroll up one screenM-move to end of fileC-l refreshes the screen and centers the cursor.Inserting and deleting textTo insert text, position the cursor and typeType RETURN to enter a new line, otherwise the line will wrap aroundDEL deletes the character before the cursor and C-d the character after the cursor. Hold down for repeated operationM-del and M-d deletes whole wordsC-k deletes from cursor to end of line. A further C-k deletes the new line characterMark 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 regionM-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-yAnother C-y to yank back another copyM-y to yank back next previously deleted textCan use C-w and C-y to move text aroundFile handlingNeed to be able to load and save filesC-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 createdChanges made on screen are not immediately made to the file, they are made to a copy called the bufferHave 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 bufferSave frequently - work will be lost if emacs or the system crashesPrevious version may be backed up in filename~Undoing mistakesC-x u, C-_, and C-/ undo the previous commandRepeating the undo command takes you further back through the editing historySearchingNeed to search for text in a large fileIncremental searchtype C-s to search forward. Type C-r to search backwardsOr 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 stringC-g to quit at any time (this works for any command!)Replacing stringsChange all occurrences of one string of text with anothertype M-x and enter query-replaceOr type M-%enter search and target stringstype y or n at each prompt. ! to replace alluse replace-string to avoid the promptingM-x then spell-buffer for the spell checkerWindowsCan divide the edit window into smaller sub-windows, each with a different documentC-x 2 split horizontallyC-x 3split verticallyC-x 1make current window the full windowC-x omove cursor to the next windowModesModes configure emacs for different kinds of editing, controlling wrap-around and indentingM-x then mode namefundamental-modetext-mode (gives automatic text wrapping)c++-modeMay guess mode from the filename suffixUse TAB for autocompletionHelp and the tutorialC-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 folderCoursework 1Deadline: 10th April 4PM.Covers the first three lectures (and the revision lecture)It mentions one simple command we havent seen. Youll have to find out how it works.Next LectureUNIX processesHow to list themHow to prioritise themHow to schedule themParent and Child processesSignalsWhat are they for?Types of signal