Text Editing

Last modified: Aug 29, 2023
Contents:

So far, we have ways to move files around and to copy them, but no way to create new files with content of our own.

1 Editing Files

A text editor allows us to create or to make changes to files containing plain text.

We’ll look at a very basic text editor here, nano. It’s chief virtue is its ease of use because it pretty much tells you all the “special” commands available at any time. In a later section, we will look at more advance editors, vim and emacs.

2 nano

You’ll find nano on almost all Linux machines. A few non-Linux Unix machines may have pico instead. pico was the original simple editor. nano was developed as an open-source look-alike.

Invoke nano as

nano path-to-existing-file

to edit an existing text file, or

nano path-to-new-file

to create and start editing a new, empty text file.

Example 1: Try This: Editing with nano

Start by copying a file into your directory to work with.

cd ~/playing
cp ~cs252/Assignments/textFiles/snark.txt .

Then edit that file:

nano snark.txt

 

You should see something like the picture shown here.

  • You can see the current cursor position in the upper left.

    • Try using your arrow keys and Page Up/Page Down keys to move around.

  • In the bottom two lines, you should see a list of special commands that you can issue. The convention for these is that

    • The caret (^) indicates a control character. Type this by holding down your Ctrl key and typing the character. Even though control characters are traditionally shown as upper-case characters (e.g., ^G), do not hold the shift key down when typing them.

    • M- indicates a key sequence that can be typed by typing Esc followed by the next character (again, unshifted). You might also be able to type these key sequences by holding down your Alt key while typing the next character.

  1. For now, type ^G and read the help text. Return to your file via the “Exit” command (which will be listed at the bottom of the screen.

  2. Use the “Where is” command to hunt for the word “muffins”.

  3. Use the “Go to Line” command (note that this will require you to use the Shift key to get _ instead of -) to return to the first line of the file.

  4. Insert a line break after the comma so that the text “by Lewis Carroll” appears by itself on the second line.

  5. Move down to the line that starts “Title:”. Use ^K to cut that line. Use it again to cut the line after that as well

  6. Move down to the empty line just after the line starting with “Author:”. Use ^U to uncut (paste) the lines you had cut.

  7. Move back to line 1 of the file.

  8. Use the “Replace” command to replace all occurrences of “snark” by “snipe”.

  9. Use ^O to write the edited file out as a new file snark1.txt.

  10. Use ^X to exit nano.

    Back in the command line, let’s examine the changes you have made.

    ls
    more snark1.txt
    

3 Checking on Changes

If we have edited a file nad want to see what has changed, we can use more or load the file into the editor and read the whole thing. But if we have only made a few changes, here and there, within a large file, it can be hard to see what we have done.

Two commands that can help us focus on what has changed are diff and grep.

The command

diff path/to/file1 path/to/file2

will check two text files to see if they are different and, if so, will list the differences.

The command

grep pattern path/to/file

will scan each line of a file and will print only the lines that match or contain that pattern.

Example 2: Try This: `diff` and `grep`
  1. Let’s return to our “snark” files.

    cd ~/playing
    
  2. Give the command

    diff snark.txt snark1.txt
    

    You should see that it lists the single lines that have been changed, showing both the old and new form of the line.

  3. We can also look for the specific substitutions that you were asked to make in the earlier Try This.

    grep snark snark.txt
    grep snark snark1.txt
    grep snipe snark1.txt
    

    Look closely at the output of each command to see if it makes sense in view of the changes you made in the editor.

  4. If we add a -i to a grep command, it ignores upper/lower case differences when it searches

    grep -i snark snark.txt
    grep -i snark snark1.txt
    grep -i snipe snark1.txt
    
  5. We can give grep more than one file to look at:

    grep -i snark snark.txt snark1.txt
    grep -i snipe snark.txt snark1.txt
    

    Notice that grep tells us which file it finds each pattern within.

  6. Sometimes, all we want to know is which file contains a pattern. The -l option limits the output to simply the file names.

    grep -i -l snark snark.txt snark1.txt
    grep -i -l snipe snark.txt snark1.txt
    

4 Case Sensitive Searching

Let’s expand our nano repertoire just a bit.

Example 3: Try This: Case Sensitive Searching
  1. Return to your “snark” files.

    cd ~/playing
    
  2. Let’s try something a variation on the previous work. Start by entering nano again:

    nano snark.txt
    
  3. Use the “Where Is” command to search for all occurrences of “snark”. Repeat this command multiple times.

    Notice that this search matches both “snark” and “Snark”.

  4. Go back to the first line of the file. Use the “Where Is” command again, but this time, after typing the ^W, use the “Case Sens” command to turn on case sensitivity. Repeat the search multiple times.

    Notice that, this time, we match only “snark” and not “Snark”.

  5. Use the “Replace” command to replace all occurrences of “snark” by “snipe”. Again, notice that, because case sensitivity is on, only “snark” gets replaced. “Snark” is left alone.

  6. Use the “Write Out” command to save your changes as a new file snark2.txt.

  7. Use the “Exit” command to leave nano.

  8. Examine your changes:

    diff snark1.txt snark2.txt
    

    Do you see the difference that the case sensitive mode made?

Most text editors support both case-sensitive and case-insensitive versions of search and replace. Different editors vary, however, in whether case sensitivity is on by default. They can also vary on how they do replacement. Some editors, asked to replace “snark” by “snipe” in case-insensitive mode, would replace “snark” by “snipe” and “Snark” by “Snipe”, but others would use only “snipe” as the replacement string.