CLI Magic: An introduction to CLI editors

86

Author: Jem Matzan

At some point in your GNU/Linux or BSD adventures, you’re going to have to use a command-line text editor. Some of them are pretty easy to use but have few features. Others are powerful but require a study session with the tutorial file to learn how to operate them. Most often you just need a text editor to edit a couple of config files and don’t want to spend 45 minutes working through a tutorial. How about a crash course to get you going?

All text editors share two common qualities: they will open a file if you type its path and name in after the program name, and they will reserve changing the file until you tell it to. So to edit your /etc/shells file with any of the editors in this article, you’d type something like this:

nano /etc/shells

or if you’re already in the /etc directory, you can forgo the path:

nano shells

Once in the editor, none of your changes will be applied until you save the file. If you try to quit before you’ve saved the file, the editor will usually ask you if you want to save it first.

The easy ones

In GNU/Linux, you can sometimes find simple command line text editors like Pico and its GNU brother Nano.

Pico is an acronym for the PIne COmposer — Pine being a command-line email client that is sometimes installed by default in GNU/Linux distributions. If you have Pine, you have Pico. Pico and Nano share most of their commands, and are pretty easy to use:

  • To save a file, press Ctrl-o
  • To quit, press Ctrl-x
  • To perform a simple search, press Ctrl-w and type in the search string

You do not have to do anything to start editing text once the program is open — just start typing as you would in a word processor. If a line is too long for your terminal window, it will have a $ at the end of the line and you’ll have to use the arrow keys to scroll over to the latter portion of the line. You can also move down to the line below the extended line and hit the left arrow, which will bring you to the last character in the previous line.

You’ll probably notice that the most important commands are listed in a bar at the bottom of the terminal screen, so if you forget what command does what, you can glance down and find out.

FreeBSD has a simple editor called ee in the base system, and it’s extremely easy to use. Just press the Escape key and a menu will pop up offering you various file functions. To get rid of the menu, hit the Esc key again. As with Nano and Pico, you just start typing once the program is open.

To start Nano, Pico, or ee, just type its name in lowercase letters at the command line and press Enter.

vi and Emacs all over

The vi editor is practically universal, especially in the Unix and BSD worlds. Many operating systems package either vi or an enhanced edition of vi — usually Vim — with their base system. vi is much more difficult to learn and use than Pico, Nano, or ee, but it is able to do more. Many people use vi as a tool for writing and editing programs and Web pages and composing email as well as everyday editing.

Unlike the easier editors, vi won’t let you start typing right away. Here are the commands you need to know to use vi and Vim for quick editing jobs:

  • Press i to enter “insert mode,” which allows you to type and erase text
  • Press Escape to exit “insert mode”
  • Press colon and then type w and press Enter to save a file, as in:

:w

  • Press the d key twice to delete a line
  • Press the / key and then enter a search string to perform a search
  • Press the o key to skip to the next line, insert a line break, and enter “insert mode”
  • Type in :q and press enter to quit
  • To save and then quit, type :wq and press Enter
  • To quit without saving, type :q! and press Enter

Remember to exit insert mode before you try to execute other commands.

Emacs is a similarly archaic editor, but its command execution structure is totally different. Instead of typing letters or prefacing a command with a colon, Emacs works with key combinations. Once you open a file you can just start typing and editing, so it’s a little bit easier to use if you’re not used to complex text editors. Here are the pertinent commands:

  • Press Ctrl-x and then Ctrl-s to save a file
  • Press Ctrl-x and then Ctrl-c to quit
  • Press Ctrl-s and then type in a search string to perform a search

Vim, vi, and Emacs are all started from the command line by typing their name in lowercase letters and pressing Enter. If lines are too long for the screen, Emacs will put a at the end of the line and continue it below the line. vi will not mark the end of the too-long line, but will continue it below that line.