A text file is simply a normal file that happens to contain human-readable text. There's nothing special about it otherwise. The other kind of file, a binary file, is meant to be interpreted by the computer.
You can view either kind of file with the less
file pager, if you
have it installed (install it if you haven't, it's quite useful). Type
less /etc/profile to view a sample text file --- notice that you
can read the characters, even if their meaning is obscure. Type less
/bin/ls to view a binary file; as you can see, the ls
program is not meant to be read by humans.
The difference between the two kinds of files is purely a matter of what they contain, unlike some other systems (such as DOS or MacOS) which actually treat the files differently.
Text files can contain shell scripts, documentation, copyright notices, or any other human-readable text.
Incidentally, this illustrates the difference between source code and binary executables. /bin/ls is a binary executable you can download from Debian, but you can also download a text file which tells the computer how to create /bin/ls. This text file is the source code. Comparing /bin/ls to /etc/profile illustrates how important source code is if someone wants to understand and modify a piece of software. Free software provides you or your consultants with this all-important source code.
A text editor is a program used to create and change the contents of
text files. Most operating systems have a text editor; DOS has
edit
, Windows has Notepad
, MacOS has
SimpleText
.
Debian provides a bewildering variety of text editors. vi
and
emacs
are the classic two, probably both the most powerful and the
most widely used. Both vi
and emacs
are quite
complex and require some practice, but they can make editing text extremely
efficient. emacs
runs both in a terminal and under the X Window
System; vi
normally runs in a terminal but the vim
variant has a -g option which allows it to work with X.
Simpler editors include nedit
, ae
, jed
,
and xcoral
. nedit
and xcoral
provide
easy-to-use X Window System graphical interfaces. There are also several
vi
variants, and an Emacs variant called XEmacs
.
This tutorial will not cover the use of any particular editor in detail, though
we will briefly introduce vi
since it is small, fast, nearly
always available, and you may need to use it sometime regardless of your
preferred editor. Emacs provides an excellent interactive tutorial of its own;
to read it, load Emacs with the emacs command and type F1
t. Emacs is an excellent choice for new users interested in a
general-purpose or programming editor.
vi
vi
(pronounced "vee eye") is really the only editor that
comes with almost every Unix-like operating system, and Debian is no exception.
vi
was originally written at the University of California at
Berkeley. The editor's name is short for "visual", referring to the
fact that vi
provides a visual display of the text file; this was
once considered a unique feature, giving you an idea how old the program is.
vi
is somewhat hard to get used to, but has many powerful
features. In general, we suggest that a new user use Emacs for daily tasks
such as programming. However, vi
is sometimes more convenient or
the only available editor; it is also a much smaller file to download.
The following discussion of vi
should also apply to
vi
variants such as elvis
and vim
.
In your home directory, invoke vi by typing vi followed by the
name of the file you wish to create. You will see a screen with a column of
tildes (~) along the left side. vi
is now in command
mode. Anything you type will be understood as a command, not as content to add
to the file. In order to input text, you must type a command.
The two basic input commands are i, which means "insert the text I'm about to type to the left of the cursor", and a, which means "append the text I'm about to type to the right of the cursor". Since you are at the beginning of an empty file, either of these would work. We picked i arbitrarily.
And what, then, is belief? It is the demi-cadence which closes a musical phrase in the symphony of our intellectual life. We have seen that it has just three properties: First, it is something that we are aware of; second, it appeases the irritation of doubt; and, third, it involves the establishment in our nature of a rule of action, or, say for short, a habit.
Press RET after each line, since vi will not move to the next line automatically; when you finish typing, press the ESC key to leave insert or append mode and return to command mode.
If you've done everything correctly, when you type this command it should
appear at the bottom of your screen, below all the ~ characters.
The : tells vi
you're about to give a series of
commands; the w means to write the file you've just typed in ---
in most new programs this is called "save" --- and the q
means to quit vi
. So you should be back at the shell prompt.
cat will display the file you typed on the screen.
Don't remove testfile, we'll use it in the next tutorial section.
As you use vi
, always remember that pressing ESC will
return you to command mode. So if you get confused, press ESC a
couple times and start over.
vi
has an annoying tendency to beep whenever you do something you
aren't supposed to, like type an unknown command; don't be alarmed by this.
To use vi, you only need to read Moving around in a file, Section 8.3.2.1 and Deleting text, Section 8.3.2.2. Later sections explain advanced features, but they are not strictly necessary, though often more efficient and less tedious.
To move around in a file, Debian's vi
allows you to use the arrow
keys. The traditional keys also work, however; they are h for
left, j for down, k for up, and l for
right. These keys were chosen because they are adjacent on on the home row of
the keyboard, and thus easy to type. Many people use them instead of the arrow
keys since they're faster to reach with your fingers.
Open the file you created earlier with vi
. You should see the
text you typed before.
vi
will beep and refuse to do
so; if you want to put text there, you have to use an insertion command like
i or a.
Exit vi
.
Open your practice file again.
The dd command deletes a line; the top line of the file should be gone now.
x deletes a single character; the first letter of the second line
will be erased. Delete and backspace don't work in vi
, for
historical reasons[13]. Some
vi
variants, such as vim
will let you use backspace
and delete.
If you type a number before a command, it will repeat the command that many times. So this will delete 10 characters.
You can use a number with the dd command as well, deleting two lines.
This will cause an error, because you've changed the file but haven't saved yet. There are two ways to avoid this; you can :wq, thus writing the file as you quit, or you can quit without saving:
With an exclamation point, you tell vi
that you really mean it,
and it should quit even though the file isn't saved. If you use
:q! your deletions will not be saved to testfile; if
you use :wq, they will be.
Back at the shell prompt, view testfile. It should be shorter now, if you used :wq, or be unchanged if you used :q!.
:q! is an excellent command to remember, because you can use it to
bail out if you get hopelessly confused and feel you've ruined the file you
were editing. Just press ESC a few times to be sure you're in
command mode and then type :q!. This is guaranteed to get you out
of vi
with no damage done.
You now know everything you need to do basic editing; insertion, deletion, saving, and quitting. The following sections describe useful commands for doing things faster; you can skip over them if you like.
There are many motion commands, here's a quick summary:
The screen will automatically scroll when the cursor reaches either the top or the bottom of the screen. There are alternative commands which can control scrolling the text.
As mentioned above you can often prefix a command with a number to repeat that command multiple times. For example, the l key moves left; 10l moves you left 10 positions to the left.
If you wanted to enter a number of spaces in front of the some text you could use a number with the insert command. Enter the number n then i followed by SPACE and ESC. You should get n spaces.
The commands that deal with lines use a number to refer to line numbers. The G is a good example; if you preface it with a number it will go to that line.
This section gives a more comprehensive list of commands you can use. It is just a reference; if you want, try the commands out to see what they do.
Insertion commands:
Deletion commands:
Commands in combination can be more powerful. In particular, d followed by a motion command deletes from the cursor to wherever you asked to move. Some examples:
Undo commands:
You can undo an undo, so uu results in an undone undo, or no change.
Replacement commands:
The commands in the above list which allow you to enter more than a single character of text have to be exited with the ESC key, returning you to command mode.
Cut and paste involves first yanking (cutting or copying) some text and placing it in a buffer (or "clipboard"); then moving to the desired new location; then pasting the text.
To cut text use the y command and its variants:
Paste commands:
When using vi
within an xterm or using a variant of
vi
that supports X, you can also use the mouse to copy text. See
The X Window System, Chapter 10 for how to copy and
paste in X; be sure you're in insert mode when you paste, or the pasted text
will be interpreted as a command.
When you delete, the deleted text is copied to the buffer (clipboard); you can then use the paste commands. This allows you to cut-and-paste, while the y commands result in copy-and-paste.
vi
has commands to search for text. You can also use these as
movement commands, if you want to move to a particular word or character.
The simplest search commands look for characters.
If the character you were searching for was not found, vi
will
beep or give some other sort of signal. vi
allows you to search
for any text, not just a character.
When using the / or ? commands a line will be cleared along the bottom of the screen. You enter the text to search for followed by RET.
The text in the command / or ? is actually a regular expression, see Regular expressions, Section 11.1.
Debian Tutorial
[email protected]