Editing in Text Mode

Steven Zeil

Last modified: Aug 24, 2023
Contents:

An editor is a program that allows you to easily create and alter text files.

Because we are still working via text-mode connections, our editors will need to run within our SSH terminal windows. That means they will not provide menus, buttons, mouse interactions, and many of the conveniences that we get when connected in graphics mode. Instead we will need to give all of our instructions via the keyboard. Usually that means using special key sequences using the Ctrl or Alt keys to accomplish things like loading and saving files, copying and pasting text, etc.

nano can suffice for basic editing, but I recommend that programmers also learn either emacs or vim because

That said, there is an almost religious fervor separating the emacs and vim camps. I think that you can do well with either. Personally, I prefer emacs because

So, if you already know a little vi or vim and would prefer to stay in that world, or if you just think the name “emacs” is plain silly, you can skip down to the next section.

1 emacs

Like nano, you launch emacs by giving the command name (emacs) followed by the path to a file that you want to edit.

emacs has a built-in tutorial, and you should begin that very shortly. But first, just a couple of notes:

Example 1: Try This: The emacs tutorial

Give the command

emacs -nw

to run emacs in a text-mode session.

Follow the on-screen directions given to bring up the tutorial (i.e., type ^h followed by “t”.).

Continue following the instructions to make your way through the tutorial.

Exit emacs when you have completed it.

Now let’s try actually using emacs to edit a file.

Example 2: Try This: Edit with emacs

You should still have a snark.txt file in your playing directory.

cd ~/playing
emacs -nw snark.txt
  1. Try using your arrow keys and Page Up/Page Down keys to move around. These should work just as well as the control keys covered in the tutorial.

  2. Use the search (C-s) command to hunt for the word “muffins”.

  3. Use the M-< command to return to the first line of the file.

  4. Use the replace (M-%) command to replace all occurrences of “snark” by “snipe”. Notice as you go how emacs treats the various upper/lower case variations of “snark”.

  5. The “Undo” command in emacs is C-x u. Use it (repeatedly) to undo all of your replacements.

  6. Use the M-< command again to return to the first line of the file.

  7. Toggle the case sensitivity via the command M-x toggle-case-fold-search. You don’t need to type all of that out, however. Try doing M-x tog then hit the Tab key. emacs will attempt to compelte the command, adding gle-.

    It will stop there, because there are a lot of different commands that start with M-x toggle- and emacs doen’t know which one you want. (You can hit Tab again to see what they are.) Type a c and hit Tab again to complete your choice. Then use Enter to run the command.

  8. Use the replace (M-%) command to replace all occurrences of “snark” by “snipe”. Notice as you go how emacs treats the various upper/lower case variations of “snark” this time.

5.Use the C-x C-w command to save your changes as a new file snark3.txt.

6.Use the command C-x C-c to exit emacs.

  1. To see that you actually made a change, use diff and grep commands to examine the file:
    diff snark.txt snark3.txt
    grep -i snark snark3.txt
    grep -i snipe snark3.txt
    

    You should see your snark/snipe changes.

When you are done with the tutorial, here are few extra things you should know about emacs:

1.1 Emacs Modes

emacs offers customized modes for different kinds of files that you might be editing. Some of these are chosen automatically for you depending upon the file name you give. Others can be chosen automatically by giving the command M-x name-mode where name indicates the desired mode. Some of the most popular modes are: text, html, c, and c++. The programming language modes generally offer automatic indenting at the end of each line, though you may have to end lines with the “Line feed” or “C-j” key rather than “Return” or “Enter” to get this.

Example 3: Try this: C++ in emacs
cd ~/playing
cp ~cs252/Assignments/textFiles/hello.cpp .
emacs -nw hello.cpp

emacs will take note of the .cpp file extension and start this file in C++ mode.

If your terminal program supports color, you may see colored “syntax highlighting” as emacs renders reserved words, string constants, and comments in different colors and/or fonts.

And there are little, more subtle things that you will come to appreciate over time. Try typing a for loop

