Editing under X

Steven Zeil

Last modified: Aug 24, 2023
Contents:

Editing is one task that can benefit tremendously from the availability of a windowing system, enhancing the ability to work with more than one document at a time, adding support for menus, mouse-clicks to reposition the cursor, scroll bars, etc.

1 gedit

Every Linux distribution has at least one “vanilla” X-based editor, the graphics mode equivalent of nano or of Notepad in Windows.

On our Linux servers, this is gedit.

Example 1: Try This: Compare and Merge in emacs
  1. Launch an xterm on one of the Dept Linux servers.

  2. Copy the file ~cs252/Assignments/emacsxDemo/jabberwocky1.txt to ~/playing.

  3. Launch gedit:

    cd ~/playing
    gedit jabberwocky1.txt &
    
  4. In a few seconds, you should now be looking at a window showing the file jabberwocky1.txt.

    Use the arrow keys and the mouse to move around in the file.

  5. Explore the various menu options available to you.

  6. Now use the Open menu item or toolbar button to open ~/playing/sieve/sieve.cpp, which should be left over from one of your earlier exercises in compiling.

    Notice that, although gedit is a very basic editor, it still offers colored “syntax highlighting” for C++ (and other common programming languages).

  7. Exit gedit when you are done.

Do one or both of the following sections:

2 emacs in Graphics Mode

 

If you have not already done so, try launching emacs from within an xterm. Notice that it pops up in a separate window. Compare this look …

 

… to emacs when run in a ssh text window. Note that you now have functioning menus and scroll bars. You may see new use of color. A little experimentation will show that you can, indeed, reposition the cursor with a simple mouse click, and you may find that your PageUp, PageDown and other special keys now behave in an intuitive manner.

Many people who dislike emacs complain about having to learn a large number of keyboard command sequences to get things done, like moving the cursor, scrolling the text, etc. What you should be able to see, now, is that such key sequences are only necessary for relatively uncommon actions, or if you are not running in a windowed environment. After all, if you are running in a text window, how can you expect a mouse click to do anything? In fact, emacs senses whether or not you are running in a windowing system, and takes advantage of it if you are, but leaves you with the key sequences as a backup mode when working under more primitive conditions.

2.1 Graphics-Mode Features

There are few emacs functions that can only be done in graphics mode. But there are also a lot of things that just get easier to work with once you have color and formatting options and menus.

