The Text Editor =============== The first thing we will need to build our scientific computing environment is a **text editor**. In fact you already have one on your computer--all computers have a default editor that will save files as basic ASCII text. ASCII ( **A**\merican **S**\tandard **C**\ode for **I**\nformation **I**\nterchange ), pronounced [ass' key], is a simple character encoding format based on the English alphabet. It basically lets you encode the keyboard characters plus some special characters like a command to jump to the beginning of a new line (Enter or Return on your keyboad). The printed ASCII characters (those that get printed to your screen, as opposed to control keys like *CTRL-D* or *ESC*), are: :: !"#$%&'()\*+,-./0123456789:;<=>? @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ `abcdefghijklmnopqrstuvwxyz{|}~ The binary equivalents are of the alphabetical characters are here: :: Letter ASCII Code Binary Letter ASCII Code Binary a 097 01100001 A 065 01000001 b 098 01100010 B 066 01000010 c 099 01100011 C 067 01000011 d 100 01100100 D 068 01000100 e 101 01100101 E 069 01000101 f 102 01100110 F 070 01000110 g 103 01100111 G 071 01000111 h 104 01101000 H 072 01001000 i 105 01101001 I 073 01001001 j 106 01101010 J 074 01001010 k 107 01101011 K 075 01001011 l 108 01101100 L 076 01001100 m 109 01101101 M 077 01001101 n 110 01101110 N 078 01001110 o 111 01101111 O 079 01001111 p 112 01110000 P 080 01010000 q 113 01110001 Q 081 01010001 r 114 01110010 R 082 01010010 s 115 01110011 S 083 01010011 t 116 01110100 T 084 01010100 u 117 01110101 U 085 01010101 v 118 01110110 V 086 01010110 w 119 01110111 W 087 01010111 x 120 01111000 X 088 01011000 y 121 01111001 Y 089 01011001 z 122 01111010 Z 090 01011010 There are a few others for a *TAB*, *New Line*, and "*Alarm*" (tell the computer to make a noise or flash). Here are a few of those: :: ASCII Code Binary Character Description 0 00000000 NUL null 1 00000001 SOH start of header 2 00000010 STX start of text 3 00000011 ETX end of text 4 00000100 EOT end of transmission 5 00000101 ENQ enquiry 6 00000110 ACK acknowledge 7 00000111 BEL bell 8 00001000 BS backspace 9 00001001 HT horizontal tab ... When interacting with your computer, you will need to create text files containing data, programs, scripts (short programs that are not compiled ), and LaTeX files. We'll learn more about each of these later. A **Text editor** is very different from a **WYSIWYG** editor ( pronounced [WIZ' ee wig]; short for **W**\hat **Y**\ou **S**\ee **I**\s **W**\hat **Y**\ou **G**\et ) like *Microsoft Word*. WYSIWYG editors are for the creation of documents that will be printed (or faxed or emailed) where layout, font, and formatting is essential--as in business documents. WYSIWYG files are huge, even if they contain only a few lines of text, since they keep formatting info, change history, and many other things. ASCII (also called "*plain text*") files will often be read by another program--**and this is their true beauty** This could be a list of numbers to be plotted, or the source code (the program text) to be compiled into an executable program file (like an .exe file). You might use your computer very professionally in a business office for many years without using a text editor, but for scientific computing and programming, we can't do much without a text editor. The Default Text Editor ----------------------- If you are working on a computer with the **Windows** operating system, you already have a default text editor: **Notepad** which you can find from *Start->Programs->Accessories->Notepad*. This program will create ASCII text files. By default, Notepad saves files with the extension ``.txt``. You can change this by adding your own extension when saving your file (call it something like ``filename.dat``). If you are using **OSX** on a Mac, the default editor is **TextEdit**. You should be able to find it in Applications. **Try Notepad/TextEdit now.** + Open the *Notepad* (PC) or *TextEdit* (Mac) program, as described above + Type some text into the editor + Save the file (onto your desktop or in your home dir. Name it anything you like.) The Unix Default Editor ----------------------- Under Unix the default editor is **vi**, although most unix systems also have **emacs** and others (Linux often includes **nano**, **KWrite**, and others). The first one mentioned, **vi**, is short for **Vi**\sual editor and dates from 1976. This editor has two different modes, one for adding text and another for moving around, copying/pasting, search/replace, etc. Since it was developed at a time when using Unix meant interacting with a mainframe *only* via a terminal window and keyboard (*there were no computer mice in 1976!*), both of these modes had to use characters on the keypad. In 1976, there wasn't even that much uniformity as to the binary sequence sent by an arrow key--if it even existed on your computer. In the 1980s another text editor became popular: **Emacs**. Emacs is extremely powerful with many features, although some say it has a steep learning curve. The devotion of Emacs users to Emacs and Vi users to Vi has spawned a fundamental `rivalry `_ in the unix community. Vi and Emacs are the two major sides in the `Editor Wars `_ which raged for some 35 years between unix geeks, and has only subsided when memory and bandwidth became so very cheap. I myself, use *The One True Editor*: **Emacs**. To get a sense of the features and comparisons of the most popular text editors, open the following link: + `Comparison of Text Editor Features `_ Impressive, eh? As you can see, there are many... This `site `_ lists the editors by popularity. The top two TEXT editors are: **Vim** (a modern version of **vi**)and **Emacs**. Over the years, I've used many, and they are all good. Let's look at some features that we will need. An essential feature for remote computing (working on a computer that is not in you lap or under your desk) is that they can be used in text mode over a network connection, such as a Terminal window that is connected to a computer on the other side of the world. If you want to edit a file on that remote machine, you will want to use an efficient editor which has hi functionality using only text based commands. Both **Vim** and **Emacs** are text based. The alternative to this is using a GUI ( **G**\raphical **U**\ser **I**\nterface) editor that has little icons for buttons and opens its own window(s). While these editors are very nice and have lots of features, they use a lot of computing power to update the windows, respond to the mouse clicks, highlight text, etc. Transmitting these features over the network can be quite demanding. Consider that anytime a character is written to the window, the entire window is usually repainted on the screen. When the editor window is running on you own PC, you don't notice this. But when each update has to go across the country and back, you do. Windows users may like **Notepad++** (`http://notepad-plus-plus.org/`_) is such an editor. It runs only in GUI mode, and only under Windows, but is quite nice. **TextWrangler** (`http://www.barebones.com/products/textwrangler/`_) is a similarly good GUI editor for Mac users. Note that if you *REALLY must have* a GUI editor, you can set these editors to retrieve the remote file you wish to edit via the network, edit it locally on your laptop/desktop, then put the new version of the file back on the remote machine. But that takes a bit more work... Other nice features we expect in a powerful editor are syntax-color-coded text, modes for different types of files (C++, LaTeX, Python, HTML, etc.), and language-specific keyboard shortcuts for common tasks (like commenting regions, or inserting URLs). Vi -- + Open a Terminal window + At the prompt, type: **vi** You will see this ("splash") screen: :: ~ ~ ~ ~ VIM - Vi IMproved ~ ~ version 7.2.411 ~ by Bram Moolenaar et al. ~ Modified by ~ Vim is open source and freely distributable ~ ~ Help poor children in Uganda! ~ type :help iccf for information ~ ~ type :q to exit ~ type :help or for on-line help ~ type :help version7 for version info ~ ~ Recall that there are **two modes** that *vi* can be in: + *Command* mode, and + *Insert* mode *Vi* opens in *Command* mode. So, if you type some key strokes they probably won't do anything--**until**\--you type one (of several keys) that puts you into *INSERT* mode. Then the keystroke following that will put text in your document. *Try this*: In the open *vi* program, *type* **hello** .. Nothing will happen until you type "*o*", at which point the cursor drops one line (and the splash screen disappears), and the word "**-- INSERT --**" appears at the bottom of the screen. In **COMMAND** mode, without any text in the document, the first four characters: *h-e-l-l* don't do anything. (if there *was* text in the window, **h** moves *left*, **l** moves *right*, and **e** would move to the *end of the current word*). However, the "*o*" character in *h-e-l-l-o* tells *vi* to open a new line for text *below* the current line, and go into **INSERT** mode (That's why it drops down one line). In **INSERT** mode, what you type is recorded. Type your name and address. Now type, **ESC** to exit **INSERT** mode and go back to **COMMAND** mode. In **COMMAND** mode, with the UP arrow, move the cursor up to the first line. Type **i** to go into **INSERT** mode. Now what ever you type will be inserted into your document. Type your name on the first line, and your home address on the next 2 (or 3) lines. While you are in **INSERT** mode, vi acts like a normal text editor, like Notepad. You type and text appears, you can use the arrow keys, backspace, and delete to move around. To *Save* (or "Write") your newly written text file, you must "*Exit* **INSERT** mode" by typing **ESC**, then hit the colon key "**:**". A colon will appear at the bottom left of the window. *Vi* is now waiting for you to enter some command keystrokes. Type **w myaddress.txt** (note the SPACE between the "w" and the filename "myaddress.txt") This means "**w**\rite the contents of the screen buffer to a file called "myaddress.txt". Now, to Quit *vi*, we must be in **COMMAND** mode, which we still are. We then type: **:q**. Since we just wrote our file to disk, there are no unsaved changes and *vi* will quit. If we need to Quit *without* saving, we can do a *forced quit* by adding a **!** after the **q**, like this: **:q!** Now that you have quit *vi*, list your directory contents and see if your new file ``myaddress.txt`` is there. If so, type **vi myaddress.txt** at the prompt, and open your file for further editting. Use what you have learned above to change your home address to your campus address. Remember the fundamental *vi* rule: type **i** to go into *INSERT* mode, and **ESC** to go back to **COMMAND** mode. When you are done, save your file. This ends your introduction to *Vi*. If you want to be a hacker you will have to learn how to use *Vi*, which you can do at many sites online. `Here `_ is a good one, for example. .. note:: If you typed something in *vi*, or get confused about the modes and don't know what's going on, you can always **EXIT** *vi* by typing these keys: **ESC : q !**. + **ESC** always puts you back into **COMMAND** mode, + **:** prepares *vi* to accept commands, + **q** means *Quit*, + and **!** means "*force quit even if changes have not been saved*". | Emacs ----- As I said, I use Emacs as my text editor, partly because I am old and have had too many years of practice with emacs to switch. Another reason I am recommend it is that it's **Free** ! If you look at the comparison list of editors, above, you will notice that many of them have a cost. I have tried to only use free software for this course. In fact **Emacs** was one of the first applications developed by`The Free Software Foundation `_, the people behind GNU software, which you may have heard of. Cygwin, Linux, and much of the Unix world is built on the work of the FSF. Check them out! So, let's fire up **Emacs**. Open a Terminal There are two ways to start Emacs. When running emacs over a network connection, or for a quick edit, use the *Terminal (text) mode* of emacs; at the prompt, type: **emacs -nw** The **-nw** stands for "*No Window*". You should see something like this: .. image:: imgs/emacsNWstartup.PNG There are **four areas** to the emacs screen: + The **Tool Bar** at the top: :: File Edit Options Buffers Tools Help It's not used in terminal mode , but is still there. + The **Buffer Area**, the large window area in the middle of the screen. :: Welcome to GNU Emacs, one component of the GNU/Linux operating system. Get help C-h (Hold down CTRL and press h) Emacs manual C-h r Browse manuals C-h i Emacs tutorial C-h t Undo changes C-x u Buy manuals C-h RET Exit Emacs C-x C-c Activate menubar M-` (`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key. If you have no Meta key, you may instead type ESC followed by the character.) Useful tasks: Visit New File Open Home Directory Customize Startup Open *scratch* buffer GNU Emacs 24.3.1 (x86_64-redhat-linux-gnu, GTK+ Version 3.9.10) of 2013-08-14 on buildvm-17.phx2.fedoraproject.org Copyright (C) 2013 Free Software Foundation, Inc. GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details. Emacs is Free Software--Free as in Freedom--so you can redistribute copies of Emacs and modify it; type C-h C-c to see the conditions. Type C-h C-o for information on getting the latest version. The buffer has some help info at this point. This is where you edit your files. Each file that you are editing is called a **buffer** (it's really a part of memory, which becomes a file on your disk only when you write (or Save) the Buffer. You can have many buffers open at once, i.e. you can be editing many files in one emacs session. + The **Mode Line**: :: -UUU:%%--F1 *GNU Emacs* All L1 (Fundamental) ------------------------------- This is a **highlighted bar** of text that tells you *information* about the buffer you are currently editing. We'll learn more about what these phrases mean shortly. Finally, there is a line of text below the Mode Line + The **Mini-Buffer** :: For information about GNU Emacs and the GNU system, type C-h C-a. This is where you communiate with emacs. Emacs will put error or other messages here for you (such as the " For more information... " phrase when emacs starts up). You also can enter commands here. | Ususally you would start *emacs* (and *vi* for that matter) by giving it the name of the file you want to edit, which is opened in the *buffer* area. If there is no file with the name you gave it, emacs will open a new buffer with the name of that file. So, let's **Quit** emacs by typing: **CTRL-x CTRL-c** In other words, *hold the *CTRL* key down and type *x* then *c*. Yup, we're going to be doing a lot of stuff like that. Emacs uses CTRL and ESC keys as modifiers to the rest of the alphabet keys, in order to give a very rich command set available to the user through a simple text based Terminal. Later, we'll use the GUI version of emacs, and you can go back to *File->Save*, etc. But for now, we are going to learn how powerful the keyboard can be. So, let's start emacs again with a filename. At the prompt in your Terminal, type: **emacs -nw fox.txt** Now, the **buffer** region is empty, the **mode-line** says *mothers.txt*, and the **mini-buffer** says *(New File)*. Emacs is ready for you to type something profound. Type: :: The quick brown fox jumps over the lazy dog. Notice also that two **\*\***'s appear in the **Mode Line**: :: ----: ******-F1 *scratch* (Lisp Interaction)--L1--All----------------------- These two asterics signify that the buffer has been **modified** . In order to save these modifications, you will need to save the buffer to a file. *Emacs commands*, such as *save-buffer*, *kill-emacs* (quit), are entered by typing either: + a **CTRL**-character sequence (abreviated **C-char**), or + a Meta (or **ESC**)-character sequence (abreviated **M-char**). Let's finish the example we have open. So save this buffer to a file, we type the the keyboard shortcut for the "**save-buffer**" command: **C-x C-s** Type this: **CTRL-x** then **CTRL-s**. I've done this so often, I just hold the **CTRL** button with my left pinky finkger down and press "**x**" then "**s**". This is equivalent the usual GUI menu **File->Save** on most editors. The *Mini-buffer* respondes with ``Wrote /home/jhetrick/fox.txt`` and the **Mode-line** changes to :: ----:---F1 fox.txt (Text)--L1--All--------------------------------- The **Mode-line** now displays the name of the file which is connected to this buffer (fox.txt), and the "modified" asterisks ( **\*\***) are gone since our changes have been saved. We can continue to edit the file. Go ahead and add your address to the buffer . Once you have your address in the buffer, **move around with the arrow keys**. **Try using your " Delete, Backspace, Home, and End " keys** . You can see that **emacs** acts pretty much like Notepad . Aside from the text based commands like **C-x C-s**, it's pretty intuitive ( at least to me! ). Once your have your correct address back, save your changes with " **C-x C-s** " again. To **Quit** emacs, remember that we have to type a text sequence. Do: **C-x C-c** By now I'm assuming you understand what " **C-x C-c**" means (CTRL-x then CTRL-c). You should have your xterm prompt back. Do an **ls** to see that your file is there. Go ahead and **cat** it which will print the contents of your file to the screen so you can see that you indeed did just edit a text file. Two more things to try: At your xterm prompt, type **emacs -nw YOURLASTNAME.txt** Notice that this opens your file for editing. Quit Emacs with **C-x C-c** . Now, open emacs without telling it which file to open . **emacs -nw** Now you can tell emacs to **open your YOURLASTNAME.txt ** file by issuing the command: **C-x C-f** Emacs responds by asking you the name for the file you wish it to find in the **mini-buffer**: :: Find file: ~/ Type **YOURLASTNAME.txt** and hit ENTER. Emacs will open your file. Quit emacs again with **C-x C-c**. ------------------------------------ You now know the most basic skills of using **emacs**. You can open a file, edit it, and save it, pretty much the same as you can do with Notepad. Let's summarize: +------------------------------------------+-----------------------------------------+ | Command | Meaning | +==========================================+=========================================+ | *At Terminal prompt*: **emacs -nw file** | Open *file* in *emacs* | +------------------------------------------+-----------------------------------------+ | *In Emacs*: **C-x C-f** | Find (Open) File for editting | +------------------------------------------+-----------------------------------------+ | *In Emacs*: **C-x C-s** | Save Buffer to file on disk | +------------------------------------------+-----------------------------------------+ | *In Emacs*: **C-x C-c** | Quit Emacs and Exit | +------------------------------------------+-----------------------------------------+