Ana səhifə

Ust: emacs worksheet


Yüklə 36.46 Kb.
tarix09.06.2016
ölçüsü36.46 Kb.

UST: EMACS Worksheet




Introduction

EMACS is a free, portable text editing system. By free we mean it is open source – the underlying code can be copied, distributed and extended openly. By portable we mean it is possible to use EMACS on many different machines running many different operating systems.


EMACS dates back from 1976, and comes from the name ‘Editor MACros’. It was originally written to amalgamate a number of older text editors with different command sets, keyboard controls and features.
An (occasionally) friendly rivalry has developed over the years between computer scientists who use EMACS and computer scientists who use a rival text editor called vi.
EMACS is a text editor (like Windows Notepad) and much, much more. In fact, it is generally regarded as the text editor that can do anything. If you need a text editor that can do x, you can extend EMACS to do x yourself. However, most of the functionality you could ever need is already available, so this is very much the last resort.
EMACS is a particularly suitable text editor for computer programmers. For most conceivable programming languages, EMACS offers a supporting editing mode. Once this mode is selected EMACS will help with indentation, colour keywords and comments, allow you to compile and de-bug your code, and various other helpful things.
EMACS also offers (a non-exhaustive list):


  1. Access to the command shell, allowing you to run external programs.

  2. Support and control of multiple versions of the same file (useful for group work).

  3. Built in hypertext help.

EMACS is not a what-you-see-is-what-you-get text editor. If you want to write your bank manager a letter, or pen a report for a computer science module, then stick to Word or some other document preparation system (for example, LaTeX). EMACS is best used for hacking out code and the occasional short email.


Getting Started

Opening a File for editing


To start EMACS type the following command and press return.


  • emacs

You should now see an EMACS window. Click on the middle of this window. The window is divided into 4 separate areas. First there is the MENU bar. (similar to any windows application). Below that is the TEXT area. Here text is displayed and edited. Below that is the STATUS bar. This keeps you informed of any changes to your text file (for example, it tells you when the file is being saved). Below the status bar is the MINI-BUFFER. You will use this area when giving keyboard commands to EMACS.


Study the diagram below to familiarise yourself with the various areas of an EMACS window.



Basics

Do Ctrl-x Ctrl-f. A cursor will appear in the mini-buffer. Type ‘hamlet.txt’ and press return. The file will be created (if it didn’t exist.) Click the main window and copy and paste, or type the beginning of the following text.

To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them? To die: to sleep;

No more; and by a sleep to say we end

The heart-ache and the thousand natural shocks

That flesh is heir to, 'tis a consummation

Devoutly to be wish'd. To die, to sleep;

To sleep: perchance to dream: ay, there's the rub;

For in that sleep of death what dreams may come

When we have shuffled off this mortal coil,

Must give us pause: there's the respect

That makes calamity of so long life;

For who would bear the whips and scorns of time,

The oppressor's wrong, the proud man's contumely,

The pangs of despised love, the law's delay,

The insolence of office and the spurns

That patient merit of the unworthy takes,

When he himself might his quietus make

With a bare bodkin? who would fardels bear,

To grunt and sweat under a weary life,

But that the dread of something after death,

The undiscover'd country from whose bourn

No traveller returns, puzzles the will

And makes us rather bear those ills we have

Than fly to others that we know not of?

Thus conscience does make cowards of us all;

And thus the native hue of resolution

Is sicklied o'er with the pale cast of thought,

And enterprises of great pith and moment

With this regard their currents turn awry,

And lose the name of action. - Soft you now!

The fair Ophelia! Nymph, in thy orisons

Be all my sins remember'd.
Now save your work by using Ctrl-x Ctrl-s. (Look at the minibuffer…) Close down EMACS by doing Ctrl-x Ctrl-c. Check that the file hamlet.txt is in your home directory. Create a copy of this file, named hamlet_copy.txt.

Now re-open hamlet.txt using this command:




  • emacs hamlet.txt

Here, the command to open EMACS is given an argument. The argument is name of the file you want to edit. If you give the command two arguments, EMACS will open in split screen mode. Try this command:




  • emacs hamlet.txt hamlet_copy.txt

You should now see two main windows. In the first window will be the text you typed in. In the second window another text file is visible. This allows you to work with two source files at the same time. It is useful when comparing two files for differences (although you can also use the Unix diff command).


Close EMACS and return to the command line.

EMACS as a Background Process

If you want to use the command line and EMACS at the same time, you must open up EMACS in a special way. Type the following command:




  • emacs hamlet.txt &

EMACS will open as normal, but notice that you can still use the command line to give further commands. This is because we are now running EMACS as a background process (using the & symbol). Return to the command line without shutting down EMACS and type the following:




  • emacs hamlet_copy.txt &

You should now have two instances of EMACS on your desktop. Close both of them down now and return to the command line.



EMACS Commands

EMACS commands are usually executed with a series of keystrokes. For example, where the binding given for a particular command is Ctrl-x s, you need to hold the control key then press the 'x' character, release both keys and then press the 's' character.


Most of the commands listed below are also available through the menu system, but it is much more efficient to learn and use the key commands.

Saving a File / Quitting

