Now that you've worked through your first program, it's time for a more complicated one. This next program consists of two files -- MultipleFileDemo.java and SecondFile.java. They should be in the same place that your HelloEmacs.java file was located in. In this section, we will talk about hints for opening files. Open the files as described, but do not edit them just yet.
Remember, you can always use the mouse to choose the "Files" menu followed by "Open File". If you want to do this quickly from the keyboard, you can hit Ctrl-x followed by Ctrl-f. (This will be abbreviated as C-x C-f, in this tutorial, the Emacs tutorial, and other places.)
Try to open MultipleFileDemo.java by yourself. If you get stuck, at the "Find file: " prompt, hit TAB to see completions of your current typing. So, for example, if you had typed ~/tuto and then hit TAB, it would probably complete ~/tutorial/ for you, unless you had other files or directories with that same name in your home directory. So, you could completely type ~/tutorial/MultipleFileDemo.java with just four or five keystrokes.
As a side note, the ~ symbol represents the home directory in UNIX. Even though this version of Emacs is running on windows, it still depends on having a home directory to find its setup files. If you examine the script (c:\emacs-20.7\emacs.bat) used to launch the program, you will find that the HOME environment variable is set to c:\cs101. This means that whenever Emacs sees a ~, it means c:\cs101, so ~/tutorial actually represents c:\cs101\tutorial.
If you get completely lost, you can hit TAB twice at the "Find file:" prompt. This will create a list of the files and directories in the current folder. Put your mouse on one of them and hit the Enter key to choose that completion. It looks something like this:
You can quit out of the minibuffer (and try the command again from scratch) by typing C-g (Ctrl-g) while in the minibuffer mode.
A buffer is best described as a temporary file that holds changes you make to a saved file on disk. When you save the file, Emacs overwrites the file with the contents of the buffer. So, when you open a file in Emacs, you are actually opening a buffer that holds the changes. You can revert to a saved version of a buffer by choosing "Revert Buffer" from the "Files" menu. This discards any changes since the last save.
Emacs has a few capabilities for opening multiple buffers simultaneously. In this section, we illustrate several ways to open multiple buffers. Be sure you have MultipleFileDemo.java open before proceeding.
First, try a simple "split window". Under "Files", choose "Split Window". This tiles the split windows vertically, like this:
Here, we use the term "window" to refer to any one of the editing areas on the screen. We will use the term "frame" to refer to the entire Emacs window ("window" in the Windows9x/NT sense). Notice that both halves of the window refer to the same buffer -- any changes you make in one are made in the other. You may change the relative heights of the windows by dragging the black status bar in the middle up and down with the mouse.
Now, try opening SecondFile.java in the window you just created. Click in the lower half of the window, and then open SecondFile.java just like the other files. SecondFile.java fills only half of the screen, in the window that you just created:
Now, the two windows refer to different buffers. You can visit different buffers in any window by first clicking in that window (to put the cursor in it and make it active), and then choosing the buffer you wish to visit in the "Buffers" menu:
The active buffer is the one to which global actions like saving, compiling and running are applied. So, when you wish to compile a file, be sure to click in the buffer you want to make active before compiling.
Alternately, to change files, you can use the speedbar, as described above. To change back to a one-window view, choose "One Window" from the "Files" menu.
Now, try a horizontal tile. You can't do this one from the menu. On the keyboard, hit C-x 3 (Ctrl-x followed by 3, and NOT Ctrl-3), and the windows should tile horizontally, like this:
You can drag out the edges of the window to increase the size so you can see everything. Again, "Files"/"One Window" will change it back to normal. (The keyboard shortcut for "One Window" is C-x 1 if you are interested, and the shortcut for "Split Window" is C-x 2 .)
You can create an interesting mix of splits by mixing "Split Window" and C-x
3, doing something like this:
Strangely enough, a single call to "One Window" makes all tiling go away.
One last thing you can do is open a completely different Emacs frame. From the Files menu, choose "Make New Frame". Each of these frames can then be split into windows using "Split Window" and C-x 3.
Notice that when the frames split, their titles change to reflect the file that is currently in the active window in that frame (rather than saying "emacs@PC120" or something like that). You can destroy either frame by choosing "Delete Frame" from that frame's "Files" menu, or closing it using the "X" button in the upper right-hand corner.
The last stop on our tour (besides compiling one more program) aims to tell you how to do a few simple editing tasks.
First, cutting, copying and pasting. To cut or copy, highlight the region you want to cut/copy using the mouse, and then choose "Cut" or "Copy" from the "Edit" menu. To paste, put the cursor at the place you wish to paste to, and choose "Edit"/"Paste". A word of warning: the region does NOT include the current location of the cursor. An additional word of warning: you cannot highlight a region to replace with the cut or copied text when you do a paste, as you are able to do in some other editors. If you do this, a paste will simply copy the highlighted text. (However, it's a great way to quickly copy a block of text.)
A safer way to cut and replace blocks of text at a time is to put the cursor at the beginning of the first line to be deleted, and then type C-k until each line you wish to remove is deleted. Then, to put it back, move the cursor to the place you want to put the text, and then type C-y to bring it all back at the cursor location. You can repeat C-y as many times as you wish to make multiple copies of the text. (Note: the first C-k on any line will not delete the whole line, but just the text on the line, unless there's no text. The second C-k for a line will delete the newline character that remains, and bring up the rest of the text one line.)
If you ever make a mistake, you can use "Undo" in the "Edit" menu to undo the changes. (The keyboard shortcuts for "Undo" in NTEmacs are C-/ and C-x u ; both of them do the same thing.)
The last operations we'll discuss are "search" and "query replace". Hint: whenever you do a search or a query replace, the search begins from the current location of the cursor, so you should put the cursor at the top of whatever buffer you will be searching through.
To search for a given string in a file, choose "Search" from the Search menu, and type the string you wish to search for. (Alternately, you can type C-s to enter "Interactive Search", which searches for words as you type.) To replace a given string (say "XXXXXXX") with another string (say "John Doe"), choose "Query Replace" from the "Search" menu. The prompt "Query replace:" appears in the minibuffer. At this prompt, enter the characters you wish to replace, in this case, XXXXXXX. The next prompt should read something like "Query replace XXXXXXX with:". Enter the characters that should replace XXXXXXX, in this case John Doe. Emacs will then find every instance of XXXXXXX and ask if you wish to replace it with John Doe. Type "y" each time to replace that particular occurrence, type "n" to skip it, or type ENTER to stop doing query replacements.
Try replacing "XXXXXXX" with your name wherever it appears in MultipleFileDemo.java. Then, replace "XXXXXXX" and "YYYYYYY" with your name, and a friend's name, respectively, in SecondFile.java. In order to compile and run this program, you'll need to visit the buffer containing MultipleFileDemo.java and compile and run it from there as described above for HelloEmacs.java. (The reason you cannot run it from SecondFile.java is that MultipleFileDemo has a special main method in it that outlines the steps for the program. Don't worry about that now -- when you write your own programs, it will be clearer.)
Congratulations! You now know the ins and outs of Emacs and the JDE -- enough to get you started on writing and running your own programs. The rest of this guide covers the topics found in this Quick Tour in more detail, as well as revealing other secrets of Emacs that you might want to know as you become a more advanced user of this powerful (if not somewhat unintuitive) tool. Before going off on your own, you may wish to read the TechSpeak section, which covers some terminology you might find helpful when discussing Emacs and programming in general with your (possibly more experienced) friends. Happy programming!