for (int i = 1; i <= 10; ++i)
  {
    cout << i << endl;
  }
  • Notice as you type the “)” and “}” in your code, the cursor briefly flashes back to the “(” or “{” that you just closed.
  • Notice also how the indentation is supplied automatically as you end each line.

Move your cursor back up to the endl and delete that word. Now type just the e and then do M-/.

  • The command M-/ is a special friend to all programmers who use long variable names but hate to type them. Type a few letters of any word, then hit M-/. emacs will search backwards through what you have previously typed looking for a word beginning with those letters. When it finds one, it fills in the remaining letters. If that wasn’t the word you wanted, just hit M-/ again and emacs will search for a different word beginning with the same characters.

1.2 The Mark and the Region

emacs has a number of commands that work on an entire block of text at a time. For example, the emacs tutorial told you how to delete a line using C-k. But what if you wanted to delete everything from the middle of one line to the 1st word five lines away? There is a command (C-w) for killing an entire region of text, but to use it you must first tell emacswhat region you want to kill.

The procedure for doing this is the same in all emacs commands that work on regions of text. The “current region” is the set of characters from the “mark” to the current cursor position. The “mark” is an imaginary position marker established by the set-mark-command. The keystrokes for that command are either C-[spc] (hold the control key and type a space) or C-@ (hold the control and shift keys and type ‘2’).

 

So to set up a region to operate on, you move the cursor to one end of the region, give the set-mark-command, then move the cursor to the other end of the region. Everything between the mark and the cursor constitutes the current region, and can be operated on by any region-based command. Some region commands of note are:

C-w
Kill the region, i.e., delete it but save the deleted text in the clipboard. You can “yank” (paste) the deleted text at the cursor position with C-y.
M-w
Copy the region, i.e., Save a copy of the region’s text in the clipboard. You can “yank” (paste) the deleted text at the cursor position with C-y.
C-xC-x
Exchanges the mark and the cursor. Hitting this repeatedly will flip you back and forth between the start and end of the current region. Although often useful in its own right, this command also provides a quick way to check and see if the region is really where you think it is.
C-cC-c
Comment out a region - in C++ and other programming modes, places comment markers in front of each line in the region.
M-x ispell-region or M-x flyspell-region
Run a spell check on all text in the region.

1.3 Customizing emacs

emacs can be customized by placing various commands inside a file named .emacs in your home directory.

Here, for example, is my own .emacs file. I’ve been accumulating things in there for a long time, so there’s a lot in there. Some thing that might interest you are my key bindings:

If you are interested in these, feel free to copy my .emacs file:

cp ~zeil/.emacs ~

1.4 Where’s the Documentation?

It’s in emacs. The end of the tutorial discussed some of the built-in help features in emacs. One that isn’t mentioned is the way to get to the entire “reference manual” for emacs. The “info viewer” gives you access to extensive documents about emacs (and about a number of other programs as well, as authors of many other programmers have found the info viewer a convenient way to package on-line documentation.) The commands C-h i or M-x info will launch the info viewer, and the first page of the viewer gives basic instructions on how to use it.

Finally, I have an emacs command reference sheet on the Resources page.

2 vim

If you are happy with emacs, you can skip this section.

But if you develop an incurable allergy to emacs, there are other editors that offer reasonable support to programmers. Some of the textbooks discuss vi, a popular editor that does not offer much support for programming, but a reasonable option is vim (“vi improved”).

Like emacs, vim is available on many, but not all, Unix systems and, once you have learned it, you can use it over telnet via keyboard commands. To learn the basics of running vim, give the command vimtutor.

Example 4: Try This: The vim tutorial

Give the command

vimtutor

Follow the instructions given there to learn the basic operation of vim.

Now, let’s try it out.

Example 5: Try This: Edit with vim

You should still have a snark.txt file in your playing directory.

cd ~/playing
vim snark.txt
  1. Try using your arrow keys and Page Up/Page Down keys to move around. These should work just as well as the movement keys covered in the tutorial.

  2. Use the search (?) command to hunt for the word “muffins”.

  3. Position your cursor just after that word. Type ‘i’ to enter “Insert mode”. Insert the text “ and jam”. Use the Esc key to exit Insert mode.

  4. Use the gg command to return to the first line of the file.

  5. Use the replace (:%s/old/new/igc) command to replace all occurrences of “snark” by “snipe”. Notice as you go how vim treats the various upper/lower case variations of “snark”.

  6. The “Undo” command in vim is u. Use it (repeatedly) to undo all of your replacements.

  7. Use the gg command again to return to the first line of the file.

  8. Use the replace (:%s/old/new/gc) command to replace all occurrences of “snark” by “snipe”. Notice as you go how vim treats the various upper/lower case variations of “snark”.

5.Use the command :w snark4.txt command to save your changes as a new file snark4.txt.

  1. Use the command “:q!” to exist vim.

  2. To see that you actually made a change, use diff and grep commands to examine the file:

    diff snark.txt snark4.txt
    grep -i snark snark4.txt
    grep -i snipe snark4.txt
    

    You should see your snipe and jam changes.

2.1 vim and C++

Example 6: Try this: C++ in vim
cd ~/playing
cp ~cs252/Assignments/textFiles/hello.cpp .
vim hello.cpp

vim will take note of the .cpp file extension and start this file in C++ mode.

If your terminal program supports color, you may see colored “syntax highlighting” as emacs renders reserved words, string constants, and comments in different colors and/or fonts.

And there are little, more subtle things that you will come to appreciate over time. Try typing a for loop

for (int i = 1; i <= 10; ++i)
  {
    cout << i << endl;
  }

just after the existing cout <<… statement.

  • Notice as you type the “)” and “}” in your code, the cursor briefly flashes back to the “(” or “{” that you just closed.

Move your cursor back up to the endl and delete that word. Now type just the e and, while still in Insert mode, then do ^P.

  • The commands ^P and ^N are special friends to all programmers who use long variable names but hate to type them. Type a few letters of any word, then hit one of those control keys. vim will search backwards (^P) or forwards (^N) through what you have previously typed looking for a word beginning with those letters. When it finds one, it fills in the remaining letters. If that wasn’t the word you wanted, just hit the same key again and vim will search for a different word beginning with the same characters.