[ 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 

    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 and Deleting text, Section Later sections explain advanced features, but they are not strictly necessary, though often more efficient and less tedious. 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. 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. Sophisticated movement

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


Move to the start of the next word


Move to the end of the next word


Move to the end of the next word before a space


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


Move to the start of the next line


Move to the start of the previous line


Move to the end of the file


Move to the start of the file


Move to line number n


Display the current line number


Top line of the screen


Middle line of the screen


Bottom of the screen


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.


Scroll forward a screen


Scroll backward a screen


Scroll down half a screen


Scroll down half a screen 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. 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:


Append to the right of the cursor


Append at the end of the line


Insert text to the left of the cursor


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


Open a new line below the current line and insert text


Open a new line above the current line and insert text

Deletion commands:


Delete the character under the cursor


Delete from the current position to the end of the word


Delete the current line.


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:


Deletes n words (ndw works too)


Delete from the current position to the end of the file


Delete from the current postion to the start of the file


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


Delete from current line the end of the nth line

Undo commands:


Undo the last command


Undo all change to the current line


"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:


Replace the character under the cursor with c


Overwrites text


Changes the current word


Changes text from current position to end of the line


Changes next n words.(same as ncw)


Changes to the end of the nth line


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


Changes the current line


Substitutes text you type for the current character


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:


Yank a copy of the current line


Yank the next n lines


Yank a word


Yank n words


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

Paste commands:


Paste to the right of the cursor


Paste to the left of the cursor


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.


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


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


Move right to character before the next c.


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.


Searches right and down for the next occurence of text.


Searches left and up for the next occurance of text.


Repeat the last/ or ? command


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