Ctrl-x Ctrl-s Save the current File

Ctrl-x Ctrl-w Save the current file under a different name

Ctrl-x Ctrl-c Quit EMACS (you may be asked to save first)


Try the save command. Open hamlet.txt as a background process. Insert the following text at the top of the file (i.e. above the poem).
William Shakespeare - To be, or not to be (from Hamlet 3/1)
Then save the file using the key command Ctrl-x Ctrl-s. Return to the command line leaving EMACS open. If the key command does not work, try typing the combination faster. Soon it will become second nature.



Cut/Copy/Paste/Undo/Abort
Ctrl-w Cut selected text.

Alt-w Copy selected text

Ctrl-y Paste text.

Ctrl-_ Undo the last command

Ctrl-g Abort command
Try the cut and paste commands. Highlight a section of text and press Ctrl-w. Move your cursor to another position in the text file and press Ctrl-y.
Try the undo command. Delete all of the text on the screen by selecting it with the mouse and then pressing Ctrl-w. Now undo this deletion by pressing Ctrl-_ (this means you have to press Ctrl, then SHIFT, then the – character.
Try the abort command. Type in Ctrl-x Ctrl-w . This is the command to save the current file under a different name. When EMACS prompts you for a filename type in Ctrl-g to abort the command.
Close EMACS using Ctrl-x Ctrl-c (and NOT the X in the top right corner)

Screen Commands

Ctrl-v Pages forward one page.

Alt-v Pages back one page.

Alt-> Moves to bottom of file.

Alt-< Moves to top of file.

These commands are useful when dealing with bulky text files. At the command line open up hamlet_copy.txt with EMACS and try moving about a text file. When you have done this leave EMACS open.



Search

To search for a string in a text file type one of the commands below, depending upon which direction you would like to search. The search direction moves backwards or forwards from your current cursor position. When you type in the command you will be asked to type in a text string in the mini-buffer. EMACS will immediately find the first occurrence of that string. To find the next occurrence of that string key in the search command once more. You can use this command to find every instance of the string in the text file.


Ctrl-s Forward search.

Ctrl-r Reverse search.


Try the search command. Return to hamlet_copy.txt. Place the cursor at the very beginning of the text file. Type in
Ctrl-s Sleep
EMACS should jump to the first instance of that name (on line 4). Press Ctrl-s again and it should find another instance of that name.

Replace

Alt-x replace-string Will replace every occurrence of the specified string from the current cursor position to end of file.

Try a search and replace. Return to hamlet_copy.txt. Place the cursor at the top of the text file. Type in Alt-x replace-string. When EMACS asks you for a string to replace type in ‘sleep’. When EMACS asks you for a string to replace this with type in ‘dream’. Press return.
In your hamlet_copy.txt file, all instances of the word ‘sleep’ should have been replaced with ‘dream’. Save (Ctrl-x Ctrl-s) and Exit (Ctrl-x Ctrl-c).

Program Code
EMACS is great for editing program code because it helps with indentation and braces, highlights syntax and comments, and even allows you to compile a program without leaving the editor. Install the Haskell mode by doing the following:


  • Use emacs to edit the file .emacs which is in your home directory. This file contains your personal Emacs settings.

  • Open the file .emacs which is in the home directory of user zlizta. Copy the last six last from this file to your file. Close Emacs.

Create a file Test.hs with Emacs. Type a Haskell script, such as:


module Test where
-- Sum of integers from 1 to 4

s = sum [1..4]

If the code is not already highlighted then go to the OPTIONS menu and click on GLOBAL FONT LOCK (Highlight Syntax).
You should now notice that the Haskell script has been highlighted. Comments will appear in a brown font, text string are pink, reserved words are purple and so on. Leave EMACS open.

Access to The Shell (Command Line)
Alt-! execute a shell command
Make sure EMACS is working in background mode. Load the Haskell module Test by typing Alt-! hugs Test.hs &

EMACS will split into two windows. In the top window you can still see your code. In the lower window you can see the Hugs interactive session. Use Ctrl-x o to switch between windows.


Another possibility is to open a shell in a buffer. Type Alt-x shell. This is particularly useful when splitting the screen in many windows.
Being able to compile and run a program from an editor will save you lots of time in the future. Close EMACS now and return to the command line.


Use of the Buffer

You can view several buffers at a time using EMACS. Open hamlet.txt with EMACS. Then type in C-x C-f (this is standard notation for Ctrl-x Ctrl-f). When you are prompted for a filename type in hamlet_copy.txt.


The text on the screen is now replaced by hamlet_copy.txt. To view hamlet.txt again repeat the above. Note that you can copy and paste between files in the buffer.

Copy the text from hamlet.txt and insert it above the text in hamlet_copy.txt.




Macros

C-x ( start to define a macro

C-x ) end the macro definition

C-x e execute the macro


You can define a macro for actions that are repeated frequently. For example, imagine you needed to insert the date at several points in a file. You could define a macro that does this for you with the following commands:
Ctrl-x ( Ctrl-x )
You could then use this macro to insert the date at the current cursor position with the command Ctrl-x e.


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©kagiz.org 2016
rəhbərliyinə müraciət