Transcript Slide 1
Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • emacs Basics • emacs may not be automatically installed on your system • try emacs filename to confirm if emacs is installed Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • The top line features a menu • The mode line appears near the bottom of the screen • shows the filename and the current line of the cursor; for example L7 • the two ** to the left of F1 signifies a modified buffer • initially you should see three hyphens (---) at this position • The last line, known as the minibuffer, shows an emacs-generated message. • This line is used by users to enter emacs commands and by emacs to display system messages • with 3 lines taken by emacs for its own use, namely menu, mode and minibuffer, just 22 lines are available for editing. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • When emacs is launched, users can enter text right away, using [Enter] to separate lines. • to erase text, use [Backspace] instead of [Ctrl-h]. • [Ctrl-h] is used by emacs to call up help facility • [Ctrl-d] or [Delete] to delete character under the cursor • After the required text is entered; the cursor is positioned on the last line of the text. • Use [Ctrl-b] and [Ctrl-h] to move the cursor backward and forward respectively. • Use [Ctrl-p] for previous line; and [Ctrl-n] for next line. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • In addition to [Ctrl-b], [Ctrl-f], [Ctrl-p], [Ctrl-n], there are faster means of navigation; will be discussed later… • The entered text is not save, but exists in some temporary storage called buffer. • to save the buffer, enter [Ctrl-x] [Ctrl-c] • emacs will produce the message: • Save file /home/kkhan/filename? (y, n, !, ., q, C-r, or C-h) • entering y will save the buffer to its disk file Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • The Control and Meta Keys • Like most word processors and unlike vi, emacs is a “mode-less” editor; any key pressed is always entered as text. • In order to perform navigation and text editing, you need to use the control and meta keys. • In emacs documentation, the key sequence is found as below: •C-e •C-x C-b •C-x b •M-e [Ctrl-e]. [Ctrl-x][Ctrl-b]. [Ctrl-x]b; different from [Ctrl-x][Ctrl-b] Meta [Meta-e] Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • M-e represents [Meta-e] for those keyboards that support a key named meta. For other systems including PCs, the [Alt] key often behaves like meta. • Example: [Meta-y] is M-y • In case neither [Meta] nor [Alt] works, use [Esc] key; the key is never kept pressed. To invoke M-e, press [Esc], release it, and then press e. • If you press [Ctrl-x] and then take more than one second to press the next key, the string C-x appears in the minibuffer; indicating that the command has not been completed. Press [Ctrl-g] to cancel your action. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Entering Commands Directly (M-x) • emacs also support a history facility which lets you recall previous commands that were explicitly invoked using M-x; use the Up and Down keys Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • The File .emacs • As you get comfortable with emacs, you’ll feel the need to customize it; emacs reads $HOME/.emacs (same as ~/emacs) on startup. • The entries in .emacs are written in LISP-the language originally used to write emacs. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Inserting and Replacing Text • Most word processors offer two modes of text input – the insert mode and overwrite mode. • By default emacs works in insert mode • by pressing the [Insert] key which acts as a toggle switch. The first invocation takes you to overwrite mode; the next one returns to insert mode • emacs can also write in overwrite mode which replaces the characters the cursor moves over. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Saving Text and Quitting • Whenever you are in vi or emacs, it is only a buffer (temp storage continuing a copy of the file) that you can edit and not the file directly. Changes to the buffer are written to the disk file with a saving operation. • Save and continue editing • Save and exit • Abandon all changes and quit • Unlike vi, emacs supports an autosave feature that periodically saves the buffer to a different disk file. However, you should save the buffer yourself, but in the same file to keep it current. Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Saving your work (C-x, C-s and C-x C-w) • Make some changes to the file and enter C-x C-s • Use C-x C-w to save to a different file • Quitting the editor • C-x C-c • if no changes have been made to buffer since it was last saved by you, you are immediately returned the prompt • but if you have made changes, emacs needs to know: • Save file /home/kkhan/myfile? (y, n, !, ., q, C-r or C-h) • There are 7 options here; just remember ‘y’ saves the file and quits the editor or ‘n’ will abandon the buffer. • To save quickly: C-u C-x C-c Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Escape to the shell • M-x shell • This returns you to a shell prompt but in a new shell named shell • Execute cc or any UNIX command here • You can save this buffer C-x C-s or return to the previous buffer C-x b[Enter] Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Recovering from a Crash • The autosave feature of emacs saves a copy of the buffer every 300 keystrokes or (30 sec if the user is idle). • Saves it to a different file which will be named #myfile# with # on each side of the original file • Replace the current buffer with the last autosaved file • M-x recover-file • You can remove the autosave version by using the command • rm –I #* or rm –I \#* depending upon the shell you are using Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Navigation • Movement in the Four Directions (C-b, C-f, C-p, C-n) • C-v to scroll forward • M-v to scroll back • M-f move to the beginning of the word • M-b move back one word • C-a moves to the beginning of the line • C-e moves to the end of the line • M-< moves to line number 1 • M-> to reach the end of the file Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The emacs Editor • Deleting text • C-d deletes characters • M-d deletes word • C-k deletes lines in part or full • Searching for a pattern • Incremental search (C-s and C-r) • I-search: c after entering C-s • C-r to search backward • Substitution • M-x replace-string[Enter] • Replace string: float • Replace string float with: double[Enter] Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved.