Debian Tutorial - 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 hasedit, Windows has Notepad, and 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
- 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.
viis 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. - 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.
- 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. - :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
viyou'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 quitvi. So you should be back at the shell prompt. - cat testfile cat will display the file you typed on the screen.
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, subsubsection 8.3.2.1 and Deleting text, subsubsection 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'svi 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.- vi testfile Open the file you created earlier with
vi. You should see the text you typed before. - Move around the file with the arrow keys or the hjkl keys. If you try to move to far in any direction,
viwill beep and refuse to do so; if you want to put text there, you have to use an insertion command like i or a. - :q Exit
vi.
8.3.2.2 Deleting text
- vi testfile Open your practice file again.
- dd The dd command deletes a line; the top line of the file should be gone now.
- 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. Somevivariants, such asvimwill let you use backspace and delete. - 10x If you type a number before a command, it will repeat the command that many times. So this will delete 10 characters.
- 2dd You can use a number with the dd command as well, deleting two lines.
- :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:
- :q! With an exclamation point, you tell
vithat 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. - 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!.
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
- 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
FIXME move to appendix?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
- 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
- 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
- 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.
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
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
- 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
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 orginal command.
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
The text in the command \key{/} or \key{?} is actually a regular expression, see Regular expressions, section 11.1.
[back] [Copyright Notice] [Contents] [next]
Debian Tutorial
11 December 1998
Havoc Pennington hp@debian.org
http://www-rohan.sdsu.edu/doc/debian/ch-editor.html