old-www/LDP/www.debian.org/doc/manuals/debian-tutorial/ch-editor.html

1310 lines
38 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Debian Tutorial (Obsolete Documentation) - Creating and editing text files</title>
<link href="index.html" rel="start">
<link href="ch-files.html" rel="prev">
<link href="ch-custom_shell.html" rel="next">
<link href="index.html#contents" rel="contents">
<link href="index.html#copyright" rel="copyright">
<link href="ch-about.html" rel="chapter" title="1 About this manual">
<link href="ch-introduction.html" rel="chapter" title="2 Introduction">
<link href="ch-start.html" rel="chapter" title="3 Getting started">
<link href="ch-basics.html" rel="chapter" title="4 The Basics">
<link href="ch-docs.html" rel="chapter" title="5 Reading documentation and getting help">
<link href="ch-shell.html" rel="chapter" title="6 Using the shell">
<link href="ch-files.html" rel="chapter" title="7 More on files">
<link href="ch-editor.html" rel="chapter" title="8 Creating and editing text files">
<link href="ch-custom_shell.html" rel="chapter" title="9 Customizing the shell">
<link href="ch-X.html" rel="chapter" title="10 The X Window System">
<link href="ch-text_tools.html" rel="chapter" title="11 Text tools">
<link href="ch-file_tools.html" rel="chapter" title="12 File tools">
<link href="ch-disks.html" rel="chapter" title="13 Using disks">
<link href="ch-dpkg.html" rel="chapter" title="14 Removing and installing software">
<link href="ch-troubleshooting.html" rel="chapter" title="15 Troubleshooting">
<link href="ch-advanced.html" rel="chapter" title="16 Advanced topics">
<link href="ch-next.html" rel="chapter" title="17 Where to from here?">
<link href="ch-helping.html" rel="chapter" title="18 Contributing to Debian: How can I help?">
<link href="ap-apps_appendix.html" rel="appendix" title="A A brief survey of available applications">
<link href="ap-components_appendix.html" rel="appendix" title="B Summary of GNU/Linux system components">
<link href="ap-booting_appendix.html" rel="appendix" title="C Appendix C: Booting the system">
<link href="ap-misc_appendix.html" rel="appendix" title="D Miscellaneous">
<link href="ch-about.html#s-acknowldgements" rel="section" title="1.1 Acknowledgements">
<link href="ch-introduction.html#s-introduction-debian" rel="section" title="2.1 What is Debian?">
<link href="ch-introduction.html#s-introduction-DFSG" rel="section" title="2.2 What's free software?">
<link href="ch-introduction.html#s-introduction-how" rel="section" title="2.3 How to Read This Book">
<link href="ch-introduction.html#s-introduction-ldp" rel="section" title="2.4 The Linux Documentation Project">
<link href="ch-start.html#s-start-multi" rel="section" title="3.1 A multiuser, multitasking operating system">
<link href="ch-start.html#s-start-login" rel="section" title="3.2 Logging in">
<link href="ch-start.html#s-start-keys" rel="section" title="3.3 Keys">
<link href="ch-start.html#s-start-commandline" rel="section" title="3.4 Command history and editing the command line">
<link href="ch-start.html#s-start-root" rel="section" title="3.5 Logging in as root">
<link href="ch-start.html#s-start-VC" rel="section" title="3.6 Virtual consoles">
<link href="ch-start.html#s-start-shutdown" rel="section" title="3.7 Shutting down">
<link href="ch-basics.html#s-basics-commandline" rel="section" title="4.1 The command line and <samp>man</samp> pages">
<link href="ch-basics.html#s-basics-files" rel="section" title="4.2 Files and Directories">
<link href="ch-basics.html#s-basics-processes" rel="section" title="4.3 Processes">
<link href="ch-basics.html#s-basics-shell" rel="section" title="4.4 The shell">
<link href="ch-basics.html#s-basics-jobs" rel="section" title="4.5 Managing processes with Bash">
<link href="ch-basics.html#s-basics-bash" rel="section" title="4.6 A few Bash features">
<link href="ch-basics.html#s-basics-identity" rel="section" title="4.7 Managing your identity">
<link href="ch-docs.html#s-docs-sources" rel="section" title="5.1 Kinds of documentation">
<link href="ch-docs.html#s-docs-info" rel="section" title="5.2 Using info">
<link href="ch-docs.html#s-docs-textfiles" rel="section" title="5.3 Viewing text files with more and less">
<link href="ch-docs.html#s-docs-howtos" rel="section" title="5.4 HOWTOs">
<link href="ch-docs.html#s-docs-support" rel="section" title="5.5 Getting help from a person">
<link href="ch-docs.html#s-docs-sysinfo" rel="section" title="5.6 Getting information from the system">
<link href="ch-shell.html#s-shell-variables" rel="section" title="6.1 Environment variables">
<link href="ch-shell.html#s-shell-path" rel="section" title="6.2 Where commands live: the <samp>PATH</samp> variable">
<link href="ch-shell.html#s-custom_shell-aliases" rel="section" title="6.3 Aliases and shell functions">
<link href="ch-shell.html#s-shell-io" rel="section" title="6.4 Controlling input and output">
<link href="ch-shell.html#s-shell-modifiers" rel="section" title="6.5 Specifying how and when to run commands">
<link href="ch-shell.html#s-shell-wildcards" rel="section" title="6.6 Filename expansion (&quot;Wildcards&quot;)">
<link href="ch-shell.html#s6.7" rel="section" title="6.7 Interactive/non-interactive">
<link href="ch-files.html#s-files-permissions" rel="section" title="7.1 Permissions">
<link href="ch-files.html#s-files-tour" rel="section" title="7.2 What files are on my system? Where can I put my own files?">
<link href="ch-files.html#s-file-manager" rel="section" title="7.3 Using a filemanager">
<link href="ch-editor.html#s-editor-textfile" rel="section" title="8.1 What's a text file?">
<link href="ch-editor.html#s-editor-editor" rel="section" title="8.2 Text editors">
<link href="ch-editor.html#s-editor-vi" rel="section" title="8.3 Creating and editing a text file with <code>vi</code>">
<link href="ch-custom_shell.html#s-custom_shell-dotfiles" rel="section" title="9.1 .rc files and <samp>ls -a</samp>">
<link href="ch-custom_shell.html#s-custom_shell-etc" rel="section" title="9.2 System-wide vs. user-specific configuration">
<link href="ch-X.html#s-X-starting" rel="section" title="10.1 Starting the X environment">
<link href="ch-X.html#s-X-intro" rel="section" title="10.2 Intro: What is X">
<link href="ch-X.html#s-X-basics" rel="section" title="10.3 Basic X operations">
<link href="ch-X.html#s-X-startup" rel="section" title="10.4 Customizing your X startup">
<link href="ch-text_tools.html#s-text_tools-regexps" rel="section" title="11.1 Regular expressions">
<link href="ch-file_tools.html#s-file_tools-backup" rel="section" title="12.1 Backup tools">
<link href="ch-file_tools.html#s-file_tools-compression" rel="section" title="12.2 File compression with <samp>gzip</samp>">
<link href="ch-file_tools.html#s-file_tools-split" rel="section" title="12.3 Splitting files into smaller pieces">
<link href="ch-file_tools.html#s-file_tools-find" rel="section" title="12.4 Finding files">
<link href="ch-file_tools.html#s-file_tools-file" rel="section" title="12.5 Determining a file's contents">
<link href="ch-disks.html#s-disks-concepts" rel="section" title="13.1 Concepts">
<link href="ch-disks.html#s-disks-mount" rel="section" title="13.2 <samp>mount</samp> and <samp>/etc/fstab</samp>">
<link href="ch-disks.html#s-network-ppp" rel="section" title="13.3 PPP">
<link href="ch-dpkg.html#s-dpkg-dpkg" rel="section" title="14.1 The <code>dpkg</code> package utility">
<link href="ch-dpkg.html#s-dpkg-what" rel="section" title="14.2 What a package maintenance utility does">
<link href="ch-dpkg.html#s-dpkg-apt" rel="section" title="14.3 Apt">
<link href="ch-dpkg.html#s-dpkg-dselect" rel="section" title="14.4 Using dselect.">
<link href="ch-dpkg.html#s-dpkg-commandline" rel="section" title="14.5 Using dpkg manually">
<link href="ch-dpkg.html#s-dpkg-compiling" rel="section" title="14.6 Compiling software yourself">
<link href="ch-dpkg.html#s-dpkg-proprietary" rel="section" title="14.7 Proprietary software not in .deb format">
<link href="ch-troubleshooting.html#s-troubleshooting-frozen" rel="section" title="15.1 Debian is frozen or crashed!">
<link href="ch-troubleshooting.html#s-troubleshooting-termgarbage" rel="section" title="15.2 My terminal isn't behaving properly">
<link href="ch-troubleshooting.html#s-troubleshooting-vi" rel="section" title="15.3 The computer beeps when I press a key, and my screen shows a text file and/or a lot of <samp>~</samp> symbols.">
<link href="ch-advanced.html#s-advanced-scripting" rel="section" title="16.1 Introduction to shell scripting">
<link href="ch-advanced.html#s-advanced-files" rel="section" title="16.2 Advanced files">
<link href="ch-advanced.html#s-advanced-kernel" rel="section" title="16.3 Compiling the kernel">
<link href="ch-advanced.html#s-advanced-security" rel="section" title="16.4 A few words on security">
<link href="ch-advanced.html#s-advanced-programming" rel="section" title="16.5 Programming on Linux">
<link href="ch-next.html#s-next-manuals" rel="section" title="17.1 Other Debian manuals to read">
<link href="ch-next.html#s-next-resources" rel="section" title="17.2 Other resources">
<link href="ch-helping.html#s-helping-bugs" rel="section" title="18.1 Submit bug reports">
<link href="ch-helping.html#s-helping-other" rel="section" title="18.2 Other things">
<link href="ap-apps_appendix.html#s-A_apps-whatever" rel="section" title="A.1 (Subheadings could copy the structure of the menu system)">
<link href="ap-misc_appendix.html#s-unix-history" rel="section" title="D.1 Unix History">
<link href="ap-misc_appendix.html#sD.2" rel="section" title="D.2 GNU/Linux History">
<link href="ap-misc_appendix.html#sD.3" rel="section" title="D.3 The Linux kernel's version numbering">
<link href="ch-introduction.html#s-introduction-debian-os" rel="subsection" title="2.1.1 What's an operating system, and what sort of operating system is Debian?">
<link href="ch-introduction.html#s-introduction-debian-how" rel="subsection" title="2.1.2 Who creates Debian?">
<link href="ch-basics.html#s-basics-commandline-description" rel="subsection" title="4.1.1 Describing the command line">
<link href="ch-basics.html#s-basics-files-intro" rel="subsection" title="4.2.1 Introduction to files">
<link href="ch-basics.html#s-basics-files-using" rel="subsection" title="4.2.2 Using files: a tutorial">
<link href="ch-basics.html#s-basics-bash-completion" rel="subsection" title="4.6.1 Tab Completion">
<link href="ch-docs.html#s-docs-support-posting" rel="subsection" title="5.5.1 Dos and Don'ts of asking a question">
<link href="ch-shell.html#s-shell-variables-parents" rel="subsection" title="6.1.1 Parent and child processes">
<link href="ch-shell.html#s-interactive-shells" rel="subsection" title="6.7.1 Interactive shells">
<link href="ch-files.html#s-files-permissions-groups" rel="subsection" title="7.1.1 File Ownership">
<link href="ch-files.html#s-files-permissions-mode" rel="subsection" title="7.1.2 Mode">
<link href="ch-files.html#s-files-permissions-tutorial" rel="subsection" title="7.1.3 Permissions in practice">
<link href="ch-editor.html#s-editor-vi-creating" rel="subsection" title="8.3.1 Creating a file">
<link href="ch-editor.html#s-editor-vi-editing" rel="subsection" title="8.3.2 Editing an existing file">
<link href="ch-editor.html#s-editor-vi-editing-movement" rel="subsection" title="8.3.2.1 Moving around in a file">
<link href="ch-editor.html#s-editor-vi-editing-deletion" rel="subsection" title="8.3.2.2 Deleting text">
<link href="ch-editor.html#s-editor-vi-editing-moremoving" rel="subsection" title="8.3.2.3 Sophisticated movement">
<link href="ch-editor.html#s-editor-vi-editing-numbers" rel="subsection" title="8.3.2.4 Repeating commands">
<link href="ch-editor.html#s-editor-vi-editing-advanced" rel="subsection" title="8.3.2.5 Advanced reference">
<link href="ch-X.html#s-X-basics-mouse" rel="subsection" title="10.3.1 The mouse">
<link href="ch-X.html#s-X-basics-clients" rel="subsection" title="10.3.2 X clients">
<link href="ch-X.html#s-X-basics-trouble" rel="subsection" title="10.3.3 Troubleshooting">
<link href="ch-X.html#s-X-basics-leaving" rel="subsection" title="10.3.4 Leaving the X environment">
<link href="ch-disks.html#s-disks-mount-mounting" rel="subsection" title="13.2.1 Mounting a filesystem">
<link href="ch-disks.html#s-disks-mount-cdromexample" rel="subsection" title="13.2.2 Example: Mounting a CD-ROM">
<link href="ch-disks.html#s-disks-mount-fstab" rel="subsection" title="13.2.3 <samp>/etc/fstab</samp>: Automating the mount process">
<link href="ch-disks.html#s-disks-mount-removable" rel="subsection" title="13.2.4 Removable disks (floppies, Zip disks, etc.)">
<link href="ch-disks.html#s-network-ppp-introduction" rel="subsection" title="13.3.1 Introduction">
<link href="ch-disks.html#s-network-ppp-preparation" rel="subsection" title="13.3.2 Preparation">
<link href="ch-disks.html#s-network-ppp-wvdial" rel="subsection" title="13.3.3 The Easy Way: <samp>wvdial</samp>">
<link href="ch-disks.html#s-network-ppp-pppd" rel="subsection" title="13.3.4 Doing It Manually">
<link href="ch-dpkg.html#s-apt-config" rel="subsection" title="14.3.1 Configuring Apt">
<link href="ch-dpkg.html#s-apt-usage" rel="subsection" title="14.3.2 Using apt-get.">
<link href="ch-advanced.html#s-advanced-scripting-why" rel="subsection" title="16.1.1 What and why">
<link href="ch-advanced.html#s-advanced-scripting-example" rel="subsection" title="16.1.2 A simple example">
<link href="ch-advanced.html#s-advanced-files-hardlinks" rel="subsection" title="16.2.1 The real nature of files: hard links and inodes">
<link href="ch-advanced.html#s-advanced-files-types" rel="subsection" title="16.2.2 Types of files">
<link href="ch-advanced.html#s-advanced-files-types-symlinks" rel="subsection" title="16.2.2.1 Symbolic links">
<link href="ch-advanced.html#s-files-misc-types-devices" rel="subsection" title="16.2.2.2 Device files">
<link href="ch-advanced.html#s-advanced-files-types-devices-null" rel="subsection" title="16.2.2.2.1 <samp>/dev/null</samp>">
<link href="ch-advanced.html#s-advanced-files-types-pipes" rel="subsection" title="16.2.2.3 Named pipes (FIFOs)">
<link href="ch-advanced.html#s-advanced-files-types-sockets" rel="subsection" title="16.2.2.4 Sockets">
<link href="ch-advanced.html#s-advanced-files-proc" rel="subsection" title="16.2.3 The <samp>proc</samp> filesystem">
<link href="ch-advanced.html#s-advanced-files-permissions" rel="subsection" title="16.2.4 Advanced aspects of file permissions">
<link href="ch-advanced.html#s-advanced-files-permissions-absolute" rel="subsection" title="16.2.4.1 Using numeric arguments with <samp>chmod</samp>">
<link href="ch-advanced.html#s-advanced-files-chattr" rel="subsection" title="16.2.5 chattr">
<link href="ch-advanced.html#s-advanced-files-copying" rel="subsection" title="16.2.6 Large-scale copying">
<link href="ch-advanced.html#s-advanced-files-undocumented" rel="subsection" title="16.2.7 Other concepts not yet covered, but should be">
</head>
<body>
<p><a name="ch-editor"></a></p>
<hr>
<p>
[ <a href="ch-files.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ <a href="ch-shell.html">6</a> ]
[ <a href="ch-files.html">7</a> ]
[ 8 ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ <a href="ch-advanced.html">16</a> ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-custom_shell.html">next</a> ]
</p>
<hr>
<h1>
Debian Tutorial (Obsolete Documentation)
<br>Chapter 8 - Creating and editing text files
</h1>
<hr>
<h2><a name="s-editor-textfile"></a>8.1 What's a text file?</h2>
<p>
A <em>text file</em> 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.
</p>
<p>
You can view either kind of file with the <code>less</code> file pager, if you
have it installed (install it if you haven't, it's quite useful). Type
<samp>less /etc/profile</samp> to view a sample text file --- notice that you
can read the characters, even if their meaning is obscure. Type <samp>less
/bin/ls</samp> to view a binary file; as you can see, the <samp>ls</samp>
program is not meant to be read by humans.
</p>
<p>
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.
</p>
<p>
Text files can contain shell scripts, documentation, copyright notices, or any
other human-readable text.
</p>
<p>
Incidentally, this illustrates the difference between <em>source code</em> and
<em>binary executables</em>. <samp>/bin/ls</samp> is a binary executable you
can download from Debian, but you can also download a text file which tells the
computer how to create <samp>/bin/ls</samp>. This text file is the source
code. Comparing <samp>/bin/ls</samp> to <samp>/etc/profile</samp> 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.
</p>
<hr>
<h2><a name="s-editor-editor"></a>8.2 Text editors</h2>
<p>
A <em>text editor</em> is a program used to create and change the contents of
text files. Most operating systems have a text editor; DOS has
<code>edit</code>, Windows has <code>Notepad</code>, MacOS has
<code>SimpleText</code>.
</p>
<p>
Debian provides a bewildering variety of text editors. <code>vi</code> and
<code>emacs</code> are the classic two, probably both the most powerful and the
most widely used. Both <code>vi</code> and <code>emacs</code> are quite
complex and require some practice, but they can make editing text extremely
efficient. <code>emacs</code> runs both in a terminal and under the X Window
System; <code>vi</code> normally runs in a terminal but the <code>vim</code>
variant has a <samp>-g</samp> option which allows it to work with X.
</p>
<p>
Simpler editors include <code>nedit</code>, <code>ae</code>, <code>jed</code>,
and <code>xcoral</code>. <code>nedit</code> and <code>xcoral</code> provide
easy-to-use X Window System graphical interfaces. There are also several
<code>vi</code> variants, and an Emacs variant called <code>XEmacs</code>.
</p>
<p>
This tutorial will not cover the use of any particular editor in detail, though
we will briefly introduce <code>vi</code> 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 <samp>emacs</samp> command and type <samp>F1
t</samp>. Emacs is an excellent choice for new users interested in a
general-purpose or programming editor.
</p>
<hr>
<h2><a name="s-editor-vi"></a>8.3 Creating and editing a text file with <code>vi</code></h2>
<p>
<code>vi</code> (pronounced &quot;vee eye&quot;) is really the only editor that
comes with almost every Unix-like operating system, and Debian is no exception.
<code>vi</code> was originally written at the University of California at
Berkeley. The editor's name is short for &quot;visual&quot;, referring to the
fact that <code>vi</code> provides a visual display of the text file; this was
once considered a unique feature, giving you an idea how old the program is.
</p>
<p>
<code>vi</code> 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, <code>vi</code> is sometimes more convenient or
the only available editor; it is also a much smaller file to download.
</p>
<p>
The following discussion of <code>vi</code> should also apply to
<code>vi</code> variants such as <code>elvis</code> and <code>vim</code>.
</p>
<hr>
<h3><a name="s-editor-vi-creating"></a>8.3.1 Creating a file</h3>
<ol type="1" start="1" >
<li>
<p>
<samp>vi testfile</samp>
</p>
<p>
In your home directory, invoke vi by typing <samp>vi</samp> followed by the
name of the file you wish to create. You will see a screen with a column of
tildes (<samp>~</samp>) along the left side. <code>vi</code> 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.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>i</samp>
</p>
<p>
The two basic input commands are <samp>i</samp>, which means &quot;insert the
text I'm about to type to the left of the cursor&quot;, and <samp>a</samp>,
which means &quot;append the text I'm about to type to the right of the
cursor&quot;. Since you are at the beginning of an empty file, either of these
would work. We picked <samp>i</samp> arbitrarily.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
Type in some text; here's a profound statement from philosopher Charles Sanders
Peirce, if you can't think of your own:
</p>
<pre>
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.
</pre>
<p>
Press <samp>RET</samp> after each line, since <samp>vi</samp> will not move to
the next line automatically; when you finish typing, press the <samp>ESC</samp>
key to leave insert or append mode and return to command mode.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>:wq</samp>
</p>
<p>
If you've done everything correctly, when you type this command it should
appear at the bottom of your screen, below all the <samp>~</samp> characters.
The <samp>:</samp> tells <code>vi</code> you're about to give a series of
commands; the <samp>w</samp> means to write the file you've just typed in ---
in most new programs this is called &quot;save&quot; --- and the <samp>q</samp>
means to quit <code>vi</code>. So you should be back at the shell prompt.
</p>
</li>
</ol>
<ol type="1" start="5" >
<li>
<p>
<samp>cat testfile</samp>
</p>
<p>
<samp>cat</samp> will display the file you typed on the screen.
</p>
</li>
</ol>
<p>
Don't remove <samp>testfile</samp>, we'll use it in the next tutorial section.
</p>
<p>
As you use <code>vi</code>, always remember that pressing <samp>ESC</samp> will
return you to command mode. So if you get confused, press <samp>ESC</samp> a
couple times and start over.
</p>
<p>
<code>vi</code> 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.
</p>
<hr>
<h3><a name="s-editor-vi-editing"></a>8.3.2 Editing an existing file</h3>
<p>
To use <samp>vi</samp>, you only need to read <a
href="#s-editor-vi-editing-movement">Moving around in a file, Section
8.3.2.1</a> and <a href="#s-editor-vi-editing-deletion">Deleting text, Section
8.3.2.2</a>. Later sections explain advanced features, but they are not
strictly necessary, though often more efficient and less tedious.
</p>
<hr>
<h4><a name="s-editor-vi-editing-movement"></a>8.3.2.1 Moving around in a file</h4>
<p>
To move around in a file, Debian's <code>vi</code> allows you to use the arrow
keys. The traditional keys also work, however; they are <samp>h</samp> for
left, <samp>j</samp> for down, <samp>k</samp> for up, and <samp>l</samp> 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.
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>vi testfile</samp>
</p>
<p>
Open the file you created earlier with <code>vi</code>. You should see the
text you typed before.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
Move around the file with the arrow keys or the <samp>hjkl</samp> keys. If you
try to move to far in any direction, <code>vi</code> will beep and refuse to do
so; if you want to put text there, you have to use an insertion command like
<samp>i</samp> or <samp>a</samp>.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>:q</samp>
</p>
<p>
Exit <code>vi</code>.
</p>
</li>
</ol>
<hr>
<h4><a name="s-editor-vi-editing-deletion"></a>8.3.2.2 Deleting text</h4>
<ol type="1" start="1" >
<li>
<p>
<samp>vi testfile</samp>
</p>
<p>
Open your practice file again.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>dd</samp>
</p>
<p>
The <samp>dd</samp> command deletes a line; the top line of the file should be
gone now.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>x</samp>
</p>
<p>
<samp>x</samp> deletes a single character; the first letter of the second line
will be erased. Delete and backspace don't work in <code>vi</code>, for
historical reasons[<a href="footnotes.html#f13" name="fr13">13</a>]. Some
<code>vi</code> variants, such as <code>vim</code> will let you use backspace
and delete.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>10x</samp>
</p>
<p>
If you type a number before a command, it will repeat the command that many
times. So this will delete 10 characters.
</p>
</li>
</ol>
<ol type="1" start="5" >
<li>
<p>
<samp>2dd</samp>
</p>
<p>
You can use a number with the <samp>dd</samp> command as well, deleting two
lines.
</p>
</li>
</ol>
<ol type="1" start="6" >
<li>
<p>
<samp>:q</samp>
</p>
<p>
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 <samp>:wq</samp>, thus writing
the file as you quit, or you can quit without saving:
</p>
</li>
</ol>
<ol type="1" start="7" >
<li>
<p>
<samp>:q!</samp>
</p>
<p>
With an exclamation point, you tell <code>vi</code> that you really mean it,
and it should quit even though the file isn't saved. If you use
<samp>:q!</samp> your deletions will not be saved to <samp>testfile</samp>; if
you use <samp>:wq</samp>, they will be.
</p>
</li>
</ol>
<ol type="1" start="8" >
<li>
<p>
<samp>cat testfile</samp>
</p>
<p>
Back at the shell prompt, view <samp>testfile</samp>. It should be shorter
now, if you used <samp>:wq</samp>, or be unchanged if you used
<samp>:q!</samp>.
</p>
</li>
</ol>
<p>
<samp>:q!</samp> 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 <samp>ESC</samp> a few times to be sure you're in
command mode and then type <samp>:q!</samp>. This is guaranteed to get you out
of <code>vi</code> with no damage done.
</p>
<p>
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.
</p>
<hr>
<h4><a name="s-editor-vi-editing-moremoving"></a>8.3.2.3 Sophisticated movement</h4>
<p>
There are many motion commands, here's a quick summary:
</p>
<dl>
<dt><samp>w</samp></dt>
<dd>
<p>
Move to the start of the next word
</p>
</dd>
</dl>
<dl>
<dt><samp>e</samp></dt>
<dd>
<p>
Move to the end of the next word
</p>
</dd>
</dl>
<dl>
<dt><samp>E</samp></dt>
<dd>
<p>
Move to the end of the next word before a space
</p>
</dd>
</dl>
<dl>
<dt><samp>b</samp></dt>
<dd>
<p>
Move to the start of the previous word
</p>
</dd>
</dl>
<dl>
<dt><samp>0</samp> (zero)</dt>
<dd>
<p>
Move to the start of the line
</p>
</dd>
</dl>
<dl>
<dt><samp>^</samp></dt>
<dd>
<p>
Move to the first word of the current line
</p>
</dd>
</dl>
<dl>
<dt><samp>$</samp></dt>
<dd>
<p>
Move to the end of the line
</p>
</dd>
</dl>
<dl>
<dt><samp>RET</samp></dt>
<dd>
<p>
Move to the start of the next line
</p>
</dd>
</dl>
<dl>
<dt><samp>-</samp></dt>
<dd>
<p>
Move to the start of the previous line
</p>
</dd>
</dl>
<dl>
<dt><samp>G</samp></dt>
<dd>
<p>
Move to the end of the file
</p>
</dd>
</dl>
<dl>
<dt><samp>1G</samp></dt>
<dd>
<p>
Move to the start of the file
</p>
</dd>
</dl>
<dl>
<dt><samp><var>n</var>G</samp></dt>
<dd>
<p>
Move to line number <var>n</var>
</p>
</dd>
</dl>
<dl>
<dt><samp>C-G</samp></dt>
<dd>
<p>
Display the current line number
</p>
</dd>
</dl>
<dl>
<dt><samp>H</samp></dt>
<dd>
<p>
Top line of the screen
</p>
</dd>
</dl>
<dl>
<dt><samp>M</samp></dt>
<dd>
<p>
Middle line of the screen
</p>
</dd>
</dl>
<dl>
<dt><samp>L</samp></dt>
<dd>
<p>
Bottom of the screen
</p>
</dd>
</dl>
<dl>
<dt><samp><var>n</var>|</samp></dt>
<dd>
<p>
Move cursor to column <var>n</var>
</p>
</dd>
</dl>
<p>
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.
</p>
<dl>
<dt><samp>C-f</samp></dt>
<dd>
<p>
Scroll forward a screen
</p>
</dd>
</dl>
<dl>
<dt><samp>C-b</samp></dt>
<dd>
<p>
Scroll backward a screen
</p>
</dd>
</dl>
<dl>
<dt><samp>C-d</samp></dt>
<dd>
<p>
Scroll down half a screen
</p>
</dd>
</dl>
<dl>
<dt><samp>C-u</samp></dt>
<dd>
<p>
Scroll down half a screen
</p>
</dd>
</dl>
<hr>
<h4><a name="s-editor-vi-editing-numbers"></a>8.3.2.4 Repeating commands</h4>
<p>
As mentioned above you can often prefix a command with a number to repeat that
command multiple times. For example, the <samp>l</samp> key moves left;
<samp>10l</samp> moves you left 10 positions to the left.
</p>
<p>
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 <var>n</var> then
<samp>i</samp> followed by <samp>SPACE</samp> and <samp>ESC</samp>. You should
get <var>n</var> spaces.
</p>
<p>
The commands that deal with lines use a number to refer to line numbers. The
<samp>G</samp> is a good example; if you preface it with a number it will go to
that line.
</p>
<hr>
<h4><a name="s-editor-vi-editing-advanced"></a>8.3.2.5 Advanced reference</h4>
<p>
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.
</p>
<p>
Insertion commands:
</p>
<dl>
<dt><samp>a</samp></dt>
<dd>
<p>
Append to the right of the cursor
</p>
</dd>
</dl>
<dl>
<dt><samp>A</samp></dt>
<dd>
<p>
Append at the end of the line
</p>
</dd>
</dl>
<dl>
<dt><samp>i</samp></dt>
<dd>
<p>
Insert text to the left of the cursor
</p>
</dd>
</dl>
<dl>
<dt><samp>I</samp></dt>
<dd>
<p>
Insert text to the left of the first non-blank character on current line
</p>
</dd>
</dl>
<dl>
<dt><samp>o</samp></dt>
<dd>
<p>
Open a new line below the current line and insert text
</p>
</dd>
</dl>
<dl>
<dt><samp>O</samp></dt>
<dd>
<p>
Open a new line above the current line and insert text
</p>
</dd>
</dl>
<p>
Deletion commands:
</p>
<dl>
<dt><samp>x</samp></dt>
<dd>
<p>
Delete the character under the cursor
</p>
</dd>
</dl>
<dl>
<dt><samp>dw</samp></dt>
<dd>
<p>
Delete from the current position to the end of the word
</p>
</dd>
</dl>
<dl>
<dt><samp>dd</samp></dt>
<dd>
<p>
Delete the current line.
</p>
</dd>
</dl>
<dl>
<dt><samp>D</samp></dt>
<dd>
<p>
Delete from the current position to the end of the line
</p>
</dd>
</dl>
<p>
Commands in combination can be more powerful. In particular, <samp>d</samp>
followed by a motion command deletes from the cursor to wherever you asked to
move. Some examples:
</p>
<dl>
<dt><samp>d<var>n</var>w</samp></dt>
<dd>
<p>
Deletes <var>n</var> words (<samp><var>n</var>dw</samp> works too)
</p>
</dd>
</dl>
<dl>
<dt><samp>dG</samp></dt>
<dd>
<p>
Delete from the current position to the end of the file
</p>
</dd>
</dl>
<dl>
<dt><samp>d1G</samp></dt>
<dd>
<p>
Delete from the current postion to the start of the file
</p>
</dd>
</dl>
<dl>
<dt><samp>d$</samp></dt>
<dd>
<p>
Delete from current postion to the end of the line (same as <samp>D</samp>)
</p>
</dd>
</dl>
<dl>
<dt><samp>d<var>n</var>$</samp></dt>
<dd>
<p>
Delete from current line the end of the <var>n</var>th line
</p>
</dd>
</dl>
<p>
Undo commands:
</p>
<dl>
<dt><samp>u</samp></dt>
<dd>
<p>
Undo the last command
</p>
</dd>
</dl>
<dl>
<dt><samp>U</samp></dt>
<dd>
<p>
Undo all change to the current line
</p>
</dd>
</dl>
<dl>
<dt><samp>:e!</samp></dt>
<dd>
<p>
&quot;Edit again&quot;. Like quitting with <samp>:q!</samp> and restarting ---
returns you to the last time you did a <samp>:w</samp> to save.
</p>
</dd>
</dl>
<p>
You can undo an undo, so <samp>uu</samp> results in an undone undo, or no
change.
</p>
<p>
Replacement commands:
</p>
<dl>
<dt><samp>r<var>c</var></samp></dt>
<dd>
<p>
Replace the character under the cursor with <var>c</var>
</p>
</dd>
</dl>
<dl>
<dt><samp>R</samp></dt>
<dd>
<p>
Overwrites text
</p>
</dd>
</dl>
<dl>
<dt><samp>cw</samp></dt>
<dd>
<p>
Changes the current word
</p>
</dd>
</dl>
<dl>
<dt><samp>c$</samp></dt>
<dd>
<p>
Changes text from current position to end of the line
</p>
</dd>
</dl>
<dl>
<dt><samp>c<var>n</var>w</samp></dt>
<dd>
<p>
Changes next <var>n</var> words.(same as <var>n</var>cw)
</p>
</dd>
</dl>
<dl>
<dt><samp>c<var>n</var>$</samp></dt>
<dd>
<p>
Changes to the end of the <var>n</var>th line
</p>
</dd>
</dl>
<dl>
<dt><samp>C</samp></dt>
<dd>
<p>
Changes to the end of the line (same as <var>c$</var>)
</p>
</dd>
</dl>
<dl>
<dt><samp>cc</samp></dt>
<dd>
<p>
Changes the current line
</p>
</dd>
</dl>
<dl>
<dt><samp>s</samp></dt>
<dd>
<p>
Substitutes text you type for the current character
</p>
</dd>
</dl>
<dl>
<dt><samp><var>n</var>s</samp></dt>
<dd>
<p>
Substitutes text you type for the next <var>n</var> characters
</p>
</dd>
</dl>
<p>
The commands in the above list which allow you to enter more than a single
character of text have to be exited with the <samp>ESC</samp> key, returning
you to command mode.
</p>
<p>
Cut and paste involves first <em>yanking</em> (cutting or copying) some text
and placing it in a buffer (or &quot;clipboard&quot;); then moving to the
desired new location; then pasting the text.
</p>
<p>
To cut text use the <samp>y</samp> command and its variants:
</p>
<dl>
<dt><samp>yy</samp></dt>
<dd>
<p>
Yank a copy of the current line
</p>
</dd>
</dl>
<dl>
<dt><samp><var>n</var>yy</samp></dt>
<dd>
<p>
Yank the next <var>n</var> lines
</p>
</dd>
</dl>
<dl>
<dt><samp>yw</samp></dt>
<dd>
<p>
Yank a word
</p>
</dd>
</dl>
<dl>
<dt><samp>y<var>n</var>w</samp></dt>
<dd>
<p>
Yank <var>n</var> words
</p>
</dd>
</dl>
<dl>
<dt><samp>y$</samp></dt>
<dd>
<p>
Yank the text between the cursor and the end of the line
</p>
</dd>
</dl>
<p>
Paste commands:
</p>
<dl>
<dt><samp>p</samp></dt>
<dd>
<p>
Paste to the right of the cursor
</p>
</dd>
</dl>
<dl>
<dt><samp>P</samp></dt>
<dd>
<p>
Paste to the left of the cursor
</p>
</dd>
</dl>
<dl>
<dt><samp><var>n</var>P</samp></dt>
<dd>
<p>
Paste <var>n</var> copies to the left of the cursor
</p>
</dd>
</dl>
<p>
When using <code>vi</code> within an xterm or using a variant of
<code>vi</code> that supports X, you can also use the mouse to copy text. See
<a href="ch-X.html">The X Window System, Chapter 10</a> 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.
</p>
<p>
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
<samp>y</samp> commands result in copy-and-paste.
</p>
<p>
<code>vi</code> 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.
</p>
<p>
The simplest search commands look for characters.
</p>
<dl>
<dt><samp>f<var>c</var></samp></dt>
<dd>
<p>
Find the next character <var>c</var> to the right of or below the current
position
</p>
</dd>
</dl>
<dl>
<dt><samp>F<var>c</var></samp></dt>
<dd>
<p>
Find the next character <var>c</var> to the left of or above the current
position
</p>
</dd>
</dl>
<dl>
<dt><samp>t<var>c</var></samp></dt>
<dd>
<p>
Move right to character before the next <var>c</var>.
</p>
</dd>
</dl>
<dl>
<dt><samp>T<var>c</var></samp></dt>
<dd>
<p>
Move left to the character following the preceding <var>c</var>.
</p>
</dd>
</dl>
<dl>
<dt><samp>;</samp></dt>
<dd>
<p>
Repeats the last character search command
</p>
</dd>
</dl>
<dl>
<dt><samp>,</samp></dt>
<dd>
<p>
Same as <samp>;</samp> but reverses the direction of the original command.
</p>
</dd>
</dl>
<p>
If the character you were searching for was not found, <code>vi</code> will
beep or give some other sort of signal. <code>vi</code> allows you to search
for any text, not just a character.
</p>
<dl>
<dt><samp>/<var>text</var></samp></dt>
<dd>
<p>
Searches right and down for the next occurence of <var>text</var>.
</p>
</dd>
</dl>
<dl>
<dt><samp>?<var>text</var></samp></dt>
<dd>
<p>
Searches left and up for the next occurance of <var>text</var>.
</p>
</dd>
</dl>
<dl>
<dt><samp>n</samp></dt>
<dd>
<p>
Repeat the last<samp>/</samp> or <samp>?</samp> command
</p>
</dd>
</dl>
<dl>
<dt><samp>N</samp></dt>
<dd>
<p>
Repeats the last <samp>/</samp> or <samp>?</samp> in the reverse direction
</p>
</dd>
</dl>
<p>
When using the <samp>/</samp> or <samp>?</samp> commands a line will be cleared
along the bottom of the screen. You enter the text to search for followed by
<samp>RET</samp>.
</p>
<p>
The text in the command <samp>/</samp> or <samp>?</samp> is actually a
<em>regular expression</em>, see <a
href="ch-text_tools.html#s-text_tools-regexps">Regular expressions, Section
11.1</a>.
</p>
<hr>
<p>
[ <a href="ch-files.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ <a href="ch-shell.html">6</a> ]
[ <a href="ch-files.html">7</a> ]
[ 8 ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ <a href="ch-advanced.html">16</a> ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-custom_shell.html">next</a> ]
</p>
<hr>
<p>
Debian Tutorial (Obsolete Documentation)
</p>
<address>
29 Dezember 2009<br>
<br>
Havoc Pennington <code><a href="mailto:hp@debian.org">hp@debian.org</a></code><br>
<br>
</address>
<hr>
</body>
</html>