[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ 14 ] [ 15 ] [ 16 ] [ 17 ] [ 18 ] [ A ] [ B ] [ C ] [ D ] [ next ]


Debian Tutorial (Obsolete Documentation)
Chapter 8 - Creating and editing text files


8.1 What's a text file?

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.


8.2 Text editors

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.


8.3 Creating and editing a text file with 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.


8.3.1 Creating a file

  1. vi testfile

    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.

  1. i

    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.

  1. Type in some text; here's a profound statement from philosopher Charles Sanders Peirce, if you can't think of your own:

         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.

  1. :wq

    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.

  1. cat testfile

    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.


8.3.2 Editing an existing file

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.


8.3.2.1 Moving around in a file

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.

  1. vi testfile

    Open the file you created earlier with vi. You should see the text you typed before.

  1. Move around the file with the arrow keys or the hjkl keys. If you try to move to far in any direction, 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.

  1. :q

    Exit vi.


8.3.2.2 Deleting text

  1. vi testfile

    Open your practice file again.

  1. dd

    The dd command deletes a line; the top line of the file should be gone now.

  1. x

    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.

  1. 10x

    If you type a number before a command, it will repeat the command that many times. So this will delete 10 characters.

  1. 2dd

    You can use a number with the dd command as well, deleting two lines.

  1. :q

    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:

  1. :q!

    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.

  1. cat testfile

    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.


8.3.2.3 Sophisticated movement

There are many motion commands, here's a quick summary:

w

Move to the start of the next word

e

Move to the end of the next word

E

Move to the end of the next word before a space

b

Move to the start of the previous word

0 (zero)

Move to the start of the line

^

Move to the first word of the current line

$

Move to the end of the line

RET

Move to the start of the next line

-

Move to the start of the previous line

G

Move to the end of the file

1G

Move to the start of the file

nG

Move to line number n

C-G

Display the current line number

H

Top line of the screen

M

Middle line of the screen

L

Bottom of the screen

n|

Move cursor to column n

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.

C-f

Scroll forward a screen

C-b

Scroll backward a screen

C-d

Scroll down half a screen

C-u

Scroll down half a screen


8.3.2.4 Repeating commands

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.


8.3.2.5 Advanced reference

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:

a

Append to the right of the cursor

A

Append at the end of the line

i

Insert text to the left of the cursor

I

Insert text to the left of the first non-blank character on current line

o

Open a new line below the current line and insert text

O

Open a new line above the current line and insert text

Deletion commands:

x

Delete the character under the cursor

dw

Delete from the current position to the end of the word

dd

Delete the current line.

D

Delete from the current position to the end of the line

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:

dnw

Deletes n words (ndw works too)

dG

Delete from the current position to the end of the file

d1G

Delete from the current postion to the start of the file

d$

Delete from current postion to the end of the line (same as D)

dn$

Delete from current line the end of the nth line

Undo commands:

u

Undo the last command

U

Undo all change to the current line

:e!

"Edit again". Like quitting with :q! and restarting --- returns you to the last time you did a :w to save.

You can undo an undo, so uu results in an undone undo, or no change.

Replacement commands:

rc

Replace the character under the cursor with c

R

Overwrites text

cw

Changes the current word

c$

Changes text from current position to end of the line

cnw

Changes next n words.(same as ncw)

cn$

Changes to the end of the nth line

C

Changes to the end of the line (same as c$)

cc

Changes the current line

s

Substitutes text you type for the current character

ns

Substitutes text you type for the next n characters

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:

yy

Yank a copy of the current line

nyy

Yank the next n lines

yw

Yank a word

ynw

Yank n words

y$

Yank the text between the cursor and the end of the line

Paste commands:

p

Paste to the right of the cursor

P

Paste to the left of the cursor

nP

Paste n copies to the left of the cursor

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.

fc

Find the next character c to the right of or below the current position

Fc

Find the next character c to the left of or above the current position

tc

Move right to character before the next c.

Tc

Move left to the character following the preceding c.

;

Repeats the last character search command

,

Same as ; but reverses the direction of the original command.

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.

/text

Searches right and down for the next occurence of text.

?text

Searches left and up for the next occurance of text.

n

Repeat the last/ or ? command

N

Repeats the last / or ? in the reverse direction

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.


[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ 14 ] [ 15 ] [ 16 ] [ 17 ] [ 18 ] [ A ] [ B ] [ C ] [ D ] [ next ]


Debian Tutorial (Obsolete Documentation)

29 Dezember 2009

Havoc Pennington hp@debian.org