We already know what a command line is.
A command line text editor is a text editor that runs directly from the command line, and does not require a graphical user interface, or even a separate window.
Command line text editors only edit plain text files.
The most common examples of command line text editors are Ed, Pico, Nano, Emacs and Vi.
Each editor presents is own set of challenges and rewards; it is not necessary to choose a favorite, but it is a good idea to be briefly introduced to each one.
The Ed Text Editor
Ed is a very old, very basic interactive command line text editor.
To open an (existing) file, type ed filename.txt
.
Existing text in the file will not be displayed.
To add text to the file, type a or i and then Enter so that the insert/append letter is shown on it’s own line. This will enter into insert/append mode.
After typing in what you want added to the file (can be multiple lines, etc.) put a single period . on it’s own line and then Enter. This will indicate that you are finished with insert/append mode.
To save, type a single w on it’s own line and hit Enter.
To exit, type a single q on it’s own line and hit Enter.
To view the contents of the file, type ,p and hit Enter.
Instead of the man page, type info ed
for full usage details.
The Pico and Nano Text Editors
The Pico text editor is a very basic command line text editor. Nano is an enhanced Pico clone.
On most systems, one of the two editors should be available, and if not, should be able to be installed. On my system, Pico is not installed by default, but Nano is.
To edit a file with Pico or Nano, simply type in the editor name, followed by the name of the file to edit. (A new, empty file will be created, if the file name is not found to already exist.)
pico filename.txt
or nano filename.txt
Once the editor is opened to your file, simply begin typing, as you would in any editor. Use the arrow keys to navigate.
At the bottom of the editor are multiple options, many of which are preceded by a ^ caret symbol that stands for the Ctrl key. Hold down the Ctrl key while pressing each letter, to select the option in question.
If changes to the file have not been saved at the time of exit, you will be prompted to save before exiting.
The Emacs Text Editor
Emacs is a very powerful command line text editor.
To edit a file, type in emacs
, followed by the name of the file to edit: emacs popcorn.txt
The file will open, and can be edited right away.
When using Emacs, some tasks are performed — and options selected — by key combinations.
Instances of C-
Instances of M-
In cases that using the Alt key does not work, the Esc key can be substituted by pressing it — and releasing it — before pressing the next character.
Some common Emacs commands are:
C-h for help.
C-x or C-x to exit.
C-x or C-s to save.
C-p will move the cursor to the previous line.
C-n will move the cursor to the next line.
C-b will move the cursor back one character.
C-f will move the cursor forward one character.
M-b or C-< will move the cursor back one word. M-f or C-> will move the cursor forward one word.
C-a or home will move the cursor to the beginning of the line.
C-e or end will move the cursor to the end of the line.
Page up will move the cursor up a page.
Page down will move the cursor down a page.
M-< will move the cursor to the beginning of the file. M-> will move the cursor to the end of the file.
C-d will delete a character.
M-d will delete a word.
C-k will kill (cut) the remainder of a line.
C-y will yank (paste) a line.
C-x u will undo an action.
C-s will search forward in the file.
C-r will search backward in the file.
The arrow keys can also be used for navigation.
For a full Emacs tutorial, try C-h t.
The Vi/Vim Text Editor(s)
The VI editor is perhaps the most widely-used command line text editor, as it is the default on many systems.
It is also my favorite command line text editor, and as such, I will cover it in detail — next week.
No Comments Yet