Example 2: Try This: Menus in emacs
  1. Launch an xterm on one of the Dept Linux servers.

  2. Let’s set up a directory with some C++ code.

    mkdir ~/playing/sieve
    cd ~/playing/sieve
    cp ~cs252/Assignments/progdevx/* .
    
  3. Launch emacs:

    emacs sieve.cpp &
    

    When emacs comes up on your screen, you should see the various syntax elements of the C++ code appearing in different colors.

  4. Click in various places in the C++ text. Notice that you can now position the cursor with a mouse click instead of the keyboard movement commands (although those are still available if you want them).

    You should be able to select text by dragging the mouse as well.

    You have a scroll bar, though it may not be visible at the moment because this file is so short.

  5. You have already seen how emacs provides commands C-x 0, C-x 1, and C-x 2 to split the text into “windows” and recombine them again. If you have forgotten how these work, now might be a good time to refamiliarize yourself with them.

    Of course, these aren’t “real” windows (or else those commands would be useless when working via text-mode ssh), but we have to cut emacs a little slack here. Its use of the term “window” to denote those text areas predates all window-based operating systems.

    When running under a true windowing system like X, however, emacs does provide analogues of these commands that really do work with “real” windows (called “frames” by emacs and, ofr that matter, by most GUI programming libraries as well). These are produced by replacing the C-x by C-x 5. For example, C-x 5 2 will create a new frame.

    Try this, and the other frame command analogues.

  6. You should have a fully functional menu bar at the top of the emacs window. Click on a few of the menus just to see what’s there.

    In the File menu, for example, you will find alternate ways to invoke the windows and frames commands that you were just experimenting with.

    Note that, because you are currently editing a C++ file, you have a C++ menu of items specific to C++ editing. (Others that will be of interest for program development appear in the Tools menu, including entries for compiling your code and launching the debugger.)

    We’ll explore some of those in upcoming lessons.

  7. Exit emacs. You can do this from the File menu or by the usual C-x C-c key sequence.

2.2 Enhanced Text Manipulation

It’s very common for both writers and programmers to wind up with multiple copies of files containing largely the same content. You might want to figure out what, if any, the actual differences are. For example, you might have an older and a newer version of a program’s source code and wonder just what changes you made to go from one version to another. In the Tools menu, you will find commands to Compare two files (or two buffers, if you have laready loaded the files into emacs). This will show the two files, one beneath the other, with colored sections highlighting the differences and will allow you to step from one to the other.

Such comparisons are particularly useful when testing computer programs, where you often have a file indicating the expected output for a particular test, and would like to compare the actual output that you received by running your program to see if they match or, if they do not, exactly what the differences might be.

A related problem often faced with programmers is discovering that you have made some changes to a program in one file, but that you or a teammate have made other changes in a separate copy of that file. The Tools menu also includes commands to Merge two files (or two buffers). Like the Compare function, this highlights the differences between the two input files and allows you to step from one point of differnece to the next. But the Merge function allows you to select which changes to copy into a third, merged copy that you can use to consolidate the separate changes.

Example 3: Try This: Compare and Merge in emacs
  1. Launch an xterm on one of the Dept Linux servers.

  2. Copy the file ~cs252/Assignments/emacsxDemo/jabberwocky1.txt to ~/playing.

  3. Launch emacs:

    cd ~/playing
    emacs jabberwocky1.txt &
    
  4. You should now be looking at a frame showing only the file jabberwocky1.txt. We’re going to make a copy of this file and make a few changes. Save this file as jabberwocky2.txt (use C-x C-w or choose “Save As” from the File menu).

    Now, let’s invoke the emacs “batch” spell checker, used for checking an entire file at a time. M-x ispell-buffer is the command to launch this.

    As you might suspect, the spell checker is going to have a field day with this poem.

    • For each misspelled word in the first and third lines of the poem (starting with the line beginning “Twas…”, not counting the title lines), if ispell suggests one or more replacement words, choose one of the suggested replacements by typing the indicated key. (For example, you should be able to replace “brillig” by “Broiling” by typing ‘4’.)

    • Leave all misspelled words in the 2nd line unchanged.

    • When the spell checker has moved past the first 3 lines of the poem, use q to quit the spell checker. Save this file.

  5. The ispell spell checker actually is not restricted to use under a windowing system. It will work perfectly fine in a text-mode emacs session. But, now let’s look at an alternative. Open up a new, blank file, called jabber.txt. Then give the command M-x flyspell-mode. (Don’t forget, by the way, that you can tap the space bar after the first few characters to attempt auto-completion of the command.) Then type (not copy-and-paste) in the following line of text:

    The bandersnatch also appears in the pome "The Hunting
    of the Snark".
    

    Notice that, in the on-the-fly spellcheck mode, misspellings are flagged immediately, just as in most commercial word processors. (And were you surprised that one of those words actually was in the checker’s dictionary?)

    If you have a 3-button mouse, and your X server supports it, try middle-clicking on one of the misspelled words. If you have a two-button mouse, some servers will emulate the middle button if you press both buttons simultaneously. If this works for you, you should see a list of suggested corrections pop up.

    Save that file, or dispose of it as you will. We’re done with that one.

  6. A fairly common problem in program development is coming up with two versions of the same file and wondering if their contents are really different and, if so, what those differences are. Of course, we can use the Unix diff command to obtain this info, but it can be rather hard to interpret the differences from the diff output format.

    From the Tools menu, select “Compare (Ediff)” then “Two Files…”. Follow the prompts to select “jabberwocky1.txt” and “jabberwocky2.txt” as the files to be compared.

    You should see a new, small window pop up momentarily. This is the control window for ediff.

    The principal commands for ediff are n to go to the *n*ext difference, p to go back to the *p*revious difference, and q to *q*uit when you are done. Use n and p to navigate through the differences between the two files. You can also expand the command window to get a list of other ediff commands with ?. Another ? will close it again.

    Quit ediff when you have viewed all the differences.

  7. Sometimes when you have two versions of the same file, you find that you have gotten unlucky. Both versions contain changes that you want to keep. This can easily happen if you are working in teams on a project, or if you keep separate copies on your home PC and on the Unix network. emacs supports a merge facility that can let you selectively combine changes from two files.

    Do a C-x 1. From the Tools menu, select “Merge” then “Files…”, and again select the two jabberwocky files.

    Again, you can use n and p to navigate back and forth across the set of differences. In addition, however, the commands a and b will copy into the “merge” window the “A” and “B” versions of the differing text.

    Using these commands, let the merged version of the files contain the “A” version of the first line of differing text but the “B” version of the 2nd difference. Then quit the merge process with q.

  8. With the cursor position in the *ediff-merge* area, use C-x C-w to save this merged version as jabberwocky3.txt.

  9. Exit emacs.

  10. Back in your xterm window, do an ls to examine the files you have been working with.

    Give the following commands to compare the various jabberwocky files:

    diff jabberwocky1.txt jabberwocky2.txt 
    diff jabberwocky2.txt jabberwocky3.txt 
    diff jabberwocky1.txt jabberwocky3.txt 
    

    Do the number of changed lines reported make sense, given the changes you actually made?